From cf7c6343834291753f3ee850b3f91459fb33aa10 Mon Sep 17 00:00:00 2001 From: HissPirat Date: Thu, 20 Aug 2020 11:18:29 +0200 Subject: [PATCH] #2302 back-button now works from search --- .../core/search/viewmodel/SearchViewModel.kt | 4 +-- .../viewmodel/effects/FinishActivity.kt | 28 +++++++++++++++ .../search/viewmodel/SearchViewModelTest.kt | 4 +-- .../viewmodel/effects/FinishActivityTest.kt | 34 +++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/FinishActivity.kt create mode 100644 core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/FinishActivityTest.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModel.kt index 0058729b5..fb855a923 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModel.kt @@ -48,7 +48,7 @@ import org.kiwix.kiwixmobile.core.search.viewmodel.SearchOrigin.FromWebView import org.kiwix.kiwixmobile.core.search.viewmodel.State.NoResults import org.kiwix.kiwixmobile.core.search.viewmodel.State.Results import org.kiwix.kiwixmobile.core.search.viewmodel.effects.DeleteRecentSearch -import org.kiwix.kiwixmobile.core.search.viewmodel.effects.PopFragmentBackstack +import org.kiwix.kiwixmobile.core.search.viewmodel.effects.FinishActivity import org.kiwix.kiwixmobile.core.search.viewmodel.effects.OpenSearchItem import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ProcessActivityResult import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SaveSearchToRecents @@ -93,7 +93,7 @@ class SearchViewModel @Inject constructor( private fun actionMapper() = actions.map { when (it) { - ExitedSearch -> effects.offer(PopFragmentBackstack) + ExitedSearch -> effects.offer(FinishActivity) is OnItemClick -> saveSearchAndOpenItem(it.searchListItem, false) is OnOpenInNewTabClick -> saveSearchAndOpenItem(it.searchListItem, true) is OnItemLongClick -> showDeleteDialog(it) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/FinishActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/FinishActivity.kt new file mode 100644 index 000000000..50337866d --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/FinishActivity.kt @@ -0,0 +1,28 @@ +/* + * Kiwix Android + * Copyright (c) 2020 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.search.viewmodel.effects + +import androidx.appcompat.app.AppCompatActivity +import org.kiwix.kiwixmobile.core.base.SideEffect + +object FinishActivity : SideEffect { + override fun invokeWith(activity: AppCompatActivity) { + activity.finish() + } +} diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModelTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModelTest.kt index d3f055fa4..4ef81e2df 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModelTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/SearchViewModelTest.kt @@ -54,7 +54,7 @@ import org.kiwix.kiwixmobile.core.search.viewmodel.SearchOrigin.FromWebView import org.kiwix.kiwixmobile.core.search.viewmodel.State.NoResults import org.kiwix.kiwixmobile.core.search.viewmodel.State.Results import org.kiwix.kiwixmobile.core.search.viewmodel.effects.DeleteRecentSearch -import org.kiwix.kiwixmobile.core.search.viewmodel.effects.PopFragmentBackstack +import org.kiwix.kiwixmobile.core.search.viewmodel.effects.FinishActivity import org.kiwix.kiwixmobile.core.search.viewmodel.effects.OpenSearchItem import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ProcessActivityResult import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SaveSearchToRecents @@ -218,7 +218,7 @@ internal class SearchViewModelTest { inner class ActionMapping { @Test fun `ExitedSearch offers Finish`() { - actionResultsInEffects(ExitedSearch, PopFragmentBackstack) + actionResultsInEffects(ExitedSearch, FinishActivity) } @Test diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/FinishActivityTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/FinishActivityTest.kt new file mode 100644 index 000000000..25ef4ec11 --- /dev/null +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/search/viewmodel/effects/FinishActivityTest.kt @@ -0,0 +1,34 @@ +/* + * Kiwix Android + * Copyright (c) 2020 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.search.viewmodel.effects + +import io.mockk.mockk +import io.mockk.verify +import org.junit.jupiter.api.Test +import org.kiwix.kiwixmobile.core.main.CoreMainActivity + +internal class FinishActivityTest { + + @Test + fun `invoke with finishes activity`() { + val activity = mockk(relaxed = true) + FinishActivity.invokeWith(activity) + verify { activity.finish() } + } +}