M
M
Muslim Mamaev2022-03-28 21:45:21
Android
Muslim Mamaev, 2022-03-28 21:45:21

How to make some sections of the application available after purchasing the Pro version?

There is a 6th section in my apps, which is available for those who have purchased the Pro version.
I would like to make the 4th partition available after purchasing the Pro version.
The code is presented below:

class LibraryFragment : Fragment() {

    var expandableListView: ExpandableListView? = null
    var expandableListAdapter: ExpandableListAdapter? = null
    var expandableListTitle: List<String>? = null
    var expandableListDetail: LinkedHashMap<String, List<Int>>? = null
    private var doubleBackToExitPressedOnce = false
    private var isPurchasedKey = false
    lateinit var sPref: SharedPreferences
    private var prefPurchaseKey = Constants.PREF_PURCHASED_KEY_PRO

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)

        activity?.onBackPressedDispatcher?.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                if (doubleBackToExitPressedOnce) {
                    requireActivity().finish()
                    return
                }

                doubleBackToExitPressedOnce = true
                Toast.makeText(requireActivity(), getString(R.string.toast), Toast.LENGTH_SHORT).show()

                Handler().postDelayed(Runnable { doubleBackToExitPressedOnce = false }, 2000)
            }

        })
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_library, container, false)
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.library_menu, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        when(item.itemId){

            R.id.searchArticles->{
                findNavController().navigate(R.id.navigation_search_texts)
            }

            R.id.favoritesTexts->{
                val intent = Intent(context, FavoriteTextActivity::class.java)
                startActivity(intent)
            }

        }
        return super.onOptionsItemSelected(item)
    }

    override fun onStart() {
        super.onStart()

        sPref = requireActivity().getSharedPreferences("Settings", Context.MODE_PRIVATE)
        isPurchasedKey = sPref.getBoolean(prefPurchaseKey, false)

        expandableListView = view?.findViewById(R.id.expandableListView) as ExpandableListView
        expandableListDetail = LibraryListDataPump.getData() as LinkedHashMap<String, List<Int>>?
        expandableListTitle = ArrayList(expandableListDetail!!.keys)
        expandableListAdapter =
                LibraryExpandableListAdapter(
                        activity,
                        expandableListTitle,
                        null,
                        isPurchasedKey
                )
        expandableListView!!.setAdapter(expandableListAdapter)
        expandableListView!!.setOnGroupExpandListener { groupPosition ->
            if (groupPosition != 6) {
                openLibrary?hapter(groupPosition)
            } else {
                if (isPurchasedKey) {
                    openLibrary?hapter(groupPosition)
                } else {
                    showKeyDialog(
                        "Раздел доступен только Pro версии",
                        "Чтобы получить доступ к разделу, купите Pro версию в настройках приложения.",
                        "Перейти в настройки",
                        "Отменить")
                }
            }

        }
    }

    fun showKeyDialog(header: String, message: String, positiveButtonText:String, negativeButtonText:String){
        var builder = AlertDialog.Builder(activity)
        builder.setMessage(message)
        builder.setTitle(header)
        builder.setPositiveButton(positiveButtonText) { dialogInterface: DialogInterface, i: Int ->
            dialogInterface.cancel()
            val i= Intent(activity, SettingsActivity::class.java)
            startActivity(i)
        }
        builder.setNegativeButton(negativeButtonText) { dialogInterface: DialogInterface, i: Int ->
            dialogInterface.cancel()
        }
        builder.create().show()
    }

    private fun openLibrary?hapter(groupPosition: Int) {
        val i = Intent(activity, SubLibraryListActivity::class.java)
        val l: ArrayList<List<Int>> = ArrayList<List<Int>>(expandableListDetail!!.values)

        i.putExtra("LibraryGroupID", (groupPosition + 1).toString())
        i.putExtra("LibraryGroupTitle", (expandableListTitle as ArrayList<String>)[groupPosition])
        i.putExtra("LibraryList", l[groupPosition] as java.util.ArrayList<Int>)
        startActivity(i)
    }

}


I think something needs to be done here.if (groupPosition != 6)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question