Merge pull request #2247 from kiwix/feature/Frans-Lukas/2241-settings-to-fragment

Feature/frans lukas/2241 settings to fragment
This commit is contained in:
Seán Mac Gillicuddy 2020-08-18 09:39:09 +01:00 committed by GitHub
commit e64417a49f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 127 additions and 134 deletions

View File

@ -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<KiwixSettingsActivity> activityTestRule =
new ActivityTestRule<>(KiwixSettingsActivity.class);
public ActivityTestRule<KiwixMainActivity> 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()));
}

View File

@ -137,12 +137,6 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".settings.KiwixSettingsActivity">
<intent-filter>
<action android:name="org.kiwix.kiwixmobile.core.settings.CoreSettingsActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".language.LanguageActivity" />
<activity
android:name=".local_file_transfer.LocalFileTransferActivity"

View File

@ -31,7 +31,7 @@ import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragment
import org.kiwix.kiwixmobile.nav.destination.library.OnlineLibraryFragment
import org.kiwix.kiwixmobile.nav.destination.reader.KiwixReaderFragment
import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity
import org.kiwix.kiwixmobile.settings.KiwixSettingsFragment
import org.kiwix.kiwixmobile.splash.KiwixSplashActivity
import org.kiwix.kiwixmobile.webserver.ZimHostActivity
import org.kiwix.kiwixmobile.webserver.ZimHostModule
@ -53,7 +53,7 @@ interface KiwixActivityComponent : CoreActivityComponent {
fun inject(deleteFiles: DeleteFiles)
fun inject(localFileTransferActivity: LocalFileTransferActivity)
fun inject(languageActivity: LanguageActivity)
fun inject(kiwixSettingsActivity: KiwixSettingsActivity)
fun inject(kiwixSettingsFragment: KiwixSettingsFragment)
fun inject(zimHostActivity: ZimHostActivity)
fun inject(introActivity: IntroActivity)
fun inject(kiwixSplashActivity: KiwixSplashActivity)

View File

@ -37,14 +37,11 @@ import kotlinx.android.synthetic.main.activity_kiwix_main.reader_drawer_nav_view
import org.kiwix.kiwixmobile.R
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
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.main.PAGE_URL_KEY
import org.kiwix.kiwixmobile.core.main.ZIM_FILE_URI_KEY
import org.kiwix.kiwixmobile.core.utils.REQUEST_PREFERENCES
import org.kiwix.kiwixmobile.kiwixActivityComponent
import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity
import org.kiwix.kiwixmobile.webserver.ZimHostActivity
class KiwixMainActivity : CoreMainActivity() {
@ -55,6 +52,7 @@ class KiwixMainActivity : CoreMainActivity() {
override val drawerContainerLayout: DrawerLayout by lazy { navigation_container }
override val drawerNavView: NavigationView by lazy { drawer_nav_view }
override val bookmarksFragmentResId: Int = R.id.bookmarksFragment
override val settingsFragmentResId: Int = R.id.kiwixSettingsFragment
override val historyFragmentResId: Int = R.id.historyFragment
override val topLevelDestinations =
setOf(R.id.navigation_downloads, R.id.navigation_library, R.id.navigation_reader)
@ -133,10 +131,6 @@ class KiwixMainActivity : CoreMainActivity() {
return true
}
override fun openSettingsActivity() {
startActivityForResult(intent<KiwixSettingsActivity>(), REQUEST_PREFERENCES)
}
override fun openPage(pageUrl: String, zimFilePath: String) {
navigate(
R.id.navigation_reader,

View File

@ -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)
}
}

View File

@ -61,4 +61,8 @@
android:id="@+id/historyFragment"
android:name="org.kiwix.kiwixmobile.core.page.history.HistoryFragment"
android:label="HistoryFragment" />
<fragment
android:id="@+id/kiwixSettingsFragment"
android:name="org.kiwix.kiwixmobile.settings.KiwixSettingsFragment"
android:label="KiwixSettingsFragment" />
</navigation>

View File

@ -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)
}

View File

@ -48,11 +48,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 cachedComponent: CoreActivityComponent
abstract val topLevelDestinations: Set<Int>
@ -123,7 +124,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<HelpActivity>()
R.id.menu_history -> openHistoryActivity()
R.id.menu_bookmarks_list -> openBookmarksActivity()
@ -187,7 +188,10 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
navController.navigate(fragmentId, bundle)
}
abstract fun openSettingsActivity()
fun openSettings() {
handleDrawerOnNavigation()
navigate(settingsFragmentResId)
}
private fun openHistoryActivity() {
navigate(historyFragmentResId)

View File

@ -96,13 +96,13 @@ abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActiv
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super<BaseFragment>.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<BaseFragment>.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {

View File

@ -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;
});

View File

@ -1,72 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 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.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());
}
}

View File

@ -0,0 +1,55 @@
/*
* Kiwix Android
* Copyright (c) 2019 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.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)
}
}

View File

@ -13,7 +13,14 @@
app:layout_constraintTop_toTopOf="parent"
tools:showIn="@layout/activity_help">
<include layout="@layout/layout_toolbar" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:popupTheme="@style/KiwixTheme"
app:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
tools:showIn="@layout/activity_search" />
<Switch
android:id="@+id/page_switch"

View File

@ -9,7 +9,7 @@
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/layout_standard_app_bar" />
<include layout="@layout/layout_toolbar" />
<FrameLayout
android:id="@+id/content_frame"

View File

@ -55,12 +55,6 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".settings.CustomSettingsActivity">
<intent-filter>
<action android:name="org.kiwix.kiwixmobile.core.settings.CoreSettingsActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".download.CustomDownloadActivity" />
<receiver android:name=".main.CustomSearchWidget">

View File

@ -26,14 +26,14 @@ 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
import org.kiwix.kiwixmobile.custom.splash.CustomSplashActivity
@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(customSplashActivity: CustomSplashActivity)
fun inject(customReaderFragment: CustomReaderFragment)

View File

@ -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 cachedComponent by lazy { customActivityComponent }
override val topLevelDestinations =
@ -90,10 +88,6 @@ class CustomMainActivity : CoreMainActivity() {
.isVisible = false
}
override fun openSettingsActivity() {
startActivityForResult(intent<CustomSettingsActivity>(), 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)

View File

@ -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)
}
}

View File

@ -37,4 +37,8 @@
android:id="@+id/bookmarksFragment"
android:name="org.kiwix.kiwixmobile.core.page.bookmark.BookmarksFragment"
android:label="BookmarksFragment" />
<fragment
android:id="@+id/customSettingsFragment"
android:name="org.kiwix.kiwixmobile.custom.settings.CustomSettingsFragment"
android:label="CustomSettingsFragment" />
</navigation>