VancedManager/app/src/main/java/com/vanced/manager/ui/fragments/SelectAppsFragment.kt

57 lines
1.9 KiB
Kotlin
Raw Normal View History

2020-10-31 19:45:39 +00:00
package com.vanced.manager.ui.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
2020-11-05 16:35:49 +00:00
import androidx.core.content.edit
2020-10-31 19:45:39 +00:00
import androidx.navigation.fragment.findNavController
2020-11-15 17:04:23 +00:00
import androidx.preference.PreferenceManager.getDefaultSharedPreferences
2020-10-31 19:45:39 +00:00
import androidx.recyclerview.widget.LinearLayoutManager
import com.vanced.manager.R
import com.vanced.manager.adapter.SelectAppsAdapter
import com.vanced.manager.core.ui.base.BindingFragment
2020-10-31 19:45:39 +00:00
import com.vanced.manager.databinding.FragmentSelectAppsBinding
2020-11-14 21:55:29 +00:00
class SelectAppsFragment : BindingFragment<FragmentSelectAppsBinding>() {
2020-10-31 19:45:39 +00:00
2020-11-14 21:55:29 +00:00
private lateinit var selectAdapter: SelectAppsAdapter
2020-10-31 19:45:39 +00:00
2020-11-14 21:55:29 +00:00
override fun binding(
inflater: LayoutInflater,
container: ViewGroup?,
2020-10-31 19:45:39 +00:00
savedInstanceState: Bundle?
2020-11-14 21:55:29 +00:00
) = FragmentSelectAppsBinding.inflate(inflater, container, false)
override fun otherSetups() {
bindData()
2020-10-31 19:45:39 +00:00
}
2020-11-14 21:55:29 +00:00
private fun bindData() {
with(binding) {
initRecycler()
selectAppsFab.setOnClickListener { actionOnClickAppsFab() }
}
}
private fun FragmentSelectAppsBinding.initRecycler() {
selectAdapter = SelectAppsAdapter(requireActivity())
selectAppsRecycler.apply {
2020-10-31 19:45:39 +00:00
layoutManager = LinearLayoutManager(requireActivity())
setHasFixedSize(true)
adapter = selectAdapter
}
2020-11-14 21:55:29 +00:00
}
2020-10-31 19:45:39 +00:00
2020-11-14 21:55:29 +00:00
private fun actionOnClickAppsFab() {
if (selectAdapter.apps.all { app -> !app.isChecked }) {
Toast.makeText(requireActivity(), R.string.select_at_least_one_app, Toast.LENGTH_SHORT).show()
return
2020-10-31 19:45:39 +00:00
}
2020-11-14 21:55:29 +00:00
val prefs = getDefaultSharedPreferences(requireActivity())
selectAdapter.apps.forEach { app ->
prefs.edit { putBoolean("enable_${app.tag}", app.isChecked) }
}
findNavController().navigate(SelectAppsFragmentDirections.selectAppsToGrantRoot())
2020-10-31 19:45:39 +00:00
}
}