Introduction to the

This article belongs to the Kotlin Jetpack In Action series.

This is an “original code” App I wrote in Java called the Kotlinjet Package Inaction, which has only one function: Worship the God!

To help you understand Kotlin, Coroutines, Jetpack, Functional Programming, and MMVM, this Demo is extremely simple. As the article gets updated, I’ll take steps to refactor it with Kotlin and Jetpack, and then add some new features.

screenshots

There’s a cult presence in Android that everyone knows about: JakeWharton

Engineering structure

MainActivity: Used to worship the great god

ImagePreviewActivity: Used to view the head of a god

MainActivity source

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "Main";
    public static final String EXTRA_PHOTO = "photo";

    StringRequest stringRequest;
    RequestQueue requestQueue;

    private ImageView image;
    private ImageView gif;
    private TextView username;
    private TextView company;
    private TextView website;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(a) {
        image = findViewById(R.id.image);
        gif = findViewById(R.id.gif);
        username = findViewById(R.id.username);
        company = findViewById(R.id.company);
        website = findViewById(R.id.website);

        display(User.CACHE_RESPONSE);
        requestOnlineInfo();
    }

    private void requestOnlineInfo(a) {
        requestQueue = Volley.newRequestQueue(this);
        String url ="https://api.github.com/users/JakeWharton";
        stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) { display(response); }},new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show(); }}); stringRequest.setTag(TAG); requestQueue.add(stringRequest); }private void display(@Nullable String response) {
        if (TextUtils.isEmpty(response)) { return; }

        Gson gson = new Gson();
        final User user = gson.fromJson(response, User.class);
        if(user ! =null){
            Glide.with(this).load("file:///android_asset/bless.gif").into(gif);
            Glide.with(this).load(user.getAvatar_url()).apply(RequestOptions.circleCropTransform()).into(image);
            this.username.setText(user.getName());
            this.company.setText(user.getCompany());
            this.website.setText(user.getBlog());

            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { gotoImagePreviewActivity(user); }}); }}private void gotoImagePreviewActivity(User user) {
        Intent intent = new Intent(this, ImagePreviewActivity.class);
        intent.putExtra(EXTRA_PHOTO, user.getAvatar_url());
        startActivity(intent);
    }

    @Override
    protected void onStop (a) {
        super.onStop();
        if(requestQueue ! =null) { requestQueue.cancelAll(TAG); }}}Copy the code

ImagePreviewActivity source

public class ImagePreviewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_preview);

        Intent intent = getIntent();
        String url = intent.getStringExtra(MainActivity.EXTRA_PHOTO);
        if(! TextUtils.isEmpty(url)) { ImageView imageView = findViewById(R.id.imagePreview); Glide.with(this).load(url).into(imageView); }}}Copy the code

At the end

I believe that even beginners can easily understand this Demo.

That’s what makes this series of articles different. I’m going to walk you through the “high end” techniques, starting with this simple Demo, and eventually turning this “simple “App into a” high end “App.

This Demo is open source on GitHub, welcomestar.forkPlease also make an Issue with your suggestions:Github.com/chaxiu/Kotl…

Kotlin Jetpack Combat

Now that you’ve seen it, give it a thumbs up!