Loading...
JavaScript | Как сделать доступным некоторые разделы приложения
В моём приложений есть 6-ой раздел, который доступен для тех, кто приобрел Pro версию.
Хотел бы добавить к 4-ому разделу Pro доступ.
Код представлен ниже:
javascript
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)
    }

}
Думаю здесь нужно добавить 4-раздел
javascript
if (groupPosition != 6) {
                openLibrary?hapter(groupPosition)
            } else {
                if (isPurchasedKey) {
                    openLibrary?hapter(groupPosition)
                } else {
                    showKeyDialog(
                        "Раздел доступен только Pro версии",
                        "Чтобы получить доступ к разделу, купите Pro версию в настройках приложения.",
                        "Перейти в настройки",
                        "Отменить")
                }
            }
кто поможет, тому 50р если будет работать как надо
320517996 (29.03.2022 в 09:30)
}[/hljs]
Думаю здесь нужно добавить 4-раздел
javascript
if (groupPosition != 6) {
                openLibrary?hapter(groupPosition)
            } else {
                if (isPurchasedKey) {
                    openLibrary?hapter(groupPosition)
                } else {
                    showKeyDialog(
                        "Раздел доступен только Pro версии",
                        "Чтобы получить доступ к разделу, купите Pro версию в настройках приложения.",
                        "Перейти в настройки",
                        "Отменить")
                }
            }
php

groupPosition != 6 or groupPosition != 4
Elenrill (29.03.2022 в 11:32)
php

groupPosition != 6 or groupPosition != 4
А это сработает?
320517996 (29.03.2022 в 17:01)

А это сработает?
В душе не чаю.
Elenrill (29.03.2022 в 17:32)

В душе не чаю.
Как бы идёт and, а не or
320517996 (29.03.2022 в 17:01)

А это сработает?
php

groupPosition != 6 AND groupPosition != 4
php
if (groupPosition != 6 && groupPosition != 4) {
Так какой правильный вариант?
FullTwo, Так какой правильный вариант?
Онлайн: 2
Время:
Gen. 0.0895
(c) Bym.Guru 2010-2026