mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-20 18:48:16 -04:00
#2238 updated lifecycle of bookmark & history viewmodels
This commit is contained in:
parent
b6893942dc
commit
2508061c2a
@ -34,7 +34,7 @@ import kotlinx.android.synthetic.main.activity_kiwix_main.drawer_nav_view
|
||||
import kotlinx.android.synthetic.main.activity_kiwix_main.navigation_container
|
||||
import kotlinx.android.synthetic.main.activity_kiwix_main.reader_drawer_nav_view
|
||||
import org.kiwix.kiwixmobile.R
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
|
||||
import org.kiwix.kiwixmobile.core.di.components.CoreComponent
|
||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.intent
|
||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.start
|
||||
@ -117,7 +117,7 @@ class KiwixMainActivity : CoreMainActivity() {
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
supportFragmentManager.fragments.filterIsInstance<BaseFragmentActivityExtensions>().forEach {
|
||||
supportFragmentManager.fragments.filterIsInstance<FragmentActivityExtensions>().forEach {
|
||||
it.onNewIntent(intent, this)
|
||||
}
|
||||
}
|
||||
|
@ -23,14 +23,14 @@ import android.view.ActionMode
|
||||
import android.view.Menu
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.navigation.fragment.NavHostFragment
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions.Super
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions.Super.ShouldCall
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions.Super.ShouldNotCall
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldCall
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldNotCall
|
||||
import org.kiwix.kiwixmobile.core.main.KiwixWebView
|
||||
import org.kiwix.kiwixmobile.core.main.WebViewProvider
|
||||
|
||||
class NavigationHostFragment : NavHostFragment(), WebViewProvider, BaseFragmentActivityExtensions {
|
||||
class NavigationHostFragment : NavHostFragment(), WebViewProvider, FragmentActivityExtensions {
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
childFragmentManager.fragments.forEach { it.onActivityResult(requestCode, resultCode, data) }
|
||||
@ -57,7 +57,7 @@ class NavigationHostFragment : NavHostFragment(), WebViewProvider, BaseFragmentA
|
||||
activity: AppCompatActivity
|
||||
): Super {
|
||||
var result = ShouldCall
|
||||
childFragmentManager.fragments.filterIsInstance<BaseFragmentActivityExtensions>().forEach {
|
||||
childFragmentManager.fragments.filterIsInstance<FragmentActivityExtensions>().forEach {
|
||||
if (it.onActionModeStarted(actionMode, activity) == ShouldNotCall) {
|
||||
result = ShouldNotCall
|
||||
}
|
||||
@ -70,7 +70,7 @@ class NavigationHostFragment : NavHostFragment(), WebViewProvider, BaseFragmentA
|
||||
activity: AppCompatActivity
|
||||
): Super {
|
||||
var result = ShouldCall
|
||||
childFragmentManager.fragments.filterIsInstance<BaseFragmentActivityExtensions>().forEach {
|
||||
childFragmentManager.fragments.filterIsInstance<FragmentActivityExtensions>().forEach {
|
||||
if (it.onActionModeFinished(actionMode, activity) == ShouldNotCall) {
|
||||
result = ShouldNotCall
|
||||
}
|
||||
@ -80,7 +80,7 @@ class NavigationHostFragment : NavHostFragment(), WebViewProvider, BaseFragmentA
|
||||
|
||||
override fun onBackPressed(activity: AppCompatActivity): Super {
|
||||
var result = ShouldCall
|
||||
childFragmentManager.fragments.filterIsInstance<BaseFragmentActivityExtensions>().forEach {
|
||||
childFragmentManager.fragments.filterIsInstance<FragmentActivityExtensions>().forEach {
|
||||
if (it.onBackPressed(activity) == ShouldNotCall) {
|
||||
result = ShouldNotCall
|
||||
}
|
||||
@ -93,7 +93,7 @@ class NavigationHostFragment : NavHostFragment(), WebViewProvider, BaseFragmentA
|
||||
activity: AppCompatActivity
|
||||
): Super {
|
||||
var result = ShouldCall
|
||||
childFragmentManager.fragments.filterIsInstance<BaseFragmentActivityExtensions>().forEach {
|
||||
childFragmentManager.fragments.filterIsInstance<FragmentActivityExtensions>().forEach {
|
||||
if (it.onNewIntent(intent, activity) == ShouldNotCall) {
|
||||
result = ShouldNotCall
|
||||
}
|
||||
@ -106,7 +106,7 @@ class NavigationHostFragment : NavHostFragment(), WebViewProvider, BaseFragmentA
|
||||
activity: AppCompatActivity
|
||||
): Super {
|
||||
var result = ShouldCall
|
||||
childFragmentManager.fragments.filterIsInstance<BaseFragmentActivityExtensions>().forEach {
|
||||
childFragmentManager.fragments.filterIsInstance<FragmentActivityExtensions>().forEach {
|
||||
if (it.onCreateOptionsMenu(menu, activity) == ShouldNotCall) {
|
||||
result = ShouldNotCall
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ import androidx.appcompat.widget.SearchView
|
||||
import androidx.appcompat.widget.Toolbar
|
||||
import org.kiwix.kiwixmobile.R
|
||||
import org.kiwix.kiwixmobile.core.base.BaseActivity
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
|
||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.start
|
||||
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
||||
import org.kiwix.kiwixmobile.core.utils.SimpleTextListener
|
||||
@ -38,7 +38,7 @@ import org.kiwix.kiwixmobile.kiwixActivityComponent
|
||||
import org.kiwix.kiwixmobile.language.LanguageActivity
|
||||
import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment
|
||||
|
||||
class OnlineLibraryFragment : LibraryFragment(), BaseFragmentActivityExtensions {
|
||||
class OnlineLibraryFragment : LibraryFragment(), FragmentActivityExtensions {
|
||||
|
||||
override fun inject(baseActivity: BaseActivity) {
|
||||
baseActivity.kiwixActivityComponent.inject(this)
|
||||
@ -57,9 +57,9 @@ class OnlineLibraryFragment : LibraryFragment(), BaseFragmentActivityExtensions
|
||||
zimManageViewModel.requestFiltering.onNext("")
|
||||
}
|
||||
|
||||
override fun onBackPressed(activity: AppCompatActivity): BaseFragmentActivityExtensions.Super {
|
||||
override fun onBackPressed(activity: AppCompatActivity): FragmentActivityExtensions.Super {
|
||||
getActivity()?.finish()
|
||||
return BaseFragmentActivityExtensions.Super.ShouldNotCall
|
||||
return FragmentActivityExtensions.Super.ShouldNotCall
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
|
@ -40,9 +40,9 @@ import org.json.JSONArray
|
||||
import org.kiwix.kiwixmobile.R
|
||||
import org.kiwix.kiwixmobile.core.R.anim
|
||||
import org.kiwix.kiwixmobile.core.base.BaseActivity
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions.Super
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions.Super.ShouldCall
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions.Super.ShouldNotCall
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldCall
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldNotCall
|
||||
import org.kiwix.kiwixmobile.core.extensions.getAttribute
|
||||
import org.kiwix.kiwixmobile.core.extensions.setImageDrawableCompat
|
||||
import org.kiwix.kiwixmobile.core.extensions.snack
|
||||
@ -82,9 +82,7 @@ class KiwixReaderFragment : CoreReaderFragment() {
|
||||
activity.supportActionBar!!.setDisplayHomeAsUpEnabled(true)
|
||||
activity.setupDrawerToggle(toolbar)
|
||||
setFragmentContainerBottomMarginToSizeOfNavBar()
|
||||
if (arguments != null) {
|
||||
openPageInBookFromNavigationArguments()
|
||||
}
|
||||
openPageInBookFromNavigationArguments()
|
||||
}
|
||||
|
||||
private fun openPageInBookFromNavigationArguments() {
|
||||
|
@ -22,9 +22,9 @@ import android.content.Intent
|
||||
import android.view.ActionMode
|
||||
import android.view.Menu
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions.Super.ShouldCall
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldCall
|
||||
|
||||
interface BaseFragmentActivityExtensions {
|
||||
interface FragmentActivityExtensions {
|
||||
enum class Super {
|
||||
ShouldCall,
|
||||
ShouldNotCall
|
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Kiwix Android
|
||||
* Copyright (c) 2020 Kiwix <android.kiwix.org>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.kiwix.kiwixmobile.core.extensions
|
||||
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
|
||||
inline fun <reified T : ViewModel> Fragment.viewModel(
|
||||
viewModelFactory: ViewModelProvider.Factory
|
||||
) =
|
||||
ViewModelProviders.of(this, viewModelFactory)
|
||||
.get(T::class.java)
|
@ -29,7 +29,7 @@ import androidx.navigation.NavController
|
||||
import androidx.navigation.NavDirections
|
||||
import org.kiwix.kiwixmobile.core.R
|
||||
import org.kiwix.kiwixmobile.core.base.BaseActivity
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
|
||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.start
|
||||
import org.kiwix.kiwixmobile.core.extensions.browserIntent
|
||||
import org.kiwix.kiwixmobile.core.help.HelpActivity
|
||||
@ -65,21 +65,21 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
|
||||
|
||||
override fun onActionModeStarted(mode: ActionMode) {
|
||||
super.onActionModeStarted(mode)
|
||||
activeFragments().filterIsInstance<BaseFragmentActivityExtensions>().forEach {
|
||||
activeFragments().filterIsInstance<FragmentActivityExtensions>().forEach {
|
||||
it.onActionModeStarted(mode, this)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActionModeFinished(mode: ActionMode) {
|
||||
super.onActionModeFinished(mode)
|
||||
activeFragments().filterIsInstance<BaseFragmentActivityExtensions>().forEach {
|
||||
activeFragments().filterIsInstance<FragmentActivityExtensions>().forEach {
|
||||
it.onActionModeFinished(mode, this)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
activeFragments().filterIsInstance<BaseFragmentActivityExtensions>().forEach {
|
||||
activeFragments().filterIsInstance<FragmentActivityExtensions>().forEach {
|
||||
it.onNewIntent(intent, this)
|
||||
}
|
||||
}
|
||||
@ -120,11 +120,11 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
|
||||
closeNavigationDrawer()
|
||||
return
|
||||
}
|
||||
if (activeFragments().filterIsInstance<BaseFragmentActivityExtensions>().isEmpty()) {
|
||||
if (activeFragments().filterIsInstance<FragmentActivityExtensions>().isEmpty()) {
|
||||
return super.onBackPressed()
|
||||
}
|
||||
activeFragments().filterIsInstance<BaseFragmentActivityExtensions>().forEach {
|
||||
if (it.onBackPressed(this) == BaseFragmentActivityExtensions.Super.ShouldCall) {
|
||||
activeFragments().filterIsInstance<FragmentActivityExtensions>().forEach {
|
||||
if (it.onBackPressed(this) == FragmentActivityExtensions.Super.ShouldCall) {
|
||||
super.onBackPressed()
|
||||
}
|
||||
}
|
||||
@ -140,7 +140,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
|
||||
navController.navigate(action)
|
||||
}
|
||||
|
||||
private fun navigate(fragmentId: Int) {
|
||||
fun navigate(fragmentId: Int) {
|
||||
navController.navigate(fragmentId)
|
||||
}
|
||||
|
||||
|
@ -99,14 +99,13 @@ import org.kiwix.kiwixmobile.core.R;
|
||||
import org.kiwix.kiwixmobile.core.R2;
|
||||
import org.kiwix.kiwixmobile.core.StorageObserver;
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragment;
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions;
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions;
|
||||
import org.kiwix.kiwixmobile.core.dao.NewBookDao;
|
||||
import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao;
|
||||
import org.kiwix.kiwixmobile.core.dao.entities.BookOnDiskEntity;
|
||||
import org.kiwix.kiwixmobile.core.extensions.ContextExtensionsKt;
|
||||
import org.kiwix.kiwixmobile.core.extensions.ViewExtensionsKt;
|
||||
import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions;
|
||||
import org.kiwix.kiwixmobile.core.page.bookmark.BookmarksFragment;
|
||||
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem;
|
||||
import org.kiwix.kiwixmobile.core.reader.ZimFileReader;
|
||||
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer;
|
||||
@ -153,7 +152,7 @@ import static org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_KIWIX_M
|
||||
|
||||
public abstract class CoreReaderFragment extends BaseFragment
|
||||
implements WebViewCallback,
|
||||
MainMenu.MenuClickListener, BaseFragmentActivityExtensions, WebViewProvider {
|
||||
MainMenu.MenuClickListener, FragmentActivityExtensions, WebViewProvider {
|
||||
protected final List<KiwixWebView> webViewList = new ArrayList<>();
|
||||
private final BehaviorProcessor<String> webUrlsProcessor = BehaviorProcessor.create();
|
||||
|
||||
@ -1030,8 +1029,8 @@ public abstract class CoreReaderFragment extends BaseFragment
|
||||
@OnLongClick(R2.id.bottom_toolbar_bookmark)
|
||||
boolean goToBookmarks() {
|
||||
saveTabStates();
|
||||
Intent intentBookmarks = new Intent(getActivity(), BookmarksFragment.class);
|
||||
startActivityForResult(intentBookmarks, BOOKMARK_CHOSEN_REQUEST);
|
||||
CoreMainActivity parentActivity = (CoreMainActivity) requireActivity();
|
||||
parentActivity.navigate(parentActivity.getBookmarksFragmentResId());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ import kotlinx.android.synthetic.main.fragment_page.recycler_view
|
||||
import kotlinx.android.synthetic.main.layout_toolbar.toolbar
|
||||
import org.kiwix.kiwixmobile.core.R
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragment
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
|
||||
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
||||
import org.kiwix.kiwixmobile.core.page.adapter.OnItemClickListener
|
||||
import org.kiwix.kiwixmobile.core.page.adapter.Page
|
||||
@ -53,7 +53,7 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
||||
import org.kiwix.kiwixmobile.core.utils.SimpleTextListener
|
||||
import javax.inject.Inject
|
||||
|
||||
abstract class PageFragment : OnItemClickListener, BaseFragment(), BaseFragmentActivityExtensions {
|
||||
abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActivityExtensions {
|
||||
abstract val pageViewModel: PageViewModel<*, *>
|
||||
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
||||
@Inject lateinit var sharedPreferenceUtil: SharedPreferenceUtil
|
||||
|
@ -3,7 +3,7 @@ package org.kiwix.kiwixmobile.core.page.bookmark
|
||||
import org.kiwix.kiwixmobile.core.R
|
||||
import org.kiwix.kiwixmobile.core.base.BaseActivity
|
||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.coreActivityComponent
|
||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel
|
||||
import org.kiwix.kiwixmobile.core.extensions.viewModel
|
||||
import org.kiwix.kiwixmobile.core.page.PageFragment
|
||||
import org.kiwix.kiwixmobile.core.page.adapter.PageAdapter
|
||||
import org.kiwix.kiwixmobile.core.page.adapter.PageDelegate.PageItemDelegate
|
||||
@ -11,7 +11,7 @@ import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.BookmarkViewModel
|
||||
|
||||
class BookmarksFragment : PageFragment() {
|
||||
override val pageViewModel by lazy {
|
||||
requireActivity().viewModel<BookmarkViewModel>(
|
||||
viewModel<BookmarkViewModel>(
|
||||
viewModelFactory
|
||||
)
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package org.kiwix.kiwixmobile.core.page.history
|
||||
import org.kiwix.kiwixmobile.core.R
|
||||
import org.kiwix.kiwixmobile.core.base.BaseActivity
|
||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.coreActivityComponent
|
||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.viewModel
|
||||
import org.kiwix.kiwixmobile.core.extensions.viewModel
|
||||
import org.kiwix.kiwixmobile.core.page.PageFragment
|
||||
import org.kiwix.kiwixmobile.core.page.adapter.PageAdapter
|
||||
import org.kiwix.kiwixmobile.core.page.adapter.PageDelegate.HistoryDateDelegate
|
||||
@ -14,7 +14,7 @@ const val USER_CLEARED_HISTORY: String = "user_cleared_history"
|
||||
|
||||
class HistoryFragment : PageFragment() {
|
||||
override val pageViewModel by lazy {
|
||||
requireActivity().viewModel<HistoryViewModel>(
|
||||
viewModel<HistoryViewModel>(
|
||||
viewModelFactory
|
||||
)
|
||||
}
|
||||
|
@ -36,8 +36,8 @@ import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.drawerlayout.widget.DrawerLayout
|
||||
import org.kiwix.kiwixmobile.core.base.BaseActivity
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions.Super
|
||||
import org.kiwix.kiwixmobile.core.base.BaseFragmentActivityExtensions.Super.ShouldCall
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super
|
||||
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldCall
|
||||
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.start
|
||||
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
|
||||
import org.kiwix.kiwixmobile.core.main.CoreReaderFragment
|
||||
|
Loading…
x
Reference in New Issue
Block a user