Skip to content

Commit

Permalink
Moved openSearchItem, findInPage functionality to our `CoreReader…
Browse files Browse the repository at this point in the history
…Fragment` as we were using same code twice for these methods in app and custom module.
  • Loading branch information
MohitMaliDeveloper committed Oct 30, 2023
1 parent 20ba392 commit aa93445
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@ import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toFile
import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.Observer
import com.google.android.material.bottomnavigation.BottomNavigationView
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.cachedComponent
import org.kiwix.kiwixmobile.core.R.anim
import org.kiwix.kiwixmobile.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super
import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldCall
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.consumeObservable
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.observeNavigationResult
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setupDrawerToggle
import org.kiwix.kiwixmobile.core.extensions.coreMainActivity
import org.kiwix.kiwixmobile.core.extensions.isFileExist
Expand All @@ -54,16 +51,11 @@ import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.main.CoreReaderFragment
import org.kiwix.kiwixmobile.core.main.CoreWebViewClient
import org.kiwix.kiwixmobile.core.main.FIND_IN_PAGE_SEARCH_STRING
import org.kiwix.kiwixmobile.core.main.ToolbarScrollingKiwixWebView
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.TAG_CURRENT_FILE
import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED
import org.kiwix.kiwixmobile.core.utils.TAG_KIWIX
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
import org.kiwix.kiwixmobile.core.utils.titleToUrl
import org.kiwix.kiwixmobile.core.utils.urlSuffixToParsableUrl
import java.io.File

private const val HIDE_TAB_SWITCHER_DELAY: Long = 300
Expand All @@ -88,27 +80,6 @@ class KiwixReaderFragment : CoreReaderFragment() {
toolbar?.let(activity::setupDrawerToggle)
setFragmentContainerBottomMarginToSizeOfNavBar()
openPageInBookFromNavigationArguments()

requireActivity().observeNavigationResult<String>(
FIND_IN_PAGE_SEARCH_STRING,
viewLifecycleOwner,
Observer(::findInPage)
)
requireActivity().observeNavigationResult<SearchItemToOpen>(
TAG_FILE_SEARCHED,
viewLifecycleOwner,
Observer(::openSearchItem)
)
}

private fun openSearchItem(item: SearchItemToOpen) {
zimReaderContainer?.titleToUrl(item.pageTitle)?.let {
if (item.shouldOpenInNewTab) {
createNewTab()
}
loadUrlWithCurrentWebview(zimReaderContainer?.urlSuffixToParsableUrl(it))
}
requireActivity().consumeObservable<SearchItemToOpen>(TAG_FILE_SEARCHED)
}

private fun openPageInBookFromNavigationArguments() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import androidx.core.view.WindowInsetsControllerCompat
import androidx.core.widget.ContentLoadingProgressBar
import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -107,7 +108,9 @@ 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.downloader.fetch.DOWNLOAD_NOTIFICATION_TITLE
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.consumeObservable
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.hasNotificationPermission
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.observeNavigationResult
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.requestNotificationPermission
import org.kiwix.kiwixmobile.core.extensions.ViewGroupExtensions.findFirstTextView
import org.kiwix.kiwixmobile.core.extensions.isFileExist
Expand All @@ -131,6 +134,7 @@ import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudService.Companion.ACTION_P
import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudService.Companion.ACTION_STOP_TTS
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen
import org.kiwix.kiwixmobile.core.utils.AnimationUtils.rotate
import org.kiwix.kiwixmobile.core.utils.DimenUtils.getToolbarHeight
import org.kiwix.kiwixmobile.core.utils.ExternalLinkOpener
Expand All @@ -153,6 +157,8 @@ import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.deleteCachedFiles
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.readFile
import org.kiwix.kiwixmobile.core.utils.titleToUrl
import org.kiwix.kiwixmobile.core.utils.urlSuffixToParsableUrl
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -486,6 +492,16 @@ abstract class CoreReaderFragment :
readAloudService?.registerCallBack(this@CoreReaderFragment)
}
}
requireActivity().observeNavigationResult<String>(
FIND_IN_PAGE_SEARCH_STRING,
viewLifecycleOwner,
Observer(::findInPage)
)
requireActivity().observeNavigationResult<SearchItemToOpen>(
TAG_FILE_SEARCHED,
viewLifecycleOwner,
Observer(::openSearchItem)
)
}

private fun initTabCallback() {
Expand Down Expand Up @@ -1621,6 +1637,16 @@ abstract class CoreReaderFragment :
openSearch("", isOpenedFromTabView = false, isVoice)
}

private fun openSearchItem(item: SearchItemToOpen) {
zimReaderContainer?.titleToUrl(item.pageTitle)?.let {
if (item.shouldOpenInNewTab) {
createNewTab()

Check warning on line 1643 in core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreReaderFragment.kt#L1643

Added line #L1643 was not covered by tests
}
loadUrlWithCurrentWebview(zimReaderContainer?.urlSuffixToParsableUrl(it))
}
requireActivity().consumeObservable<SearchItemToOpen>(TAG_FILE_SEARCHED)
}

private fun handleIntentActions(intent: Intent) {
Log.d(TAG_KIWIX, "action" + requireActivity().intent?.action)
startIntentBasedOnAction(intent)
Expand Down Expand Up @@ -1760,7 +1786,7 @@ abstract class CoreReaderFragment :
}
}

protected fun findInPage(title: String?) {
private fun findInPage(title: String?) {
// if the search is localized trigger find in page UI.
compatCallback?.apply {
setActive()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,13 @@ import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import org.kiwix.kiwixmobile.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.observeNavigationResult
import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setupDrawerToggle
import org.kiwix.kiwixmobile.core.main.CoreReaderFragment
import org.kiwix.kiwixmobile.core.main.FIND_IN_PAGE_SEARCH_STRING
import org.kiwix.kiwixmobile.core.main.MainMenu
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen
import org.kiwix.kiwixmobile.core.utils.LanguageUtils
import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED
import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower
import org.kiwix.kiwixmobile.core.utils.titleToUrl
import org.kiwix.kiwixmobile.core.utils.urlSuffixToParsableUrl
import org.kiwix.kiwixmobile.custom.BuildConfig
import org.kiwix.kiwixmobile.custom.R
import org.kiwix.kiwixmobile.custom.customActivityComponent
Expand Down Expand Up @@ -77,26 +70,6 @@ class CustomReaderFragment : CoreReaderFragment() {
toolbar?.let { setupDrawerToggle(it) }
}
loadPageFromNavigationArguments()

requireActivity().observeNavigationResult<String>(
FIND_IN_PAGE_SEARCH_STRING,
viewLifecycleOwner,
Observer(::findInPage)
)
requireActivity().observeNavigationResult<SearchItemToOpen>(
TAG_FILE_SEARCHED,
viewLifecycleOwner,
Observer(::openSearchItem)
)
}
}

private fun openSearchItem(item: SearchItemToOpen) {
zimReaderContainer?.titleToUrl(item.pageTitle)?.apply {
if (item.shouldOpenInNewTab) {
createNewTab()
}
loadUrlWithCurrentWebview(zimReaderContainer?.urlSuffixToParsableUrl(this))
}
}

Expand Down

0 comments on commit aa93445

Please sign in to comment.