Merge pull request #2581 from s-ayush2903/fix/s-ayush2903/#2567-save-position-of-article-on-new-launches

fix: Restored position of articles on new launches
This commit is contained in:
Kelson 2021-01-19 09:11:30 +01:00 committed by GitHub
commit 57f32694a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 74 deletions

View File

@ -1,60 +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.search;
import android.Manifest;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.rule.GrantPermissionRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kiwix.kiwixmobile.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.matcher.ViewMatchers.withEffectiveVisibility;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.allOf;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class SearchFragmentTest {
@Rule
public ActivityTestRule<KiwixMainActivity> mActivityTestRule = 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);
@Test
public void SearchFragmentSimple() {
onView(allOf(withText(R.string.reader),
withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))).perform(click());
onView(withId(R.id.menu_search));
}
}

View File

@ -0,0 +1,35 @@
/*
* 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.search
import androidx.test.internal.runner.junit4.statement.UiThreadStatement
import com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertDisplayed
import org.junit.Before
import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.R
class SearchFragmentTest : BaseActivityTest() {
@Before fun setUp() {
UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.searchFragment) }
}
@Test fun searchFragmentSimple() {
assertDisplayed(R.string.menu_search_in_text)
}
}

View File

@ -38,7 +38,7 @@ import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions
import org.kiwix.kiwixmobile.core.di.components.CoreComponent import org.kiwix.kiwixmobile.core.di.components.CoreComponent
import org.kiwix.kiwixmobile.core.main.CoreMainActivity import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.kiwixActivityComponent import org.kiwix.kiwixmobile.kiwixActivityComponent
import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragmentDirections import org.kiwix.kiwixmobile.nav.destination.reader.KiwixReaderFragmentDirections
const val NAVIGATE_TO_ZIM_HOST_FRAGMENT = "navigate_to_zim_host_fragment" const val NAVIGATE_TO_ZIM_HOST_FRAGMENT = "navigate_to_zim_host_fragment"
@ -95,7 +95,7 @@ class KiwixMainActivity : CoreMainActivity() {
} }
} }
if (sharedPreferenceUtil.showIntro()) { if (sharedPreferenceUtil.showIntro()) {
navigate(LocalLibraryFragmentDirections.actionLibraryFragmentToIntrofragment()) navigate(KiwixReaderFragmentDirections.actionReaderFragmentToIntroFragment())
} }
} }

View File

@ -111,6 +111,7 @@ class KiwixReaderFragment : CoreReaderFragment() {
private fun openPageInBookFromNavigationArguments() { private fun openPageInBookFromNavigationArguments() {
val args = KiwixReaderFragmentArgs.fromBundle(requireArguments()) val args = KiwixReaderFragmentArgs.fromBundle(requireArguments())
if (args.pageUrl.isNotEmpty()) { if (args.pageUrl.isNotEmpty()) {
if (args.zimFileUri.isNotEmpty()) { if (args.zimFileUri.isNotEmpty()) {
tryOpeningZimFile(args.zimFileUri) tryOpeningZimFile(args.zimFileUri)
@ -250,9 +251,13 @@ class KiwixReaderFragment : CoreReaderFragment() {
val settings = requireActivity().getSharedPreferences(SharedPreferenceUtil.PREF_KIWIX_MOBILE, 0) val settings = requireActivity().getSharedPreferences(SharedPreferenceUtil.PREF_KIWIX_MOBILE, 0)
val zimFile = settings.getString(TAG_CURRENT_FILE, null) val zimFile = settings.getString(TAG_CURRENT_FILE, null)
if (zimFile != null) { if (zimFile != null && File(zimFile).exists()) {
if (zimReaderContainer.zimFile == null) { if (zimReaderContainer.zimFile == null) {
openZimFile(File(zimFile)) openZimFile(File(zimFile))
Log.d(
TAG_KIWIX,
"Kiwix normal start, Opened last used zimFile: -> $zimFile"
)
} }
} else { } else {
getCurrentWebView().snack(R.string.zim_not_opened) getCurrentWebView().snack(R.string.zim_not_opened)

View File

@ -19,7 +19,7 @@
<navigation xmlns:android="http://schemas.android.com/apk/res/android" <navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/libraryFragment"> app:startDestination="@id/readerFragment">
<fragment <fragment
android:id="@+id/readerFragment" android:id="@+id/readerFragment"
android:name="org.kiwix.kiwixmobile.nav.destination.reader.KiwixReaderFragment" android:name="org.kiwix.kiwixmobile.nav.destination.reader.KiwixReaderFragment"
@ -52,6 +52,11 @@
<action <action
android:id="@+id/action_readerFragment_to_searchFragment" android:id="@+id/action_readerFragment_to_searchFragment"
app:destination="@id/searchFragment" /> app:destination="@id/searchFragment" />
<action
android:id="@+id/action_readerFragment_to_introFragment"
app:destination="@id/introFragment"
app:popUpTo="@id/readerFragment"
app:popUpToInclusive="true" />
</fragment> </fragment>
<fragment <fragment
@ -69,11 +74,6 @@
<action <action
android:id="@+id/action_libraryFragment_to_localFileTransferFragment" android:id="@+id/action_libraryFragment_to_localFileTransferFragment"
app:destination="@id/localFileTransferFragment" /> app:destination="@id/localFileTransferFragment" />
<action
android:id="@+id/action_libraryFragment_to_introfragment"
app:destination="@id/introFragment"
app:popUpTo="@id/libraryFragment"
app:popUpToInclusive="true" />
</fragment> </fragment>
<fragment <fragment

View File

@ -1574,19 +1574,26 @@ public abstract class CoreReaderFragment extends BaseFragment
return Math.max(settings.getInt(TAG_CURRENT_TAB, 0), 0); return Math.max(settings.getInt(TAG_CURRENT_TAB, 0), 0);
} }
/* This method restores tabs state in new launches, do not modify it
unless it is explicitly mentioned in the issue you're fixing */
protected void restoreTabs(@Nullable String zimArticles, @Nullable String zimPositions, protected void restoreTabs(@Nullable String zimArticles, @Nullable String zimPositions,
int currentTab) { int currentTab) {
try { try {
JSONArray urls = new JSONArray(zimArticles); JSONArray urls = new JSONArray(zimArticles);
JSONArray positions = new JSONArray(zimPositions); JSONArray positions = new JSONArray(zimPositions);
webViewList.clear(); currentWebViewIndex = 0;
currentWebViewIndex=0;
tabsAdapter.notifyItemRemoved(0); tabsAdapter.notifyItemRemoved(0);
tabsAdapter.notifyDataSetChanged(); tabsAdapter.notifyDataSetChanged();
for (int i = 0; i < urls.length(); i++) { int cursor = 0;
newTab(UpdateUtils.reformatProviderUrl(urls.getString(i)), i == currentTab) getCurrentWebView().loadUrl(UpdateUtils.reformatProviderUrl(urls.getString(cursor)));
.setScrollY(positions.getInt(i)); getCurrentWebView().setScrollY(positions.getInt(cursor));
cursor++;
while (cursor < urls.length()) {
newTab(UpdateUtils.reformatProviderUrl(urls.getString(cursor)));
getCurrentWebView().setScrollY(positions.getInt(cursor));
cursor++;
} }
selectTab(currentTab);
} catch (JSONException e) { } catch (JSONException e) {
Log.w(TAG_KIWIX, "Kiwix shared preferences corrupted", e); Log.w(TAG_KIWIX, "Kiwix shared preferences corrupted", e);
ContextExtensionsKt.toast(getActivity(), "Could not restore tabs.", Toast.LENGTH_LONG); ContextExtensionsKt.toast(getActivity(), "Could not restore tabs.", Toast.LENGTH_LONG);