Play Android from 0 to 1 project summary

preface

This is the seventh article in the series, and the last one I intend to write. Here are the first six:

A general overview of playing Android from 0 to 1

2, play Android from 0 to 1 project home page

3. Play Android home page frame building from 0 to 1.

4. Thinking about the architecture of playing Android from 0 to 1

5, Play android from 0 to 1 adapter thinking

6. Play android from 0 to 1 with one click at the top

As usual, put the Github address and apK download address.

Apk download address: www.pgyer.com/llj2

Github address: github.com/zhujiang521…

When the series first began writing feel that there are so many things to write, but write and don’t know what to write, is not don’t know what to write, how to say, there is something that he could not consolidate before, at the time of writing this project and have strengthened, a place like this a lot, also said that difficult is easy, simple is not simple, Afraid of writing too Low for everyone useless, so did not write back when do not know what to write…

On second thought, what is the purpose of blogging? At the beginning of writing, I just want to take notes and record all the problems that are easy to make mistakes, and then I can remember them quickly. That’s all. But slowly, more and more people read my blog, writing is not so arbitrary at the beginning, afraid of writing bad others say, timid.

No, no, no, no, no, no, in the future, I will write what I want to write in the blog, no longer feel that everyone will stop writing, because there must be someone like me.

If you have any questions about this project, please let me know in the comments section, or send me issues on Github.

Little question left

Before summarizing, there is another problem that needs to be solved. Some time ago, I saw some comments on the article, which probably meant that the Base class of the project is too bloated and has too many responsibilities. Indeed, there are a lot of things written in BaseActivity and BaseFragment, and I want to extract all the public things together. I didn’t think about it before, actually I did that when I wrote the MVP, but I didn’t do that in this project, so let’s do it!

comments

After looking at the code of BaseActivity and BaseFragment, I think the LCE module should be extracted most. Because there is this code in both BaseActivity and BaseFragment, it can be extracted separately.

Let’s take a look at the previous code. I don’t want to post the code for space reasons, but I’ll give you a link to view the historical code:

Github.com/zhujiang521…

The LCE logic in BaseActivity and BaseFragment is similar. The LCE logic in BaseActivity and BaseFragment is similar. The LCE logic in BaseActivity and BaseFragment is similar. However, the logic of BaseActivity and BaseFragment is basically the same, so it is not appropriate to write in this way. We can completely extract this part and call it directly in BaseActivity and BaseFragment.

interface ILce {



    fun startLoading(a)



    fun loadFinished(a)



    / * *

* This method displays a prompt to the user when the content server in the Activity fails to load.

     *

     * @param tip

* Prompt information on the interface

* /


    fun showLoadErrorView(tip: String= Play.context!! .getString(R.string.failed_load_data))



    / * *

* Use this method to display a prompt to the user when the content in the Activity cannot be displayed due to network reasons.

     *

     * @param listener

* Reload the click event callback

* /


    fun showBadNetworkView(listener: View.OnClickListener)



    / * *

* Use this method to display a prompt to the user when there is no content in the Activity.

     * @param tip

* Prompt information on the interface

* /


    fun showNoContentView(tip: String)



}

Copy the code

The BaseActivity and BaseFragment classes are implemented in the BaseActivity and BaseFragment classes. The BaseActivity and BaseFragment classes are implemented in the BaseActivity and BaseFragment classes.

class DefaultLceImpl constructor(

    private valloading: ProgressBar? .

    private valloadErrorView: View? .

    private valbadNetworkView: View? .

    private val noContentView: View?

) : ILce {



    override fun startLoading(a) {

        loadFinished()

loading? .visibility = View.VISIBLE

    }



    override fun loadFinished(a) {

loading? .visibility = View.GONE

badNetworkView? .visibility = View.GONE

noContentView? .visibility = View.GONE

loadErrorView? .visibility = View.GONE

    }



    override fun showLoadErrorView(tip: String) {

        loadFinished()

        valloadErrorText = loadErrorView? .findViewById<TextView>(R.id.loadErrorText)

loadErrorText? .text = tip

loadErrorView? .visibility = View.VISIBLE

    }



    override fun showBadNetworkView(listener: View.OnClickListener) {

        loadFinished()

badNetworkView? .visibility = View.VISIBLE

badNetworkView? .setOnClickListener(listener)

    }



    override fun showNoContentView(tip: String) {

        loadFinished()

        valnoContentText = noContentView? .findViewById<TextView>(R.id.noContentText)

noContentText? .text = tip

noContentView? .visibility = View.VISIBLE

    }



}

Copy the code

That’s OK, isn’t it easy? The constructor takes four arguments from the View. The name of each argument should give you an idea of what each argument means. The rest is the basic operation.

Let’s look at how to write BaseActivity and BaseFragment.

The LCE interface must also be implemented first:

abstract class BaseActivity : AppCompatActivity(), ILce, BaseInit {}

Copy the code

We don’t need to put these states in BaseActivity and BaseFragment anymore, because we’ve already written our implementation classes and we just need to use them:

defaultLce = DefaultLceImpl(

    loading,

    loadErrorView,

    badNetworkView,

    noContentView

)

Copy the code

Initialization is very simple, just need to use a few views to pass in.

The code of the specific method is as follows:

@CallSuper

override fun startLoading(a) {

defaultLce? .startLoading()

}



@CallSuper

override fun loadFinished(a) {

defaultLce? .loadFinished()

}



@CallSuper

override fun showLoadErrorView(tip: String) {

defaultLce? .showLoadErrorView(tip)

}



@CallSuper

override fun showBadNetworkView(listener: View.OnClickListener) {

defaultLce? .showBadNetworkView(listener)

}



@CallSuper

override fun showNoContentView(tip: String) {

defaultLce? .showNoContentView(tip)

}

Copy the code

BaseActivity this is ok, BaseFragment is the same, the code is basically the same, there is no need to repeat here.

Project summary

This small project has been written for a long time. The first version was completed in May, 2000, and nothing has been done since then. It started again in September, and finally stabilized the project by the end of last year, which was tested on Both Xiaomi cloud platform and Vivo cloud platform, and all the results were passed.

The project uses the most popular Android MVVM framework and is basically completed in accordance with the official design drawings. Many JackPek components are also used in the project, such as ViewModel, Room, DataStore and so on. Coroutines are generally used to replace the previous threads.

Some of the code in the project was written in Java before, and then all of it was converted to Kotlin, which helped me and everyone else to better understand the use of Kotlin.

By writing this little project their learning to a lot of things, a lot of things is no longer an armchair strategist, is also in the practical application to the project, very good, you can also write a small program, if when you learn a new things can directly into your small projects, so you don’t need to write a bunch of small Demo, and can also achieve mastery through a comprehensive study.

At the end

Sometimes I really can’t see through the official blog, I inadvertently write an article, the official hard to recommend you, a few days to read can break ten thousand; And when you spend a few days to write a good article, the amount of reading is only more than 100… Also do not know what to say, may be because of the readily written article is more easy to understand!

It’s already 2021 in a blink of an eye. I wrote a year-end summary two days ago, and today CAME a project summary, although nothing was said…

What do you call that blessing? 2020 rat is not easy, 2021 cattle turn the universe!

All the unhappiness in 2020 has passed, we usher in the New Year of 2021, and this year is their birth year!

I hope the epidemic will pass soon and everyone will get better and better.

I almost forgot that I got into the top 200 in CSDN’s Blog Star event and now need to vote for the top spot… Now that we are in, let’s hope we can get ahead. This activity is very that what, every day can vote, made a circle of friends, after all embarrassed to find others, once also go, every day to find others is too much 😂, if you have votes can point the following link to help me cast a vote, grateful.

Bss.csdn.net/m/topic/blo…