diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/settings/KiwixSettingsActivityTest.java b/app/src/androidTest/java/org/kiwix/kiwixmobile/settings/KiwixSettingsFragmentTest.java similarity index 76% rename from app/src/androidTest/java/org/kiwix/kiwixmobile/settings/KiwixSettingsActivityTest.java rename to app/src/androidTest/java/org/kiwix/kiwixmobile/settings/KiwixSettingsFragmentTest.java index 6ba4c18a4..c7879b0a8 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/settings/KiwixSettingsActivityTest.java +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/settings/KiwixSettingsFragmentTest.java @@ -18,30 +18,46 @@ package org.kiwix.kiwixmobile.settings; +import android.Manifest; import android.view.View; import androidx.annotation.StringRes; -import androidx.recyclerview.widget.RecyclerView; import androidx.test.rule.ActivityTestRule; +import androidx.test.rule.GrantPermissionRule; import org.hamcrest.Matcher; import org.jetbrains.annotations.NotNull; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.kiwix.kiwixmobile.core.R; +import org.kiwix.kiwixmobile.main.KiwixMainActivity; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.contrib.RecyclerViewActions.actionOnItem; import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant; -import static androidx.test.espresso.matcher.ViewMatchers.withClassName; +import static androidx.test.espresso.matcher.ViewMatchers.withId; import static androidx.test.espresso.matcher.ViewMatchers.withText; import static com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertDisplayed; import static org.hamcrest.Matchers.anyOf; -import static org.hamcrest.Matchers.is; +import static org.kiwix.kiwixmobile.utils.StandardActions.enterSettings; +import static org.kiwix.kiwixmobile.utils.StandardActions.openDrawer; -public class KiwixSettingsActivityTest { +public class KiwixSettingsFragmentTest { @Rule - public ActivityTestRule activityTestRule = - new ActivityTestRule<>(KiwixSettingsActivity.class); + public ActivityTestRule activityTestRule = + new ActivityTestRule<>(KiwixMainActivity.class); + @Rule + public GrantPermissionRule readPermissionRule = + GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE); + @Rule + public GrantPermissionRule writePermissionRule = + GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE); + + @Before + public void setup() { + openDrawer(); + enterSettings(); + } @Test public void testToggle() { @@ -56,7 +72,7 @@ public class KiwixSettingsActivityTest { for (int i = 0; i < stringIds.length; i++) { matchers[i] = withText(stringIds[i]); } - onView(withClassName(is(RecyclerView.class.getName()))) + onView(withId(R.id.recycler_view)) .perform(actionOnItem(hasDescendant(anyOf(matchers)), click())); } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4721b68d1..0a06b1214 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -132,12 +132,6 @@ - - - - - - (), REQUEST_PREFERENCES) - } - override fun openPage(pageUrl: String, zimFilePath: String) { navigate( R.id.readerFragment, diff --git a/app/src/main/java/org/kiwix/kiwixmobile/settings/KiwixSettingsActivity.kt b/app/src/main/java/org/kiwix/kiwixmobile/settings/KiwixSettingsFragment.kt similarity index 70% rename from app/src/main/java/org/kiwix/kiwixmobile/settings/KiwixSettingsActivity.kt rename to app/src/main/java/org/kiwix/kiwixmobile/settings/KiwixSettingsFragment.kt index 1230c3d89..2decb31e7 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/settings/KiwixSettingsActivity.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/settings/KiwixSettingsFragment.kt @@ -18,15 +18,15 @@ package org.kiwix.kiwixmobile.settings -import org.kiwix.kiwixmobile.core.di.components.CoreComponent -import org.kiwix.kiwixmobile.core.settings.CoreSettingsActivity -import org.kiwix.kiwixmobile.kiwixActivityComponent +import org.kiwix.kiwixmobile.core.base.BaseActivity +import org.kiwix.kiwixmobile.core.settings.CoreSettingsFragment +import org.kiwix.kiwixmobile.main.KiwixMainActivity -class KiwixSettingsActivity : CoreSettingsActivity() { - - override fun injection(coreComponent: CoreComponent) { - kiwixActivityComponent.inject(this) - } +class KiwixSettingsFragment : CoreSettingsFragment() { override fun createPreferenceFragment() = KiwixPrefsFragment() + + override fun inject(baseActivity: BaseActivity) { + (baseActivity as KiwixMainActivity).cachedComponent.inject(this) + } } diff --git a/app/src/main/res/navigation/kiwix_nav_graph.xml b/app/src/main/res/navigation/kiwix_nav_graph.xml index 82cf0a738..bf81513e4 100644 --- a/app/src/main/res/navigation/kiwix_nav_graph.xml +++ b/app/src/main/res/navigation/kiwix_nav_graph.xml @@ -65,4 +65,8 @@ android:id="@+id/historyFragment" android:name="org.kiwix.kiwixmobile.core.page.history.HistoryFragment" android:label="HistoryFragment" /> + diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt index 6064e06b1..32dbbe1bc 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt @@ -50,7 +50,7 @@ import org.kiwix.kiwixmobile.core.reader.ZimFileReader import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.search.SearchActivity import org.kiwix.kiwixmobile.core.search.viewmodel.SearchResultGenerator -import org.kiwix.kiwixmobile.core.settings.CoreSettingsActivity +import org.kiwix.kiwixmobile.core.settings.CoreSettingsFragment import org.kiwix.kiwixmobile.core.utils.BookUtils import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import javax.inject.Singleton @@ -107,5 +107,5 @@ interface CoreComponent { fun inject(searchActivity: SearchActivity) fun inject(helpActivity: HelpActivity) - fun inject(settingsActivity: CoreSettingsActivity) + fun inject(settingsFragment: CoreSettingsFragment) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt index e4938642f..a94ca3e44 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt @@ -53,11 +53,12 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { @Inject lateinit var externalLinkOpener: ExternalLinkOpener protected lateinit var drawerToggle: ActionBarDrawerToggle - abstract val navController: NavController + abstract val drawerContainerLayout: DrawerLayout abstract val drawerNavView: NavigationView abstract val bookmarksFragmentResId: Int + abstract val settingsFragmentResId: Int abstract val historyFragmentResId: Int abstract val initialDestinationFragmentId: Int abstract val cachedComponent: CoreActivityComponent @@ -148,7 +149,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { open fun onNavigationItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_support_kiwix -> openSupportKiwixExternalLink() - R.id.menu_settings -> openSettingsActivity() + R.id.menu_settings -> openSettings() R.id.menu_help -> start() R.id.menu_history -> openHistoryActivity() R.id.menu_bookmarks_list -> openBookmarksActivity() @@ -212,7 +213,10 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { navController.navigate(fragmentId, bundle) } - abstract fun openSettingsActivity() + fun openSettings() { + handleDrawerOnNavigation() + navigate(settingsFragmentResId) + } private fun openHistoryActivity() { navigate(historyFragmentResId) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt index a2c33a8b0..2cbfe54d7 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt @@ -96,13 +96,13 @@ abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActiv } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + super.onCreateOptionsMenu(menu, inflater) inflater.inflate(R.menu.menu_page, menu) val search = menu.findItem(R.id.menu_page_search).actionView as SearchView search.queryHint = searchQueryHint search.setOnQueryTextListener(SimpleTextListener { pageViewModel.actions.offer(Action.Filter(it)) }) - super.onCreateOptionsMenu(menu, inflater) } override fun onOptionsItemSelected(item: MenuItem): Boolean { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/settings/CorePrefsFragment.java b/core/src/main/java/org/kiwix/kiwixmobile/core/settings/CorePrefsFragment.java index 2beb09112..17885332a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/settings/CorePrefsFragment.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/settings/CorePrefsFragment.java @@ -203,7 +203,6 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme private void clearAllHistoryDialog() { alertDialogShower.show(KiwixDialog.ClearAllHistory.INSTANCE, () -> { presenter.clearHistory(); - CoreSettingsActivity.allHistoryCleared = true; Snackbar.make(getView(), R.string.all_history_cleared, Snackbar.LENGTH_SHORT).show(); return Unit.INSTANCE; }); diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/settings/CoreSettingsActivity.java b/core/src/main/java/org/kiwix/kiwixmobile/core/settings/CoreSettingsActivity.java deleted file mode 100644 index 6f24aa55d..000000000 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/settings/CoreSettingsActivity.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 Kiwix - * 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 . - * - */ - -package org.kiwix.kiwixmobile.core.settings; - -import android.content.Intent; -import android.os.Bundle; -import androidx.appcompat.widget.Toolbar; -import androidx.fragment.app.Fragment; -import org.kiwix.kiwixmobile.core.R; -import org.kiwix.kiwixmobile.core.base.BaseActivity; - -import static org.kiwix.kiwixmobile.core.utils.ConstantsKt.EXTRA_WEBVIEWS_LIST; -import static org.kiwix.kiwixmobile.core.utils.ConstantsKt.RESULT_HISTORY_CLEARED; - -public abstract class CoreSettingsActivity extends BaseActivity { - - public static boolean allHistoryCleared = false; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.settings); - - allHistoryCleared = false; - - getSupportFragmentManager() - .beginTransaction(). - replace(R.id.content_frame, createPreferenceFragment()) - .commit(); - - setUpToolbar(); - } - - protected abstract Fragment createPreferenceFragment(); - - @Override - public void onBackPressed() { - getWindow().setWindowAnimations(0); - if (allHistoryCleared) { - Intent data = new Intent(); - data.putExtra(EXTRA_WEBVIEWS_LIST, allHistoryCleared); - setResult(RESULT_HISTORY_CLEARED, data); - } - super.onBackPressed(); - } - - private void setUpToolbar() { - Toolbar toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - getSupportActionBar().setTitle(getString(R.string.menu_settings)); - getSupportActionBar().setHomeButtonEnabled(true); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - - toolbar.setNavigationOnClickListener(v -> onBackPressed()); - } -} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/settings/CoreSettingsFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/settings/CoreSettingsFragment.kt new file mode 100644 index 000000000..c3faaf935 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/settings/CoreSettingsFragment.kt @@ -0,0 +1,55 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * 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 . + * + */ +package org.kiwix.kiwixmobile.core.settings + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment +import kotlinx.android.synthetic.main.layout_toolbar.toolbar +import org.kiwix.kiwixmobile.core.R +import org.kiwix.kiwixmobile.core.base.BaseFragment + +abstract class CoreSettingsFragment : BaseFragment() { + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + requireActivity().supportFragmentManager + .beginTransaction().replace(R.id.content_frame, createPreferenceFragment()) + .commit() + setUpToolbar() + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View = inflater.inflate(R.layout.settings, container, false) + + protected abstract fun createPreferenceFragment(): Fragment + + private fun setUpToolbar() { + val activity = requireActivity() as AppCompatActivity + activity.setSupportActionBar(toolbar) + activity.supportActionBar!!.title = getString(R.string.menu_settings) + activity.supportActionBar!!.setHomeButtonEnabled(true) + activity.supportActionBar!!.setDisplayHomeAsUpEnabled(true) + } +} diff --git a/core/src/main/res/layout/fragment_page.xml b/core/src/main/res/layout/fragment_page.xml index 63f4b6737..6fd61ba68 100644 --- a/core/src/main/res/layout/fragment_page.xml +++ b/core/src/main/res/layout/fragment_page.xml @@ -13,7 +13,14 @@ app:layout_constraintTop_toTopOf="parent" tools:showIn="@layout/activity_help"> - + + - + - - - - - - diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/di/CustomActivityComponent.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/di/CustomActivityComponent.kt index a56436d8e..56e494bf5 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/di/CustomActivityComponent.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/di/CustomActivityComponent.kt @@ -26,13 +26,13 @@ import org.kiwix.kiwixmobile.core.di.components.CoreActivityComponent import org.kiwix.kiwixmobile.custom.download.CustomDownloadActivity import org.kiwix.kiwixmobile.custom.main.CustomMainActivity import org.kiwix.kiwixmobile.custom.main.CustomReaderFragment -import org.kiwix.kiwixmobile.custom.settings.CustomSettingsActivity +import org.kiwix.kiwixmobile.custom.settings.CustomSettingsFragment @ActivityScope @Subcomponent(modules = [CustomActivityModule::class]) interface CustomActivityComponent : CoreActivityComponent { fun inject(customMainActivity: CustomMainActivity) - fun inject(customSettingsActivity: CustomSettingsActivity) + fun inject(customSettingsFragment: CustomSettingsFragment) fun inject(customDownloadActivity: CustomDownloadActivity) fun inject(customReaderFragment: CustomReaderFragment) diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt index 323c5e769..50e306ade 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt @@ -29,13 +29,10 @@ import com.google.android.material.navigation.NavigationView import kotlinx.android.synthetic.main.activity_main.custom_drawer_container import kotlinx.android.synthetic.main.activity_main.drawer_nav_view import org.kiwix.kiwixmobile.core.di.components.CoreComponent -import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.intent import org.kiwix.kiwixmobile.core.main.CoreMainActivity import org.kiwix.kiwixmobile.core.main.ZIM_FILE_URI_KEY -import org.kiwix.kiwixmobile.core.utils.REQUEST_PREFERENCES import org.kiwix.kiwixmobile.custom.R import org.kiwix.kiwixmobile.custom.customActivityComponent -import org.kiwix.kiwixmobile.custom.settings.CustomSettingsActivity const val REQUEST_READ_FOR_OBB = 5002 @@ -47,6 +44,7 @@ class CustomMainActivity : CoreMainActivity() { override val drawerContainerLayout: DrawerLayout by lazy { custom_drawer_container } override val drawerNavView: NavigationView by lazy { drawer_nav_view } override val bookmarksFragmentResId: Int = R.id.bookmarksFragment + override val settingsFragmentResId: Int = R.id.customSettingsFragment override val historyFragmentResId: Int = R.id.historyFragment override val initialDestinationFragmentId: Int = R.id.customReaderFragment override val cachedComponent by lazy { customActivityComponent } @@ -92,10 +90,6 @@ class CustomMainActivity : CoreMainActivity() { .isVisible = false } - override fun openSettingsActivity() { - startActivityForResult(intent(), REQUEST_PREFERENCES) - } - override fun openPage(pageUrl: String, zimFilePath: String) { val bundle = bundleOf(PAGE_URL_KEY to pageUrl, ZIM_FILE_URI_KEY to zimFilePath) navigate(R.id.customReaderFragment, bundle) diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/settings/CustomSettingsActivity.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/settings/CustomSettingsFragment.kt similarity index 70% rename from custom/src/main/java/org/kiwix/kiwixmobile/custom/settings/CustomSettingsActivity.kt rename to custom/src/main/java/org/kiwix/kiwixmobile/custom/settings/CustomSettingsFragment.kt index 0e0017a17..88fac0d6a 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/settings/CustomSettingsActivity.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/settings/CustomSettingsFragment.kt @@ -18,15 +18,15 @@ package org.kiwix.kiwixmobile.custom.settings -import org.kiwix.kiwixmobile.core.di.components.CoreComponent -import org.kiwix.kiwixmobile.core.settings.CoreSettingsActivity -import org.kiwix.kiwixmobile.custom.customActivityComponent +import org.kiwix.kiwixmobile.core.base.BaseActivity +import org.kiwix.kiwixmobile.core.settings.CoreSettingsFragment +import org.kiwix.kiwixmobile.custom.main.CustomMainActivity -class CustomSettingsActivity : CoreSettingsActivity() { - - override fun injection(coreComponent: CoreComponent) { - customActivityComponent.inject(this) - } +class CustomSettingsFragment : CoreSettingsFragment() { override fun createPreferenceFragment() = CustomPrefsFragment() + + override fun inject(baseActivity: BaseActivity) { + (baseActivity as CustomMainActivity).cachedComponent.inject(this) + } } diff --git a/custom/src/main/res/navigation/custom_nav_graph.xml b/custom/src/main/res/navigation/custom_nav_graph.xml index 5ec03b14e..44dc7c5c0 100644 --- a/custom/src/main/res/navigation/custom_nav_graph.xml +++ b/custom/src/main/res/navigation/custom_nav_graph.xml @@ -37,4 +37,8 @@ android:id="@+id/bookmarksFragment" android:name="org.kiwix.kiwixmobile.core.page.bookmark.BookmarksFragment" android:label="BookmarksFragment" /> +