mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 18:56:44 -04:00
Moved the common toolbar functionality to the BaseFragment
This commit is contained in:
parent
79e43d8735
commit
f8594b0183
@ -38,7 +38,7 @@ import android.view.View
|
|||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.widget.Toolbar
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import org.kiwix.kiwixmobile.R
|
import org.kiwix.kiwixmobile.R
|
||||||
@ -89,6 +89,12 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
|
|||||||
private lateinit var serviceConnection: ServiceConnection
|
private lateinit var serviceConnection: ServiceConnection
|
||||||
private var dialog: Dialog? = null
|
private var dialog: Dialog? = null
|
||||||
private var activityZimHostBinding: ActivityZimHostBinding? = null
|
private var activityZimHostBinding: ActivityZimHostBinding? = null
|
||||||
|
override val fragmentTitle: String? by lazy {
|
||||||
|
getString(R.string.menu_wifi_hotspot)
|
||||||
|
}
|
||||||
|
override val fragmentToolbar: Toolbar? by lazy {
|
||||||
|
activityZimHostBinding?.root?.findViewById(R.id.toolbar)
|
||||||
|
}
|
||||||
private val selectedBooksPath: ArrayList<String>
|
private val selectedBooksPath: ArrayList<String>
|
||||||
get() {
|
get() {
|
||||||
return booksAdapter.items
|
return booksAdapter.items
|
||||||
@ -122,7 +128,6 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
|
|||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
setUpToolbar(view)
|
|
||||||
|
|
||||||
bookDelegate =
|
bookDelegate =
|
||||||
BookOnDiskDelegate.BookDelegate(sharedPreferenceUtil, multiSelectAction = ::select)
|
BookOnDiskDelegate.BookDelegate(sharedPreferenceUtil, multiSelectAction = ::select)
|
||||||
@ -389,16 +394,6 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
|
|||||||
activityZimHostBinding = null
|
activityZimHostBinding = null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setUpToolbar(view: View) {
|
|
||||||
val activity = requireActivity() as AppCompatActivity
|
|
||||||
activity.setSupportActionBar(view.findViewById(R.id.toolbar))
|
|
||||||
activity.supportActionBar?.apply {
|
|
||||||
title = getString(R.string.menu_wifi_hotspot)
|
|
||||||
setHomeButtonEnabled(true)
|
|
||||||
setDisplayHomeAsUpEnabled(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Advice user to turn on hotspot manually for API<26
|
// Advice user to turn on hotspot manually for API<26
|
||||||
private fun startHotspotManuallyDialog() {
|
private fun startHotspotManuallyDialog() {
|
||||||
|
|
||||||
|
@ -19,6 +19,10 @@
|
|||||||
package org.kiwix.kiwixmobile.core.base
|
package org.kiwix.kiwixmobile.core.base
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.View
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.appcompat.widget.Toolbar
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,10 +31,35 @@ import androidx.fragment.app.Fragment
|
|||||||
|
|
||||||
abstract class BaseFragment : Fragment() {
|
abstract class BaseFragment : Fragment() {
|
||||||
|
|
||||||
|
open val fragmentToolbar: Toolbar? = null
|
||||||
|
open val fragmentTitle: String? = null
|
||||||
|
|
||||||
override fun onAttach(context: Context) {
|
override fun onAttach(context: Context) {
|
||||||
super.onAttach(context)
|
super.onAttach(context)
|
||||||
inject(activity as BaseActivity)
|
inject(activity as BaseActivity)
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract fun inject(baseActivity: BaseActivity)
|
abstract fun inject(baseActivity: BaseActivity)
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
setupToolbar()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup toolbar to handle back pressed event
|
||||||
|
private fun setupToolbar() {
|
||||||
|
val activity = activity as AppCompatActivity?
|
||||||
|
fragmentToolbar?.apply {
|
||||||
|
activity?.setSupportActionBar(this)
|
||||||
|
setNavigationOnClickListener {
|
||||||
|
activity?.apply {
|
||||||
|
onBackPressedDispatcher.onBackPressed()
|
||||||
|
supportActionBar?.let { actionBar ->
|
||||||
|
actionBar.setDisplayHomeAsUpEnabled(true)
|
||||||
|
fragmentTitle?.let { title = it }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,10 @@ import org.kiwix.kiwixmobile.core.utils.LanguageUtils.Companion.getCurrentLocale
|
|||||||
abstract class HelpFragment : BaseFragment() {
|
abstract class HelpFragment : BaseFragment() {
|
||||||
private var fragmentHelpBinding: FragmentHelpBinding? = null
|
private var fragmentHelpBinding: FragmentHelpBinding? = null
|
||||||
protected open fun rawTitleDescriptionMap(): List<Pair<Int, Int>> = emptyList()
|
protected open fun rawTitleDescriptionMap(): List<Pair<Int, Int>> = emptyList()
|
||||||
|
override val fragmentToolbar: Toolbar? by lazy {
|
||||||
|
fragmentHelpBinding?.root?.findViewById(R.id.toolbar)
|
||||||
|
}
|
||||||
|
override val fragmentTitle: String? by lazy { getString(R.string.menu_help) }
|
||||||
|
|
||||||
private val titleDescriptionMap by lazy {
|
private val titleDescriptionMap by lazy {
|
||||||
rawTitleDescriptionMap().associate { (title, description) ->
|
rawTitleDescriptionMap().associate { (title, description) ->
|
||||||
@ -61,15 +65,6 @@ abstract class HelpFragment : BaseFragment() {
|
|||||||
fragmentHelpBinding?.activityHelpFeedbackTextView?.setOnClickListener { sendFeedback() }
|
fragmentHelpBinding?.activityHelpFeedbackTextView?.setOnClickListener { sendFeedback() }
|
||||||
fragmentHelpBinding?.activityHelpFeedbackImageView?.setOnClickListener { sendFeedback() }
|
fragmentHelpBinding?.activityHelpFeedbackImageView?.setOnClickListener { sendFeedback() }
|
||||||
fragmentHelpBinding?.diagnosticClickableArea?.setOnClickListener { sendDiagnosticReport() }
|
fragmentHelpBinding?.diagnosticClickableArea?.setOnClickListener { sendDiagnosticReport() }
|
||||||
val toolbar: Toolbar? = fragmentHelpBinding?.root?.findViewById(R.id.toolbar)
|
|
||||||
toolbar?.apply {
|
|
||||||
activity.setSupportActionBar(this)
|
|
||||||
setNavigationOnClickListener { requireActivity().onBackPressedDispatcher.onBackPressed() }
|
|
||||||
}
|
|
||||||
activity.supportActionBar?.let {
|
|
||||||
it.setDisplayHomeAsUpEnabled(true)
|
|
||||||
it.setTitle(R.string.menu_help)
|
|
||||||
}
|
|
||||||
fragmentHelpBinding?.activityHelpRecyclerView?.addItemDecoration(
|
fragmentHelpBinding?.activityHelpRecyclerView?.addItemDecoration(
|
||||||
DividerItemDecoration(activity, DividerItemDecoration.VERTICAL)
|
DividerItemDecoration(activity, DividerItemDecoration.VERTICAL)
|
||||||
)
|
)
|
||||||
|
@ -69,6 +69,10 @@ abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActiv
|
|||||||
abstract val pageAdapter: PageAdapter
|
abstract val pageAdapter: PageAdapter
|
||||||
abstract val switchIsChecked: Boolean
|
abstract val switchIsChecked: Boolean
|
||||||
private var fragmentPageBinding: FragmentPageBinding? = null
|
private var fragmentPageBinding: FragmentPageBinding? = null
|
||||||
|
override val fragmentToolbar: Toolbar? by lazy {
|
||||||
|
fragmentPageBinding?.root?.findViewById(R.id.toolbar)
|
||||||
|
}
|
||||||
|
override val fragmentTitle: String? by lazy { screenTitle }
|
||||||
|
|
||||||
private val actionModeCallback: ActionMode.Callback =
|
private val actionModeCallback: ActionMode.Callback =
|
||||||
object : ActionMode.Callback {
|
object : ActionMode.Callback {
|
||||||
@ -135,15 +139,6 @@ abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActiv
|
|||||||
fragmentPageBinding?.recyclerView?.layoutManager =
|
fragmentPageBinding?.recyclerView?.layoutManager =
|
||||||
LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
|
LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
|
||||||
fragmentPageBinding?.recyclerView?.adapter = pageAdapter
|
fragmentPageBinding?.recyclerView?.adapter = pageAdapter
|
||||||
val toolbar = fragmentPageBinding?.root?.findViewById<Toolbar>(R.id.toolbar)
|
|
||||||
toolbar?.apply {
|
|
||||||
activity.setSupportActionBar(this)
|
|
||||||
setNavigationOnClickListener { requireActivity().onBackPressedDispatcher.onBackPressed() }
|
|
||||||
}
|
|
||||||
activity.supportActionBar?.apply {
|
|
||||||
setDisplayHomeAsUpEnabled(true)
|
|
||||||
title = screenTitle
|
|
||||||
}
|
|
||||||
fragmentPageBinding?.noPage?.text = noItemsString
|
fragmentPageBinding?.noPage?.text = noItemsString
|
||||||
|
|
||||||
fragmentPageBinding?.pageSwitch?.text = switchString
|
fragmentPageBinding?.pageSwitch?.text = switchString
|
||||||
|
@ -21,7 +21,6 @@ import android.os.Bundle
|
|||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
|
||||||
import androidx.appcompat.widget.Toolbar
|
import androidx.appcompat.widget.Toolbar
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import org.kiwix.kiwixmobile.core.R
|
import org.kiwix.kiwixmobile.core.R
|
||||||
@ -31,12 +30,18 @@ import org.kiwix.kiwixmobile.core.databinding.SettingsBinding
|
|||||||
abstract class CoreSettingsFragment : BaseFragment() {
|
abstract class CoreSettingsFragment : BaseFragment() {
|
||||||
private lateinit var prefsFragment: Fragment
|
private lateinit var prefsFragment: Fragment
|
||||||
private var settingsBinding: SettingsBinding? = null
|
private var settingsBinding: SettingsBinding? = null
|
||||||
|
override val fragmentToolbar: Toolbar? by lazy {
|
||||||
|
settingsBinding?.root?.findViewById(R.id.toolbar)
|
||||||
|
}
|
||||||
|
override val fragmentTitle: String? by lazy {
|
||||||
|
getString(R.string.menu_settings)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
prefsFragment = createPreferenceFragment()
|
prefsFragment = createPreferenceFragment()
|
||||||
requireActivity().supportFragmentManager.beginTransaction()
|
requireActivity().supportFragmentManager.beginTransaction()
|
||||||
.replace(R.id.content_frame, prefsFragment).commit()
|
.replace(R.id.content_frame, prefsFragment).commit()
|
||||||
setUpToolbar()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
@ -50,21 +55,6 @@ abstract class CoreSettingsFragment : BaseFragment() {
|
|||||||
|
|
||||||
protected abstract fun createPreferenceFragment(): Fragment
|
protected abstract fun createPreferenceFragment(): Fragment
|
||||||
|
|
||||||
private fun setUpToolbar() {
|
|
||||||
val activity = requireActivity() as AppCompatActivity
|
|
||||||
settingsBinding?.root?.findViewById<Toolbar>(R.id.toolbar)?.apply {
|
|
||||||
activity.setSupportActionBar(this)
|
|
||||||
setNavigationOnClickListener {
|
|
||||||
requireActivity().onBackPressedDispatcher.onBackPressed()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
activity.supportActionBar?.apply {
|
|
||||||
title = getString(R.string.menu_settings)
|
|
||||||
setHomeButtonEnabled(true)
|
|
||||||
setDisplayHomeAsUpEnabled(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroyView() {
|
override fun onDestroyView() {
|
||||||
requireActivity().supportFragmentManager.beginTransaction().remove(prefsFragment)
|
requireActivity().supportFragmentManager.beginTransaction().remove(prefsFragment)
|
||||||
.commitNowAllowingStateLoss()
|
.commitNowAllowingStateLoss()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user