简体   繁体   中英

How to approach two different functionality for android app paid and free version

I have an android app which is free, and I'm going to add some functionality that can only be obtained through paid version. The free app have data hardcoded into the app. Mostly in the strings.xml file. The paid version will get data from a json api. That's a lot more data than the free app. The interface will stay the same. but the functionality when clicking certain buttons will change. Do I need to create two MainActivity for each version. Or how can I change the functionality of the paid version with the leas amount of modification. I guess this question comes down to software design patterns with free and paid version, which I'm trying to figure out. I've already added

productFlavors {
        lite {
            applicationId "com.sample.appname.lite"
            buildConfigField 'boolean', 'IS_PAID', 'false'
            versionCode 1
            versionName '1.3-free'
        }
        pro {
            applicationId "com.sample.appname.pro"
            buildConfigField 'boolean', 'IS_PAID', 'true'
            versionCode 1
            versionName '1.0-paid'
        }
    }

I have created a paid and free java folder under src folder. But don't know if I need to create a new MainActivity for my paid version and another for my free. Any help appreciated

You could do this in the following way:

  • Create a class that will be in charge of getting the information per version type (which I will call InfoRepository , but you should call it something that makes more sense given the type of data you are loading). You will have two instances of this class: one in the paid folder, and one in the free . They will both have a getInfo() method that returns a list of Info objects (for example).
    • Within the free version, implement getInfo() by loading whatever you need to load from the Strings document, and return that. (Side note: it might be worth considering putting that data within a json file in the raw folder, so that the loading of data is the same regardless of whether it's paid or free)
    • Within the paid version, implement getInfo() by loading it from your API, parsing it, and returning the final result.
  • In your MainActivity (only one instance within the main java folder), create an InfoRepository class instance and call getInfo() to populate the screen. This will either load the info from the Strings in the free version, or use the API in the paid version. MainActivity doesn't care where the data is coming from, it just cares that it has some data to display. The InfoRepository classes in each flavor takes care of loading it from the right source.

A few notes:

  • You can build InfoRepository in any way that makes the most sense. I don't know what your use case is, but say for example that you have a list of Note s that you show if the user clicks on the "Show Notes" button, and a list of Score s that you show if they click on the "Show Scores" button. In this case, you could have two methods, getNotes() and getScores() within the InfoRepository .
  • Given that in the paid version you are querying an API, you will need to be careful about threading and potentially asynchronous results. Depending on what tools you are using, you might want to use a callback system or a Deferred object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM