Skip to content

Commit

Permalink
Naming conventions improved and some reviewed changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Saifuddin53 committed Oct 18, 2024
1 parent c67ed75 commit 9f6b55d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract class KiwixRoomDatabase : RoomDatabase() {
abstract fun historyRoomDao(): HistoryRoomDao
abstract fun notesRoomDao(): NotesRoomDao
abstract fun downloadRoomDao(): DownloadRoomDao
abstract fun pageHistoryRoomDao(): WebViewHistoryRoomDao
abstract fun webViewHistoryRoomDao(): WebViewHistoryRoomDao

companion object {
private var db: KiwixRoomDatabase? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ interface CoreComponent {
fun libkiwixBookmarks(): LibkiwixBookmarks
fun recentSearchRoomDao(): RecentSearchRoomDao
fun historyRoomDao(): HistoryRoomDao
fun pageHistoryRoomDao(): WebViewHistoryRoomDao
fun webViewHistoryRoomDao(): WebViewHistoryRoomDao
fun noteRoomDao(): NotesRoomDao
fun objectBoxToRoomMigrator(): ObjectBoxToRoomMigrator
fun context(): Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ open class DatabaseModule {

@Provides
@Singleton
fun providePageHistoryRoomDao(db: KiwixRoomDatabase) = db.pageHistoryRoomDao()
fun provideWebViewHistoryRoomDao(db: KiwixRoomDatabase) = db.webViewHistoryRoomDao()

@Singleton
@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ import org.kiwix.kiwixmobile.core.navigateToAppSettings
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem
import org.kiwix.kiwixmobile.core.page.history.NavigationHistoryClickListener
import org.kiwix.kiwixmobile.core.page.history.NavigationHistoryDialog
import org.kiwix.kiwixmobile.core.page.history.adapter.DataCallback
import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryCallback
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
import org.kiwix.kiwixmobile.core.page.history.adapter.NavigationHistoryListItem
import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryItem
Expand Down Expand Up @@ -185,7 +185,7 @@ abstract class CoreReaderFragment :
ReadAloudCallbacks,
NavigationHistoryClickListener,
ShowDonationDialogCallback,
DataCallback {
WebViewHistoryCallback {
protected val webViewList: MutableList<KiwixWebView> = ArrayList()
private val webUrlsProcessor = BehaviorProcessor.create<String>()
private var fragmentReaderBinding: FragmentReaderBinding? = null
Expand Down Expand Up @@ -974,7 +974,7 @@ abstract class CoreReaderFragment :

override fun clearHistory() {
getCurrentWebView()?.clearHistory()
repositoryActions?.clearWebViewPagesHistory()
repositoryActions?.clearWebViewPageHistory()
updateBottomToolbarArrowsAlpha()
toast(R.string.navigation_history_cleared)
}
Expand Down Expand Up @@ -1873,7 +1873,7 @@ abstract class CoreReaderFragment :

// If backStack and forwardStack are empty, return
if (backStack.isEmpty() && forwardStack.isEmpty() || currentZimId != pageHistory[0].zimId) {
repositoryActions?.clearWebViewPagesHistory()
repositoryActions?.clearWebViewPageHistory()
return
}

Expand Down Expand Up @@ -1927,7 +1927,7 @@ abstract class CoreReaderFragment :
}

override fun onError(error: Throwable) {
activity.toast(R.string.could_not_restore_tabs, Toast.LENGTH_LONG)
activity.toast(R.string.could_not_restore_web_view_history, Toast.LENGTH_LONG)
}

override fun onResume() {
Expand Down Expand Up @@ -2297,47 +2297,55 @@ abstract class CoreReaderFragment :
editor.apply()
}

private fun saveWebViewHistoryItems(
startIndex: Int,
endIndex: Int,
zimId: String,
isForward: Boolean,
historyList: WebBackForwardList
) {
for (index in startIndex until endIndex) {
val historyItem = historyList.getItemAtIndex(index)
val pageHistory = WebViewHistoryItem(
zimId = zimId,
title = historyItem.title,
pageUrl = historyItem.url,
isForward = isForward,
timeStamp = index.toLong()
)
repositoryActions?.saveWebViewPageHistory(pageHistory)
}
}

private fun saveWebBackForwardListToRoom() {
val webBackForwardList = getCurrentWebView()?.copyBackForwardList()
val currentIndex = webBackForwardList?.currentIndex
val zimId = zimReaderContainer?.zimFileReader?.id ?: ""

if (currentIndex != null) {
// Save BackStack
webBackForwardList.let { historyList ->
for (index in 0 until historyList.currentIndex) {
val historyItem = historyList.getItemAtIndex(index)
val pageHistory = WebViewHistoryItem(
zimId = zimId,
title = historyItem.title,
pageUrl = historyItem.url,
isForward = false,
timeStamp = index.toLong()
)
repositoryActions?.saveWebViewPageHistory(pageHistory)
}
}
saveWebViewHistoryItems(
0,
webBackForwardList.currentIndex,
zimId,
false,
webBackForwardList
)

// Save ForwardStack
webBackForwardList.let { historyList ->
for (index in historyList.currentIndex + 1 until historyList.size) {
val historyItem = historyList.getItemAtIndex(index)
val pageHistory = WebViewHistoryItem(
zimId = zimId,
title = historyItem.title,
pageUrl = historyItem.url,
isForward = true,
timeStamp = index.toLong()
)
repositoryActions?.saveWebViewPageHistory(pageHistory)
}
}
saveWebViewHistoryItems(
currentIndex + 1,
webBackForwardList.size,
zimId,
true,
webBackForwardList
)
}
}

override fun onPause() {
super.onPause()
repositoryActions?.clearWebViewPagesHistory()
repositoryActions?.clearWebViewPageHistory()
saveWebBackForwardListToRoom()
saveTabStates()
Log.d(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import io.reactivex.disposables.Disposable
import org.kiwix.kiwixmobile.core.data.DataSource
import org.kiwix.kiwixmobile.core.di.ActivityScope
import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem
import org.kiwix.kiwixmobile.core.page.history.adapter.DataCallback
import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryCallback
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem
import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryItem
import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem
Expand All @@ -38,9 +38,9 @@ class MainRepositoryActions @Inject constructor(private val dataSource: DataSour
private var saveNoteDisposable: Disposable? = null
private var saveBookDisposable: Disposable? = null
private var deleteNoteDisposable: Disposable? = null
private var savePageHistoryDisposable: Disposable? = null
private var clearPageHistoryDisposable: Disposable? = null
private var getPageHistoryDisposable: Disposable? = null
private var saveWebViewHistoryDisposable: Disposable? = null
private var clearWebViewHistoryDisposable: Disposable? = null
private var getWebViewHistoryDisposable: Disposable? = null

fun saveHistory(history: HistoryItem) {
saveHistoryDisposable = dataSource.saveHistory(history)
Expand Down Expand Up @@ -74,17 +74,17 @@ class MainRepositoryActions @Inject constructor(private val dataSource: DataSour
}

fun saveWebViewPageHistory(pageHistory: WebViewHistoryItem) {
savePageHistoryDisposable = dataSource.insertWebViewHistoryItem(pageHistory)
saveWebViewHistoryDisposable = dataSource.insertWebViewHistoryItem(pageHistory)
.subscribe({}, { e -> Log.e(TAG, "Unable to save page history", e) })
}

fun clearWebViewPagesHistory() {
clearPageHistoryDisposable = dataSource.clearWebViewPagesHistory()
fun clearWebViewPageHistory() {
clearWebViewHistoryDisposable = dataSource.clearWebViewPagesHistory()
.subscribe({}, { e -> Log.e(TAG, "Unable to clear page history", e) })
}

fun loadWebViewPagesHistory(callBack: DataCallback) {
getPageHistoryDisposable = dataSource.getAllWebViewPagesHistory()
fun loadWebViewPagesHistory(callBack: WebViewHistoryCallback) {
getWebViewHistoryDisposable = dataSource.getAllWebViewPagesHistory()
.map { roomEntities ->
roomEntities.map(::WebViewHistoryItem)
}
Expand All @@ -97,8 +97,8 @@ class MainRepositoryActions @Inject constructor(private val dataSource: DataSour
saveNoteDisposable?.dispose()
deleteNoteDisposable?.dispose()
saveBookDisposable?.dispose()
savePageHistoryDisposable?.dispose()
clearPageHistoryDisposable?.dispose()
getPageHistoryDisposable?.dispose()
saveWebViewHistoryDisposable?.dispose()
clearWebViewHistoryDisposable?.dispose()
getWebViewHistoryDisposable?.dispose()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ data class WebViewHistoryItem(
)
}

interface DataCallback {
interface WebViewHistoryCallback {
fun onDataFetched(pageHistory: List<WebViewHistoryItem>)
fun onError(error: Throwable)
}
1 change: 1 addition & 0 deletions core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
<string name="close_all_tabs">Close all tabs</string>
<string name="close_tab">Close tab</string>
<string name="could_not_restore_tabs">Could not restore tabs.</string>
<string name="could_not_restore_web_view_history">Could not restore web view history</string>
<string name="pending_state">Pending</string>
<string name="running_state">In Progress</string>
<string name="complete">Complete</string>
Expand Down

0 comments on commit 9f6b55d

Please sign in to comment.