From 069868cc6e1eea6b021fa9efbfc71b259f331b6c Mon Sep 17 00:00:00 2001 From: Ashutosh Singh Date: Fri, 13 Nov 2020 21:18:20 +0530 Subject: [PATCH 01/10] notesFrag initial --- .../main/res/navigation/kiwix_nav_graph.xml | 4 ++ core/objectbox-models/default.json | 58 ++++++++++++++++- core/objectbox-models/default.json.bak | 46 +++++++++++++- .../kiwix/kiwixmobile/core/dao/NewNoteDao.kt | 48 ++++++++++++++ .../core/dao/entities/NotesEntity.kt | 44 +++++++++++++ .../di/components/CoreActivityComponent.kt | 2 + .../core/di/modules/CoreViewModelModule.kt | 6 ++ .../kiwixmobile/core/main/AddNoteDialog.kt | 1 + .../core/page/notes/NotesFragment.kt | 48 ++++++++++++++ .../core/page/notes/adapter/NoteItem.kt | 28 +++++++++ .../core/page/notes/viewmodel/NotesState.kt | 35 +++++++++++ .../page/notes/viewmodel/NotesViewModel.kt | 63 +++++++++++++++++++ .../effects/UpdateAllNotesPreference.kt | 32 ++++++++++ .../core/page/viewmodel/effects/OpenNote.kt | 50 +++++++++++++++ .../core/utils/SharedPreferenceUtil.kt | 7 +++ core/src/main/res/menu/menu_drawer_main.xml | 6 ++ core/src/main/res/values/strings.xml | 5 ++ .../main/res/navigation/custom_nav_graph.xml | 4 ++ 18 files changed, 483 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteItem.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/UpdateAllNotesPreference.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt diff --git a/app/src/main/res/navigation/kiwix_nav_graph.xml b/app/src/main/res/navigation/kiwix_nav_graph.xml index c88915bc3..806b3d444 100644 --- a/app/src/main/res/navigation/kiwix_nav_graph.xml +++ b/app/src/main/res/navigation/kiwix_nav_graph.xml @@ -86,6 +86,10 @@ android:name="org.kiwix.kiwixmobile.core.page.bookmark.BookmarksFragment" android:label="BookmarksFragment" tools:layout="@layout/fragment_page" /> + + * 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.dao + +import io.objectbox.Box +import io.objectbox.kotlin.query +import io.reactivex.Flowable +import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity +import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity_ +import org.kiwix.kiwixmobile.core.page.adapter.Page +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteItem +import javax.inject.Inject + +class NewNoteDao @Inject constructor(val box: Box) : PageDao { + fun notes(): Flowable> = box.asFlowable(box.query { + order(NotesEntity_.noteTitle) + }).map { it.map(::NoteItem) } + + override fun pages(): Flowable> = notes() + + override fun deletePages(pagesToDelete: List) { + deleteNote(pagesToDelete as List) + } + + fun saveNote(noteItem: NoteItem) { + box.put(NotesEntity(noteItem)) + } + + private fun deleteNote(noteList: List) { + box.remove(noteList.map(::NotesEntity)) + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt new file mode 100644 index 000000000..83d59cbed --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt @@ -0,0 +1,44 @@ +/* + * 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.dao.entities + +import io.objectbox.annotation.Entity +import io.objectbox.annotation.Id +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteItem + +@Entity +data class NotesEntity( + @Id var id: Long = 0, + val zimId: String, + var zimName: String, + var zimFilePath: String?, + var noteTitle: String, + var noteBody: String, + var favicon: String? +) { + constructor(item: NoteItem) : this( + item.databaseId, + item.zimId, + item.zimName, + item.zimFilePath, + item.noteBody, + item.title, + item.favicon + ) +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt index 1fc00a639..3f5e5d765 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt @@ -29,6 +29,7 @@ import org.kiwix.kiwixmobile.core.page.bookmark.BookmarksFragment import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.effects.ShowDeleteBookmarksDialog import org.kiwix.kiwixmobile.core.page.history.HistoryFragment import org.kiwix.kiwixmobile.core.page.history.viewmodel.effects.ShowDeleteHistoryDialog +import org.kiwix.kiwixmobile.core.page.notes.NotesFragment import org.kiwix.kiwixmobile.core.search.SearchFragment import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ShowDeleteSearchDialog import org.kiwix.kiwixmobile.core.settings.CorePrefsFragment @@ -45,6 +46,7 @@ interface CoreActivityComponent { fun inject(bookmarksFragment: BookmarksFragment) fun inject(addNoteDialog: AddNoteDialog) fun inject(helpFragment: HelpFragment) + fun inject(notesFragment: NotesFragment) @Subcomponent.Builder interface Builder { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/CoreViewModelModule.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/CoreViewModelModule.kt index 397244ab7..0d1067f75 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/CoreViewModelModule.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/CoreViewModelModule.kt @@ -27,6 +27,7 @@ import org.kiwix.kiwixmobile.core.ViewModelFactory import org.kiwix.kiwixmobile.core.di.ViewModelKey import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.BookmarkViewModel import org.kiwix.kiwixmobile.core.page.history.viewmodel.HistoryViewModel +import org.kiwix.kiwixmobile.core.page.notes.viewmodel.NotesViewModel import org.kiwix.kiwixmobile.core.search.viewmodel.SearchViewModel @Module @@ -45,6 +46,11 @@ abstract class CoreViewModelModule { @ViewModelKey(HistoryViewModel::class) abstract fun bindHistoryViewModel(historyViewModel: HistoryViewModel): ViewModel + @Binds + @IntoMap + @ViewModelKey(NotesViewModel::class) + abstract fun bindNotesViewModel(notesViewModel: NotesViewModel): ViewModel + @Binds @IntoMap @ViewModelKey(BookmarkViewModel::class) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt index dae4bc19e..82bb5490a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt @@ -264,6 +264,7 @@ class AddNoteDialog : DialogFragment() { } private fun saveNote(noteText: String) { + // save note here in object box as well , Note entity, NoteViewModel /* String content of the EditText, given by noteText, is saved into the text file given by: * "{External Storage}/Kiwix/Notes/ZimFileTitle/ArticleTitle.txt" diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt new file mode 100644 index 000000000..f95beb212 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt @@ -0,0 +1,48 @@ +/* + * 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.page.notes + +import org.kiwix.kiwixmobile.core.R +import org.kiwix.kiwixmobile.core.base.BaseActivity +import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent +import org.kiwix.kiwixmobile.core.extensions.viewModel +import org.kiwix.kiwixmobile.core.page.PageFragment +import org.kiwix.kiwixmobile.core.page.adapter.PageAdapter +import org.kiwix.kiwixmobile.core.page.adapter.PageDelegate +import org.kiwix.kiwixmobile.core.page.notes.viewmodel.NotesViewModel + +class NotesFragment : PageFragment() { + override val pageViewModel by lazy { viewModel(viewModelFactory) } + + override val pageAdapter: PageAdapter by lazy { + PageAdapter(PageDelegate.PageItemDelegate(this)) + } + + override val title: String by lazy { getString(R.string.notes) } + override val noItemsString: String by lazy { getString(R.string.no_notes) } + override val switchString: String by lazy { getString(R.string.notes_from_all_books) } + override val switchIsChecked: Boolean by lazy(sharedPreferenceUtil::showNotesAllBooks) + + override fun inject(baseActivity: BaseActivity) { + baseActivity.cachedComponent.inject(this) + } + + override val searchQueryHint: String by lazy { getString(R.string.search_bookmarks) } +} + diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteItem.kt new file mode 100644 index 000000000..b766d0a21 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteItem.kt @@ -0,0 +1,28 @@ +package org.kiwix.kiwixmobile.core.page.notes.adapter + +import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity +import org.kiwix.kiwixmobile.core.page.adapter.Page + +data class NoteItem( + val databaseId: Long = 0L, + override val zimId: String, + val zimName: String, + override val zimFilePath: String?, + val noteBody: String, + override val title: String, + override val favicon: String?, + override var isSelected: Boolean = false, + override val url: String = noteBody, + override val id: Long = databaseId +) : Page { + constructor(notesEntity: NotesEntity) : this( + notesEntity.id, + notesEntity.zimId, + notesEntity.zimName, + notesEntity.zimFilePath, + notesEntity.noteBody, + notesEntity.noteTitle, + notesEntity.favicon, + false + ) +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt new file mode 100644 index 000000000..9296c42da --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt @@ -0,0 +1,35 @@ +/* + * 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.page.notes.viewmodel + +import org.kiwix.kiwixmobile.core.page.adapter.PageRelated +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteItem +import org.kiwix.kiwixmobile.core.page.viewmodel.PageState + +data class NotesState( + override val pageItems: List, + override val showAll: Boolean, + override val currentZimId: String?, + override val searchTerm: String = "" +) : PageState() { + override val visiblePageItems: List = filteredPageItems + + override fun copyWithNewItems(newItems: List): PageState = + copy(pageItems = newItems) +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt new file mode 100644 index 000000000..15d9217f6 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt @@ -0,0 +1,63 @@ +/* + * 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.page.notes.viewmodel + +import org.kiwix.kiwixmobile.core.base.SideEffect +import org.kiwix.kiwixmobile.core.dao.NewNoteDao +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteItem +import org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects.UpdateAllNotesPreference +import org.kiwix.kiwixmobile.core.page.viewmodel.Action +import org.kiwix.kiwixmobile.core.page.viewmodel.PageViewModel +import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer +import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil +import javax.inject.Inject + +class NotesViewModel @Inject constructor( + notesDao: NewNoteDao, + zimReaderContainer: ZimReaderContainer, + sharedPrefs: SharedPreferenceUtil +) : PageViewModel(notesDao, sharedPrefs, zimReaderContainer) { + override fun initialState(): NotesState = + NotesState(emptyList(), sharedPreferenceUtil.showNotesAllBooks, zimReaderContainer.id) + + override fun updatePagesBasedOnFilter(state: NotesState, action: Action.Filter): NotesState = + state.copy(searchTerm = action.searchTerm) + + override fun updatePages(state: NotesState, action: Action.UpdatePages): NotesState = + state.copy(pageItems = action.pages.filterIsInstance()) + + override fun offerUpdateToShowAllToggle( + action: Action.UserClickedShowAllToggle, + state: NotesState + ): NotesState { + effects.offer(UpdateAllNotesPreference(sharedPreferenceUtil, action.isChecked)) + return state.copy(showAll = action.isChecked) + } + + override fun copyWithNewItems(state: NotesState, newItems: List): NotesState = + state.copy(pageItems = newItems) + + override fun deselectAllPages(state: NotesState): NotesState = + state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) }) + + override fun createDeletePageDialogEffect(state: NotesState): SideEffect<*> { + TODO("Not yet implemented") + // implement delete note or something like that + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/UpdateAllNotesPreference.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/UpdateAllNotesPreference.kt new file mode 100644 index 000000000..8a00fea06 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/UpdateAllNotesPreference.kt @@ -0,0 +1,32 @@ +/* + * 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.page.notes.viewmodel.effects + +import androidx.appcompat.app.AppCompatActivity +import org.kiwix.kiwixmobile.core.base.SideEffect +import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil + +data class UpdateAllNotesPreference( + private val sharedPreferenceUtil: SharedPreferenceUtil, + private val isChecked: Boolean +) : SideEffect { + override fun invokeWith(activity: AppCompatActivity) { + sharedPreferenceUtil.showNotesAllBooks = isChecked + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt new file mode 100644 index 000000000..250b19b89 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt @@ -0,0 +1,50 @@ +/* + * 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.page.viewmodel.effects + +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentTransaction +import org.kiwix.kiwixmobile.core.base.SideEffect +import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.popNavigationBackstack +import org.kiwix.kiwixmobile.core.main.AddNoteDialog +import org.kiwix.kiwixmobile.core.main.CoreMainActivity +import org.kiwix.kiwixmobile.core.page.adapter.Page + +class OpenNote( +) : SideEffect { + override fun invokeWith(activity: AppCompatActivity) { + activity as CoreMainActivity + activity.popNavigationBackstack() + showAddNoteDialog(activity) + } + + private fun showAddNoteDialog(activity: AppCompatActivity) { + val act: CoreMainActivity = activity as CoreMainActivity + val fragmentTransaction: FragmentTransaction = + act.supportFragmentManager.beginTransaction() + val previousInstance: Fragment? = + act.supportFragmentManager.findFragmentByTag(AddNoteDialog.TAG) + + if (previousInstance == null) { + val dialogFragment = AddNoteDialog() + dialogFragment.show(fragmentTransaction, AddNoteDialog.TAG) + } + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt index ffb3760c1..ab0fd6c0a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt @@ -151,6 +151,12 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) { putBoolean(PREF_SHOW_BOOKMARKS_ALL_BOOKS, prefShowBookmarksFromCurrentBook) } + var showNotesAllBooks: Boolean + get() = sharedPreferences.getBoolean(PREF_SHOW_NOTES_ALL_BOOKS, true) + set(prefShowBookmarksFromCurrentBook) = sharedPreferences.edit { + putBoolean(PREF_SHOW_NOTES_ALL_BOOKS, prefShowBookmarksFromCurrentBook) + } + val nightMode: NightModeConfig.Mode get() = from( sharedPreferences.getString(PREF_NIGHT_MODE, null)?.toInt() @@ -209,6 +215,7 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) { private const val PREF_IS_FIRST_RUN = "isFirstRun" private const val PREF_SHOW_BOOKMARKS_ALL_BOOKS = "show_bookmarks_current_book" private const val PREF_SHOW_HISTORY_ALL_BOOKS = "show_history_current_book" + private const val PREF_SHOW_NOTES_ALL_BOOKS = "show_notes_current_book" private const val PREF_HOSTED_BOOKS = "hosted_books" const val PREF_NIGHT_MODE = "pref_night_mode" private const val TEXT_ZOOM = "true_text_zoom" diff --git a/core/src/main/res/menu/menu_drawer_main.xml b/core/src/main/res/menu/menu_drawer_main.xml index 00c284e0f..9ac7376f0 100644 --- a/core/src/main/res/menu/menu_drawer_main.xml +++ b/core/src/main/res/menu/menu_drawer_main.xml @@ -17,6 +17,12 @@ android:title="@string/history" app:showAsAction="never" /> + + 1 -1 + + Hello blank fragment + Notes + No Notes + View Notes From All Books diff --git a/custom/src/main/res/navigation/custom_nav_graph.xml b/custom/src/main/res/navigation/custom_nav_graph.xml index 319da58c9..198bb8c4b 100644 --- a/custom/src/main/res/navigation/custom_nav_graph.xml +++ b/custom/src/main/res/navigation/custom_nav_graph.xml @@ -47,6 +47,10 @@ android:id="@+id/historyFragment" android:name="org.kiwix.kiwixmobile.core.page.history.HistoryFragment" android:label="HistoryFragment" /> + Date: Sat, 28 Aug 2021 18:42:14 +0530 Subject: [PATCH 02/10] feature #1948 commit 1 --- .../kiwixmobile/main/KiwixMainActivity.kt | 1 + .../main/res/navigation/kiwix_nav_graph.xml | 3 +- core/objectbox-models/default.json | 15 ++++---- core/objectbox-models/default.json.bak | 34 ++++++++++++------- .../kiwix/kiwixmobile/core/dao/NewNoteDao.kt | 14 +++++--- .../core/dao/entities/NotesEntity.kt | 21 ++++++------ .../kiwix/kiwixmobile/core/data/Repository.kt | 20 +++++++++++ .../core/di/components/CoreComponent.kt | 2 ++ .../core/di/modules/DatabaseModule.kt | 4 +++ .../kiwixmobile/core/main/AddNoteDialog.kt | 31 +++++++++++++++-- .../kiwixmobile/core/main/CoreMainActivity.kt | 6 ++++ .../core/main/MainRepositoryActions.kt | 6 ++++ .../core/page/notes/NotesFragment.kt | 9 ++--- .../adapter/{NoteItem.kt => NoteListItem.kt} | 24 ++++++------- .../core/page/notes/viewmodel/NotesState.kt | 8 ++--- .../page/notes/viewmodel/NotesViewModel.kt | 8 ++--- .../core/page/viewmodel/effects/OpenNote.kt | 4 +-- core/src/main/res/values/strings.xml | 1 + .../custom/main/CustomMainActivity.kt | 1 + 19 files changed, 146 insertions(+), 66 deletions(-) rename core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/{NoteItem.kt => NoteListItem.kt} (60%) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt b/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt index 0d00e9198..234570a93 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt @@ -55,6 +55,7 @@ class KiwixMainActivity : CoreMainActivity() { override val bookmarksFragmentResId: Int = R.id.bookmarksFragment override val settingsFragmentResId: Int = R.id.kiwixSettingsFragment override val historyFragmentResId: Int = R.id.historyFragment + override val notesFragmentResId: Int = R.id.notesFragment override val readerFragmentResId: Int = R.id.readerFragment override val helpFragmentResId: Int = R.id.helpFragment override val topLevelDestinations = diff --git a/app/src/main/res/navigation/kiwix_nav_graph.xml b/app/src/main/res/navigation/kiwix_nav_graph.xml index 806b3d444..c91ae56e4 100644 --- a/app/src/main/res/navigation/kiwix_nav_graph.xml +++ b/app/src/main/res/navigation/kiwix_nav_graph.xml @@ -89,7 +89,8 @@ + android:label="NotesFragment" + tools:layout="@layout/fragment_page" /> ) : PageDao { fun notes(): Flowable> = box.asFlowable(box.query { order(NotesEntity_.noteTitle) - }).map { it.map(::NoteItem) } + }).map { it.map(::NoteListItem) } override fun pages(): Flowable> = notes() override fun deletePages(pagesToDelete: List) { - deleteNote(pagesToDelete as List) + deleteNote(pagesToDelete as List) } - fun saveNote(noteItem: NoteItem) { + fun saveNote(noteItem: NoteListItem) { box.put(NotesEntity(noteItem)) } - private fun deleteNote(noteList: List) { + private fun deleteNote(noteList: List) { box.remove(noteList.map(::NotesEntity)) } + + fun deleteAllNotes() { + box.removeAll() + } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt index 83d59cbed..118712d4f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt @@ -20,25 +20,24 @@ package org.kiwix.kiwixmobile.core.dao.entities import io.objectbox.annotation.Entity import io.objectbox.annotation.Id -import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteItem +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem @Entity data class NotesEntity( @Id var id: Long = 0, val zimId: String, - var zimName: String, var zimFilePath: String?, - var noteTitle: String, + var noteTitle: String?, var noteBody: String, + var noteFilePath: String, var favicon: String? ) { - constructor(item: NoteItem) : this( - item.databaseId, - item.zimId, - item.zimName, - item.zimFilePath, - item.noteBody, - item.title, - item.favicon + constructor(item: NoteListItem) : this( + zimId = item.zimId, + zimFilePath = item.zimFilePath, + noteTitle = item.title, + noteBody = item.noteBody, + noteFilePath = item.noteFilePath, + favicon = item.favicon ) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt index e5bfb6e0b..39a7bf975 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt @@ -26,6 +26,7 @@ import org.kiwix.kiwixmobile.core.dao.HistoryDao import org.kiwix.kiwixmobile.core.dao.NewBookDao import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao +import org.kiwix.kiwixmobile.core.dao.NewNoteDao import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao import org.kiwix.kiwixmobile.core.di.qualifiers.IO import org.kiwix.kiwixmobile.core.di.qualifiers.MainThread @@ -33,6 +34,7 @@ import org.kiwix.kiwixmobile.core.extensions.HeaderizableList import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.zim_manager.Language import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem @@ -52,6 +54,7 @@ class Repository @Inject internal constructor( private val bookDao: NewBookDao, private val bookmarksDao: NewBookmarksDao, private val historyDao: HistoryDao, + private val notesDao: NewNoteDao, private val languageDao: NewLanguagesDao, private val recentSearchDao: NewRecentSearchDao, private val zimReaderContainer: ZimReaderContainer @@ -118,4 +121,21 @@ class Repository @Inject internal constructor( override fun deleteBookmark(bookmarkUrl: String): Completable? = Completable.fromAction { bookmarksDao.deleteBookmark(bookmarkUrl) } .subscribeOn(io) + + override fun saveNote(noteListItem: NoteListItem): Completable? = + Completable.fromAction { notesDao.saveNote(noteListItem) } + .subscribeOn(io) + + override fun deleteNote(noteList: MutableList): Completable = + Completable.fromAction { notesDao.deletePages(noteList) } + .subscribeOn(io) + + // this does note removes notes from storage only the list + override fun clearNotes(): Completable = Completable.fromAction { + notesDao.deleteAllNotes() + } + + // override fun notesOnDiskAsListItems(): Flowable> { + // TODO("Not yet implemented") + // } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt index 52d1667a6..8edb9a8ab 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt @@ -31,6 +31,7 @@ import org.kiwix.kiwixmobile.core.dao.HistoryDao import org.kiwix.kiwixmobile.core.dao.NewBookDao import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao +import org.kiwix.kiwixmobile.core.dao.NewNoteDao import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao import org.kiwix.kiwixmobile.core.data.DataModule import org.kiwix.kiwixmobile.core.data.DataSource @@ -87,6 +88,7 @@ interface CoreComponent { fun fetchDownloadDao(): FetchDownloadDao fun newBookDao(): NewBookDao fun historyDao(): HistoryDao + fun noteDao(): NewNoteDao fun newLanguagesDao(): NewLanguagesDao fun recentSearchDao(): NewRecentSearchDao fun newBookmarksDao(): NewBookmarksDao diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt index 6ccd463fd..8f9b957a1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/DatabaseModule.kt @@ -28,6 +28,7 @@ import org.kiwix.kiwixmobile.core.dao.HistoryDao import org.kiwix.kiwixmobile.core.dao.NewBookDao import org.kiwix.kiwixmobile.core.dao.NewBookmarksDao import org.kiwix.kiwixmobile.core.dao.NewLanguagesDao +import org.kiwix.kiwixmobile.core.dao.NewNoteDao import org.kiwix.kiwixmobile.core.dao.NewRecentSearchDao import org.kiwix.kiwixmobile.core.dao.entities.MyObjectBox import javax.inject.Singleton @@ -58,6 +59,9 @@ open class DatabaseModule { @Provides @Singleton fun providesNewBookmarksDao(boxStore: BoxStore): NewBookmarksDao = NewBookmarksDao(boxStore.boxFor()) + @Provides @Singleton fun providesNewNoteDao(boxStore: BoxStore): NewNoteDao = + NewNoteDao(boxStore.boxFor()) + @Provides @Singleton fun providesNewRecentSearchDao( boxStore: BoxStore, flowBuilder: FlowBuilder diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt index 82bb5490a..81eafa83b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt @@ -41,8 +41,10 @@ import kotlinx.android.synthetic.main.layout_toolbar.toolbar import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance import org.kiwix.kiwixmobile.core.R +import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity import org.kiwix.kiwixmobile.core.extensions.closeKeyboard import org.kiwix.kiwixmobile.core.extensions.toast +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.SimpleTextWatcher @@ -66,8 +68,10 @@ const val DISABLE_ICON_ITEM_ALPHA = 130 const val ENABLE_ICON_ITEM_ALPHA = 255 class AddNoteDialog : DialogFragment() { + private var zimId: String? = null private var zimFileName: String? = null private var zimFileTitle: String? = null + private var zimFavicon: String? = null private var articleTitle: String? = null // Corresponds to "ArticleUrl" of "{External Storage}/Kiwix/Notes/ZimFileName/ArticleUrl.txt" @@ -90,6 +94,9 @@ class AddNoteDialog : DialogFragment() { @Inject lateinit var alertDialogShower: AlertDialogShower + @Inject + lateinit var mainRepositoryActions: MainRepositoryActions + private val saveItem by lazy { toolbar.menu.findItem(R.id.save_note) } private val shareItem by lazy { toolbar.menu.findItem(R.id.share_note) } @@ -105,7 +112,9 @@ class AddNoteDialog : DialogFragment() { .inject(this) // Returns name of the form ".../Kiwix/granbluefantasy_en_all_all_nopic_2018-10.zim" + zimId = zimReaderContainer.id zimFileName = zimReaderContainer.zimCanonicalPath + zimFavicon = zimReaderContainer.favicon if (zimFileName != null) { // No zim file currently opened zimFileTitle = zimReaderContainer.zimFileTitle articleTitle = (activity as WebViewProvider?)?.getCurrentWebView()?.title @@ -264,8 +273,6 @@ class AddNoteDialog : DialogFragment() { } private fun saveNote(noteText: String) { - // save note here in object box as well , Note entity, NoteViewModel - /* String content of the EditText, given by noteText, is saved into the text file given by: * "{External Storage}/Kiwix/Notes/ZimFileTitle/ArticleTitle.txt" * */ @@ -296,6 +303,7 @@ class AddNoteDialog : DialogFragment() { // Save note text-file code: try { noteFile.writeText(noteText) + addNoteToDao(noteText, noteFile.canonicalPath) context.toast(R.string.note_save_successful, Toast.LENGTH_SHORT) noteEdited = false // As no unsaved changes remain enableDeleteNoteMenuItem() @@ -312,6 +320,25 @@ class AddNoteDialog : DialogFragment() { } } + private fun addNoteToDao(noteText: String, noteFilePath: String?) { + // adding only if saving file is success + noteFilePath?.let { filePath -> + if (filePath.isNotEmpty()) { + val noteToSave = NoteListItem( + NotesEntity( + zimId = zimId.orEmpty(), + zimFilePath = zimFileName, + noteTitle = zimFileTitle, + noteFilePath = noteFilePath, + noteBody = noteText, + favicon = zimFavicon + ) + ) + mainRepositoryActions.saveNote(noteToSave) + } + } + } + private fun deleteNote() { val notesFolder = File(zimNotesDirectory) val noteFile = diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt index 4e00d5381..397d21e34 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt @@ -72,6 +72,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { abstract val bookmarksFragmentResId: Int abstract val settingsFragmentResId: Int abstract val historyFragmentResId: Int + abstract val notesFragmentResId: Int abstract val helpFragmentResId: Int abstract val cachedComponent: CoreActivityComponent abstract val topLevelDestinations: Set @@ -194,6 +195,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { R.id.menu_support_kiwix -> openSupportKiwixExternalLink() R.id.menu_settings -> openSettings() R.id.menu_help -> openHelpFragment() + R.id.menu_notes -> openNotes() R.id.menu_history -> openHistory() R.id.menu_bookmarks_list -> openBookmarks() else -> return false @@ -303,6 +305,10 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { handleDrawerOnNavigation() } + private fun openNotes() { + navigate(notesFragmentResId) + } + protected fun handleDrawerOnNavigation() { closeNavigationDrawer() disableDrawer() diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt index 0cea23f17..a4ce74100 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt @@ -22,6 +22,7 @@ import org.kiwix.kiwixmobile.core.data.DataSource import org.kiwix.kiwixmobile.core.di.ActivityScope import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem import javax.inject.Inject private const val TAG = "MainPresenter" @@ -44,4 +45,9 @@ class MainRepositoryActions @Inject constructor(private val dataSource: DataSour ?.subscribe({}, { e -> Log.e(TAG, "Unable to delete bookmark", e) }) ?: Log.e(TAG, "Unable to delete bookmark") } + + fun saveNote(note: NoteListItem) { + dataSource.saveNote(note) + .subscribe({}, { e -> Log.e(TAG, "Unable to save note", e) }) + } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt index f95beb212..5ae9e6021 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt @@ -30,19 +30,20 @@ import org.kiwix.kiwixmobile.core.page.notes.viewmodel.NotesViewModel class NotesFragment : PageFragment() { override val pageViewModel by lazy { viewModel(viewModelFactory) } + override val screenTitle: String + get() = getString(R.string.notes) + override val pageAdapter: PageAdapter by lazy { PageAdapter(PageDelegate.PageItemDelegate(this)) } - override val title: String by lazy { getString(R.string.notes) } override val noItemsString: String by lazy { getString(R.string.no_notes) } override val switchString: String by lazy { getString(R.string.notes_from_all_books) } - override val switchIsChecked: Boolean by lazy(sharedPreferenceUtil::showNotesAllBooks) + override val switchIsChecked: Boolean by lazy { sharedPreferenceUtil.showNotesAllBooks } override fun inject(baseActivity: BaseActivity) { baseActivity.cachedComponent.inject(this) } - override val searchQueryHint: String by lazy { getString(R.string.search_bookmarks) } + override val searchQueryHint: String by lazy { getString(R.string.search_notes) } } - diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt similarity index 60% rename from core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteItem.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt index b766d0a21..1c034712a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt @@ -3,26 +3,26 @@ package org.kiwix.kiwixmobile.core.page.notes.adapter import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity import org.kiwix.kiwixmobile.core.page.adapter.Page -data class NoteItem( +data class NoteListItem( val databaseId: Long = 0L, override val zimId: String, - val zimName: String, - override val zimFilePath: String?, - val noteBody: String, override val title: String, + override val zimFilePath: String?, + val noteFilePath: String, + val noteBody: String, override val favicon: String?, override var isSelected: Boolean = false, override val url: String = noteBody, override val id: Long = databaseId ) : Page { constructor(notesEntity: NotesEntity) : this( - notesEntity.id, - notesEntity.zimId, - notesEntity.zimName, - notesEntity.zimFilePath, - notesEntity.noteBody, - notesEntity.noteTitle, - notesEntity.favicon, - false + databaseId = notesEntity.id, + zimId = notesEntity.zimId, + zimFilePath = notesEntity.zimFilePath, + noteFilePath = notesEntity.noteFilePath, + title = notesEntity.noteTitle.orEmpty(), + noteBody = notesEntity.noteBody, + favicon = notesEntity.favicon, + isSelected = false ) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt index 9296c42da..948926b77 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesState.kt @@ -19,17 +19,17 @@ package org.kiwix.kiwixmobile.core.page.notes.viewmodel import org.kiwix.kiwixmobile.core.page.adapter.PageRelated -import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteItem +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem import org.kiwix.kiwixmobile.core.page.viewmodel.PageState data class NotesState( - override val pageItems: List, + override val pageItems: List, override val showAll: Boolean, override val currentZimId: String?, override val searchTerm: String = "" -) : PageState() { +) : PageState() { override val visiblePageItems: List = filteredPageItems - override fun copyWithNewItems(newItems: List): PageState = + override fun copyWithNewItems(newItems: List): PageState = copy(pageItems = newItems) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt index 15d9217f6..91f1d3e72 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt @@ -20,7 +20,7 @@ package org.kiwix.kiwixmobile.core.page.notes.viewmodel import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.dao.NewNoteDao -import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteItem +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem import org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects.UpdateAllNotesPreference import org.kiwix.kiwixmobile.core.page.viewmodel.Action import org.kiwix.kiwixmobile.core.page.viewmodel.PageViewModel @@ -32,7 +32,7 @@ class NotesViewModel @Inject constructor( notesDao: NewNoteDao, zimReaderContainer: ZimReaderContainer, sharedPrefs: SharedPreferenceUtil -) : PageViewModel(notesDao, sharedPrefs, zimReaderContainer) { +) : PageViewModel(notesDao, sharedPrefs, zimReaderContainer) { override fun initialState(): NotesState = NotesState(emptyList(), sharedPreferenceUtil.showNotesAllBooks, zimReaderContainer.id) @@ -40,7 +40,7 @@ class NotesViewModel @Inject constructor( state.copy(searchTerm = action.searchTerm) override fun updatePages(state: NotesState, action: Action.UpdatePages): NotesState = - state.copy(pageItems = action.pages.filterIsInstance()) + state.copy(pageItems = action.pages.filterIsInstance()) override fun offerUpdateToShowAllToggle( action: Action.UserClickedShowAllToggle, @@ -50,7 +50,7 @@ class NotesViewModel @Inject constructor( return state.copy(showAll = action.isChecked) } - override fun copyWithNewItems(state: NotesState, newItems: List): NotesState = + override fun copyWithNewItems(state: NotesState, newItems: List): NotesState = state.copy(pageItems = newItems) override fun deselectAllPages(state: NotesState): NotesState = diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt index 250b19b89..36670d06b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt @@ -25,10 +25,8 @@ import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.popNavigationBackstack import org.kiwix.kiwixmobile.core.main.AddNoteDialog import org.kiwix.kiwixmobile.core.main.CoreMainActivity -import org.kiwix.kiwixmobile.core.page.adapter.Page -class OpenNote( -) : SideEffect { +class OpenNote() : SideEffect { override fun invokeWith(activity: AppCompatActivity) { activity as CoreMainActivity activity.popNavigationBackstack() diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index eb0b3d223..b6e644f32 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -318,4 +318,5 @@ Notes No Notes View Notes From All Books + Search Notes diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt index 54da5d072..5a4314d37 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt @@ -47,6 +47,7 @@ class CustomMainActivity : CoreMainActivity() { override val settingsFragmentResId: Int = R.id.customSettingsFragment override val readerFragmentResId: Int = R.id.customReaderFragment override val historyFragmentResId: Int = R.id.historyFragment + override val notesFragmentResId: Int = R.id.notesFragment override val helpFragmentResId: Int = R.id.helpFragment override val cachedComponent by lazy { customActivityComponent } override val topLevelDestinations = From 73ace41599c467435f70afcfa93392c84e72ef39 Mon Sep 17 00:00:00 2001 From: 4shutosh <4shutoshsingh@gmail.com> Date: Sun, 29 Aug 2021 21:45:13 +0530 Subject: [PATCH 03/10] feature #1948 commit 2 --- config/detekt/detekt.yml | 5 +- .../kiwix/kiwixmobile/core/dao/NewNoteDao.kt | 13 +++- .../core/dao/entities/NotesEntity.kt | 10 ++- .../kiwix/kiwixmobile/core/data/Repository.kt | 17 ++--- .../core/data/local/KiwixDatabase.java | 2 +- .../data/local/dao/NetworkLanguageDao.java | 2 +- .../core/data/local/dao/RecentSearchDao.java | 2 +- .../di/components/CoreActivityComponent.kt | 4 + .../kiwixmobile/core/downloader/Chunk.java | 14 ++-- .../core/entity/LibraryNetworkEntity.java | 4 +- .../kiwixmobile/core/main/AddNoteDialog.kt | 74 ++++++++++++------- .../core/main/MainRepositoryActions.kt | 5 ++ .../kiwixmobile/core/page/PageFragment.kt | 2 + .../core/page/bookmark/BookmarksFragment.kt | 2 + .../core/page/history/HistoryFragment.kt | 2 + .../core/page/notes/NotesFragment.kt | 6 +- .../core/page/notes/adapter/NoteListItem.kt | 35 ++++++--- .../page/notes/viewmodel/NotesViewModel.kt | 22 ++++-- .../effects/ShowDeleteNotesDialog.kt | 49 ++++++++++++ .../viewmodel/effects/ShowOpenNoteDialog.kt | 54 ++++++++++++++ .../core/page/viewmodel/PageViewModel.kt | 16 +++- .../viewmodel/PageViewModelClickListener.kt | 26 +++++++ .../core/page/viewmodel/effects/OpenNote.kt | 12 ++- .../kiwixmobile/core/utils/TestingUtils.java | 2 +- .../core/utils/dialog/KiwixDialog.kt | 21 ++++++ core/src/main/res/menu/menu_drawer_main.xml | 2 +- core/src/main/res/values/strings.xml | 8 +- 27 files changed, 325 insertions(+), 86 deletions(-) create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModelClickListener.kt diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml index 45ba1cf73..64c1c4e16 100644 --- a/config/detekt/detekt.yml +++ b/config/detekt/detekt.yml @@ -78,8 +78,9 @@ complexity: active: true threshold: 60 LongParameterList: - active: true - threshold: 6 + active: false + functionThreshold: 9 + constructorThreshold: 9 ignoreDefaultParameters: false MethodOverloading: active: false diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt index a1529536d..05009a1f1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt @@ -34,18 +34,23 @@ class NewNoteDao @Inject constructor(val box: Box) : PageDao { override fun pages(): Flowable> = notes() - override fun deletePages(pagesToDelete: List) { - deleteNote(pagesToDelete as List) - } + override fun deletePages(pagesToDelete: List) = + deleteNotes(pagesToDelete as List) fun saveNote(noteItem: NoteListItem) { box.put(NotesEntity(noteItem)) } - private fun deleteNote(noteList: List) { + fun deleteNotes(noteList: List) { box.remove(noteList.map(::NotesEntity)) } + fun deleteNote(noteUniqueKey: String) { + box.query { + equal(NotesEntity_.noteTitle, noteUniqueKey) + }.remove() + } + fun deleteAllNotes() { box.removeAll() } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt index 118712d4f..f13fa2a75 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt @@ -24,19 +24,21 @@ import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem @Entity data class NotesEntity( - @Id var id: Long = 0, + @Id var id: Long = 0L, val zimId: String, var zimFilePath: String?, - var noteTitle: String?, - var noteBody: String, + val zimUrl: String, + // @Unique(onConflict = ConflictStrategy.REPLACE) add resolve conflict dependency update required + var noteTitle: String, var noteFilePath: String, var favicon: String? ) { constructor(item: NoteListItem) : this( + id = item.databaseId, zimId = item.zimId, zimFilePath = item.zimFilePath, + zimUrl = item.zimUrl, noteTitle = item.title, - noteBody = item.noteBody, noteFilePath = item.noteFilePath, favicon = item.favicon ) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt index 39a7bf975..a0fe1d649 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt @@ -126,16 +126,15 @@ class Repository @Inject internal constructor( Completable.fromAction { notesDao.saveNote(noteListItem) } .subscribeOn(io) - override fun deleteNote(noteList: MutableList): Completable = - Completable.fromAction { notesDao.deletePages(noteList) } + override fun deleteNotes(noteList: MutableList) = + Completable.fromAction { notesDao.deleteNotes(noteList) } .subscribeOn(io) - // this does note removes notes from storage only the list - override fun clearNotes(): Completable = Completable.fromAction { - notesDao.deleteAllNotes() - } + override fun deleteNote(noteUniqueKey: String): Completable? = + Completable.fromAction { notesDao.deleteNote(noteUniqueKey) } + .subscribeOn(io) - // override fun notesOnDiskAsListItems(): Flowable> { - // TODO("Not yet implemented") - // } + // this does note removes notes from storage only the list : remove txt files as well? + override fun clearNotes(): Completable = + Completable.fromAction(notesDao::deleteAllNotes).subscribeOn(io) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/KiwixDatabase.java b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/KiwixDatabase.java index 7b1f72e24..daf5b4285 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/KiwixDatabase.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/KiwixDatabase.java @@ -179,7 +179,7 @@ public class KiwixDatabase extends SquidDatabase { public void migrateBookmarksVersion6() { String[] ids = context.fileList(); for (String id : ids) { - if (id.length() == 40 && id.substring(id.length() - 4).equals(".txt")) { + if (id.length() == 40 && id.endsWith(".txt")) { try { String idName = id.substring(0, id.length() - 4); InputStream stream = context.openFileInput(id); diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/NetworkLanguageDao.java b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/NetworkLanguageDao.java index b5a53d114..752fbdca6 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/NetworkLanguageDao.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/NetworkLanguageDao.java @@ -28,7 +28,7 @@ import org.kiwix.kiwixmobile.core.zim_manager.Language; @Deprecated public class NetworkLanguageDao { - private KiwixDatabase mDb; + private final KiwixDatabase mDb; @Inject public NetworkLanguageDao(KiwixDatabase kiwixDatabase) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/RecentSearchDao.java b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/RecentSearchDao.java index 53fc78439..df6bb96b5 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/RecentSearchDao.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/RecentSearchDao.java @@ -32,7 +32,7 @@ import org.kiwix.kiwixmobile.core.data.local.entity.RecentSearch; public class RecentSearchDao { private static final int NUM_RECENT_RESULTS = 5; - private KiwixDatabase mDb; + private final KiwixDatabase mDb; @Inject public RecentSearchDao(KiwixDatabase kiwixDatabase) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt index 3f5e5d765..4cb6b27ba 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt @@ -30,6 +30,8 @@ import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.effects.ShowDeleteBook import org.kiwix.kiwixmobile.core.page.history.HistoryFragment import org.kiwix.kiwixmobile.core.page.history.viewmodel.effects.ShowDeleteHistoryDialog import org.kiwix.kiwixmobile.core.page.notes.NotesFragment +import org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects.ShowDeleteNotesDialog +import org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects.ShowOpenNoteDialog import org.kiwix.kiwixmobile.core.search.SearchFragment import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ShowDeleteSearchDialog import org.kiwix.kiwixmobile.core.settings.CorePrefsFragment @@ -41,6 +43,8 @@ interface CoreActivityComponent { fun inject(showDeleteSearchDialog: ShowDeleteSearchDialog) fun inject(showDeleteBookmarksDialog: ShowDeleteBookmarksDialog) fun inject(showDeleteHistoryDialog: ShowDeleteHistoryDialog) + fun inject(showOpenNoteDialog: ShowOpenNoteDialog) + fun inject(showDeleteNotesDialog: ShowDeleteNotesDialog) fun inject(corePrefsFragment: CorePrefsFragment) fun inject(historyFragment: HistoryFragment) fun inject(bookmarksFragment: BookmarksFragment) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java index 84f6383fb..7d070be46 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java @@ -20,13 +20,13 @@ package org.kiwix.kiwixmobile.core.downloader; public class Chunk { public boolean isDownloaded = false; - private String rangeHeader; - private String fileName; - private String url; - private long contentLength; - private int notificationID; - private long startByte; - private long endByte; + private final String rangeHeader; + private final String fileName; + private final String url; + private final long contentLength; + private final int notificationID; + private final long startByte; + private final long endByte; public Chunk(String rangeHeader, String fileName, String url, long contentLength, int notificationID, long startByte, long endByte) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java index 6953de8d1..8ea531e70 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java @@ -157,9 +157,7 @@ public class LibraryNetworkEntity { @Override public boolean equals(Object obj) { if (obj instanceof Book) { - if (((Book) obj).getId() != null && ((Book) obj).getId().equals(getId())) { - return true; - } + return ((Book) obj).getId() != null && ((Book) obj).getId().equals(getId()); } return false; } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt index 81eafa83b..fb1fb2735 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt @@ -41,7 +41,6 @@ import kotlinx.android.synthetic.main.layout_toolbar.toolbar import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance import org.kiwix.kiwixmobile.core.R -import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity import org.kiwix.kiwixmobile.core.extensions.closeKeyboard import org.kiwix.kiwixmobile.core.extensions.toast import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem @@ -71,6 +70,7 @@ class AddNoteDialog : DialogFragment() { private var zimId: String? = null private var zimFileName: String? = null private var zimFileTitle: String? = null + private var zimFileUrl: String? = null private var zimFavicon: String? = null private var articleTitle: String? = null @@ -112,18 +112,19 @@ class AddNoteDialog : DialogFragment() { .inject(this) // Returns name of the form ".../Kiwix/granbluefantasy_en_all_all_nopic_2018-10.zim" - zimId = zimReaderContainer.id zimFileName = zimReaderContainer.zimCanonicalPath - zimFavicon = zimReaderContainer.favicon if (zimFileName != null) { // No zim file currently opened zimFileTitle = zimReaderContainer.zimFileTitle - articleTitle = (activity as WebViewProvider?)?.getCurrentWebView()?.title + zimFavicon = zimReaderContainer.favicon + zimId = zimReaderContainer.id + + val webView = (activity as WebViewProvider?)?.getCurrentWebView() + articleTitle = webView?.title + zimFileUrl = webView?.url // Corresponds to "ZimFileName" of "{External Storage}/Kiwix/Notes/ZimFileName/ArticleUrl.txt" - articleNoteFileName = getArticleNotefileName() + articleNoteFileName = getArticleNoteFileName() zimNotesDirectory = "$NOTES_DIRECTORY$zimNoteDirectoryName/" - } else { - onFailureToCreateAddNoteDialog() } } @@ -149,9 +150,13 @@ class AddNoteDialog : DialogFragment() { return (if (noteDirectoryName.isNotEmpty()) noteDirectoryName else zimFileTitle) ?: "" } - private fun getArticleNotefileName(): String { + private fun getArticleNoteFileName(): String { // Returns url of the form: "content://org.kiwix.kiwixmobile.zim.base/A/Main_Page.html" - val articleUrl = (activity as WebViewProvider?)?.getCurrentWebView()?.url + val articleUrl = if (arguments != null) { + arguments?.getString(ARTICLE_URL) + } else { + (activity as WebViewProvider?)?.getCurrentWebView()?.url + } var noteFileName = "" if (articleUrl == null) { onFailureToCreateAddNoteDialog() @@ -303,16 +308,17 @@ class AddNoteDialog : DialogFragment() { // Save note text-file code: try { noteFile.writeText(noteText) - addNoteToDao(noteText, noteFile.canonicalPath) context.toast(R.string.note_save_successful, Toast.LENGTH_SHORT) noteEdited = false // As no unsaved changes remain enableDeleteNoteMenuItem() + // adding only if saving file is success + addNoteToDao(noteFile.canonicalPath, zimFileTitle.orEmpty()) } catch (e: IOException) { e.printStackTrace() .also { context.toast(R.string.note_save_unsuccessful, Toast.LENGTH_LONG) } } } else { - context.toast(R.string.note_save_successful, Toast.LENGTH_LONG) + context.toast(R.string.note_save_unsuccessful, Toast.LENGTH_LONG) Log.d(TAG, "Required folder doesn't exist") } } else { @@ -320,21 +326,23 @@ class AddNoteDialog : DialogFragment() { } } - private fun addNoteToDao(noteText: String, noteFilePath: String?) { - // adding only if saving file is success + private fun addNoteToDao(noteFilePath: String?, title: String) { noteFilePath?.let { filePath -> - if (filePath.isNotEmpty()) { - val noteToSave = NoteListItem( - NotesEntity( - zimId = zimId.orEmpty(), - zimFilePath = zimFileName, - noteTitle = zimFileTitle, + if (filePath.isNotEmpty() && zimFileUrl.orEmpty().isNotEmpty()) { + val zimReader = zimReaderContainer.zimFileReader + if (zimReader != null) { + val noteToSave = NoteListItem( + title = title, + url = zimFileUrl.orEmpty(), noteFilePath = noteFilePath, - noteBody = noteText, - favicon = zimFavicon + zimFileReader = zimReader ) - ) - mainRepositoryActions.saveNote(noteToSave) + mainRepositoryActions.saveNote(noteToSave) + } else { + Log.d(TAG, "zim reader found null") + } + } else { + Log.d(TAG, "Cannot process with empty zim url or noteFilePath") } } } @@ -346,6 +354,7 @@ class AddNoteDialog : DialogFragment() { val noteDeleted = noteFile.delete() if (noteDeleted) { add_note_edit_text.text.clear() + mainRepositoryActions.deleteNote(articleNoteFileName) disableMenuItems() context.toast(R.string.note_delete_successful, Toast.LENGTH_LONG) } else { @@ -358,10 +367,19 @@ class AddNoteDialog : DialogFragment() { * is displayed in the EditText field (note content area) */ private fun displayNote() { - - val noteFile = File("$zimNotesDirectory$articleNoteFileName.txt") - if (noteFile.exists()) { - readNoteFromFile(noteFile) + var noteFilePath: String? = "" + noteFilePath = if (arguments != null) { + arguments?.getString(NOTE_FILE_PATH) + } else { + "$zimNotesDirectory$articleNoteFileName.txt" + } + if (noteFilePath != null && noteFilePath.isNotEmpty()) { + val noteFile = File(noteFilePath) + if (noteFile.exists()) { + readNoteFromFile(noteFile) + } else { + onFailureToCreateAddNoteDialog() + } } // No action in case the note file for the currently open article doesn't exist @@ -431,5 +449,7 @@ class AddNoteDialog : DialogFragment() { @JvmField val NOTES_DIRECTORY = instance.getExternalFilesDir("").toString() + "/Kiwix/Notes/" const val TAG = "AddNoteDialog" + const val NOTE_FILE_PATH = "NoteFilePath" + const val ARTICLE_URL = "ArticleUrl" } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt index a4ce74100..9c417d846 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/MainRepositoryActions.kt @@ -50,4 +50,9 @@ class MainRepositoryActions @Inject constructor(private val dataSource: DataSour dataSource.saveNote(note) .subscribe({}, { e -> Log.e(TAG, "Unable to save note", e) }) } + + fun deleteNote(noteUniqueKey: String) { + dataSource.deleteNote(noteUniqueKey) + .subscribe({}, { e -> Log.e(TAG, "Unable to delete note", e) }) + } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt index be37e481f..55f01bf8f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt @@ -66,6 +66,7 @@ abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActiv abstract val switchString: String abstract val searchQueryHint: String abstract val pageAdapter: PageAdapter + abstract val switchVisibility: Int abstract val switchIsChecked: Boolean private val actionModeCallback: ActionMode.Callback = @@ -133,6 +134,7 @@ abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActiv no_page.text = noItemsString page_switch.text = switchString + page_switch.visibility = switchVisibility page_switch.isChecked = switchIsChecked compositeDisposable.add(pageViewModel.effects.subscribe { it.invokeWith(activity) }) page_switch.setOnCheckedChangeListener { _, isChecked -> diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksFragment.kt index 7dd278a5b..0342174ad 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksFragment.kt @@ -1,5 +1,6 @@ package org.kiwix.kiwixmobile.core.page.bookmark +import android.view.View import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent @@ -15,6 +16,7 @@ class BookmarksFragment : PageFragment() { override val pageAdapter: PageAdapter by lazy { PageAdapter(PageItemDelegate(this)) } + override val switchVisibility: Int = View.VISIBLE override val screenTitle: String by lazy { getString(R.string.bookmarks) } override val noItemsString: String by lazy { getString(R.string.no_bookmarks) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryFragment.kt index 0ed619f2f..86f7f4344 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryFragment.kt @@ -1,5 +1,6 @@ package org.kiwix.kiwixmobile.core.page.history +import android.view.View import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent @@ -18,6 +19,7 @@ class HistoryFragment : PageFragment() { override val pageAdapter by lazy { PageAdapter(PageItemDelegate(this), HistoryDateDelegate()) } + override val switchVisibility: Int = View.VISIBLE override val noItemsString: String by lazy { getString(R.string.no_history) } override val switchString: String by lazy { getString(R.string.history_from_current_book) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt index 5ae9e6021..550f1a500 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.core.page.notes +import android.view.View import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent @@ -31,15 +32,16 @@ class NotesFragment : PageFragment() { override val pageViewModel by lazy { viewModel(viewModelFactory) } override val screenTitle: String - get() = getString(R.string.notes) + get() = getString(R.string.pref_notes) override val pageAdapter: PageAdapter by lazy { PageAdapter(PageDelegate.PageItemDelegate(this)) } + override val switchVisibility: Int = View.GONE override val noItemsString: String by lazy { getString(R.string.no_notes) } override val switchString: String by lazy { getString(R.string.notes_from_all_books) } - override val switchIsChecked: Boolean by lazy { sharedPreferenceUtil.showNotesAllBooks } + override val switchIsChecked: Boolean by lazy(sharedPreferenceUtil::showNotesAllBooks) override fun inject(baseActivity: BaseActivity) { baseActivity.cachedComponent.inject(this) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt index 1c034712a..4f40d24cd 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/adapter/NoteListItem.kt @@ -2,27 +2,42 @@ package org.kiwix.kiwixmobile.core.page.notes.adapter import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity import org.kiwix.kiwixmobile.core.page.adapter.Page +import org.kiwix.kiwixmobile.core.reader.ZimFileReader data class NoteListItem( val databaseId: Long = 0L, override val zimId: String, override val title: String, override val zimFilePath: String?, + val zimUrl: String, val noteFilePath: String, - val noteBody: String, override val favicon: String?, override var isSelected: Boolean = false, - override val url: String = noteBody, + override val url: String = zimUrl, override val id: Long = databaseId ) : Page { + constructor(notesEntity: NotesEntity) : this( - databaseId = notesEntity.id, - zimId = notesEntity.zimId, - zimFilePath = notesEntity.zimFilePath, - noteFilePath = notesEntity.noteFilePath, - title = notesEntity.noteTitle.orEmpty(), - noteBody = notesEntity.noteBody, - favicon = notesEntity.favicon, - isSelected = false + notesEntity.id, + notesEntity.zimId, + notesEntity.noteTitle, + notesEntity.zimFilePath, + notesEntity.zimUrl, + notesEntity.noteFilePath, + notesEntity.favicon + ) + + constructor( + title: String, + url: String, + noteFilePath: String, + zimFileReader: ZimFileReader + ) : this( + zimId = zimFileReader.id, + title = title, + zimFilePath = zimFileReader.zimFile.canonicalPath, + zimUrl = url, + favicon = zimFileReader.favicon, + noteFilePath = noteFilePath ) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt index 91f1d3e72..ad7aa7462 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/NotesViewModel.kt @@ -18,12 +18,15 @@ package org.kiwix.kiwixmobile.core.page.notes.viewmodel -import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.dao.NewNoteDao +import org.kiwix.kiwixmobile.core.page.adapter.Page import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem +import org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects.ShowDeleteNotesDialog +import org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects.ShowOpenNoteDialog import org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects.UpdateAllNotesPreference import org.kiwix.kiwixmobile.core.page.viewmodel.Action import org.kiwix.kiwixmobile.core.page.viewmodel.PageViewModel +import org.kiwix.kiwixmobile.core.page.viewmodel.PageViewModelClickListener import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import javax.inject.Inject @@ -32,7 +35,13 @@ class NotesViewModel @Inject constructor( notesDao: NewNoteDao, zimReaderContainer: ZimReaderContainer, sharedPrefs: SharedPreferenceUtil -) : PageViewModel(notesDao, sharedPrefs, zimReaderContainer) { +) : PageViewModel(notesDao, sharedPrefs, zimReaderContainer), + PageViewModelClickListener { + + init { + setOnItemClickListener(this) + } + override fun initialState(): NotesState = NotesState(emptyList(), sharedPreferenceUtil.showNotesAllBooks, zimReaderContainer.id) @@ -56,8 +65,9 @@ class NotesViewModel @Inject constructor( override fun deselectAllPages(state: NotesState): NotesState = state.copy(pageItems = state.pageItems.map { it.copy(isSelected = false) }) - override fun createDeletePageDialogEffect(state: NotesState): SideEffect<*> { - TODO("Not yet implemented") - // implement delete note or something like that - } + override fun createDeletePageDialogEffect(state: NotesState) = + ShowDeleteNotesDialog(effects, state, pageDao) + + override fun onItemClick(page: Page) = + ShowOpenNoteDialog(effects, page, zimReaderContainer) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt new file mode 100644 index 000000000..00db5ce9d --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt @@ -0,0 +1,49 @@ +package org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects + +/* + * 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 . + * + */ + +import android.util.Log +import androidx.appcompat.app.AppCompatActivity +import io.reactivex.processors.PublishProcessor +import org.kiwix.kiwixmobile.core.base.SideEffect +import org.kiwix.kiwixmobile.core.dao.PageDao +import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent +import org.kiwix.kiwixmobile.core.page.notes.viewmodel.NotesState +import org.kiwix.kiwixmobile.core.page.viewmodel.effects.DeletePageItems +import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower +import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog.DeleteAllNotes +import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog.DeleteSelectedNotes +import javax.inject.Inject + +data class ShowDeleteNotesDialog( + private val effects: PublishProcessor>, + private val state: NotesState, + private val pageDao: PageDao +) : SideEffect { + @Inject lateinit var dialogShower: DialogShower + override fun invokeWith(activity: AppCompatActivity) { + activity.cachedComponent.inject(this) + Log.d("invoke", "invokeWith: invoked") + dialogShower.show( + if (state.isInSelectionState) DeleteSelectedNotes else DeleteAllNotes, + { + effects.offer(DeletePageItems(state, pageDao)) + }) + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt new file mode 100644 index 000000000..f78886d2b --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt @@ -0,0 +1,54 @@ +/* + * Kiwix Android + * Copyright (c) 2021 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.page.notes.viewmodel.effects + +import androidx.appcompat.app.AppCompatActivity +import io.reactivex.processors.PublishProcessor +import org.kiwix.kiwixmobile.core.base.SideEffect +import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent +import org.kiwix.kiwixmobile.core.page.adapter.Page +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem +import org.kiwix.kiwixmobile.core.page.viewmodel.effects.OpenNote +import org.kiwix.kiwixmobile.core.page.viewmodel.effects.OpenPage +import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer +import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower +import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog.ShowNoteDialog +import java.io.File +import javax.inject.Inject + +data class ShowOpenNoteDialog( + private val effects: PublishProcessor>, + private val page: Page, + private val zimReaderContainer: ZimReaderContainer +) : SideEffect { + @Inject lateinit var dialogShower: DialogShower + override fun invokeWith(activity: AppCompatActivity) { + activity.cachedComponent.inject(this) + dialogShower.show( + ShowNoteDialog, + { effects.offer(OpenPage(page, zimReaderContainer)) }, + { + val item = page as NoteListItem + val file = File(item.zimFilePath.orEmpty()) + zimReaderContainer.setZimFile(file) + effects.offer(OpenNote(item.noteFilePath, item.zimUrl)) + } + ) + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt index f0c93c3ce..edda2f164 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModel.kt @@ -49,6 +49,8 @@ abstract class PageViewModel>( abstract fun initialState(): S + private lateinit var pageViewModelClickListener: PageViewModelClickListener + val state: MutableLiveData by lazy { MutableLiveData().apply { value = initialState() @@ -106,13 +108,21 @@ abstract class PageViewModel>( abstract fun copyWithNewItems(state: S, newItems: List): S private fun handleItemClick(state: S, action: Action.OnItemClick): S { - if (state.isInSelectionState) { - return copyWithNewItems(state, state.getItemsAfterToggleSelectionOfItem(action.page)) + if (::pageViewModelClickListener.isInitialized) { + effects.offer(pageViewModelClickListener.onItemClick(action.page)) + } else { + if (state.isInSelectionState) { + return copyWithNewItems(state, state.getItemsAfterToggleSelectionOfItem(action.page)) + } + effects.offer(OpenPage(action.page, zimReaderContainer)) } - effects.offer(OpenPage(action.page, zimReaderContainer)) return state } + fun setOnItemClickListener(clickListener: PageViewModelClickListener) { + pageViewModelClickListener = clickListener + } + abstract fun deselectAllPages(state: S): S private fun exitFragment(state: S): S { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModelClickListener.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModelClickListener.kt new file mode 100644 index 000000000..b549a89a2 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/PageViewModelClickListener.kt @@ -0,0 +1,26 @@ +/* + * Kiwix Android + * Copyright (c) 2021 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.page.viewmodel + +import org.kiwix.kiwixmobile.core.base.SideEffect +import org.kiwix.kiwixmobile.core.page.adapter.Page + +interface PageViewModelClickListener { + fun onItemClick(page: Page): SideEffect<*> +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt index 36670d06b..55cf0102b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt @@ -18,15 +18,21 @@ package org.kiwix.kiwixmobile.core.page.viewmodel.effects +import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.popNavigationBackstack import org.kiwix.kiwixmobile.core.main.AddNoteDialog +import org.kiwix.kiwixmobile.core.main.AddNoteDialog.Companion.NOTE_FILE_PATH +import org.kiwix.kiwixmobile.core.main.AddNoteDialog.Companion.ARTICLE_URL import org.kiwix.kiwixmobile.core.main.CoreMainActivity -class OpenNote() : SideEffect { +class OpenNote( + private val noteFilePath: String, + private val zimFileUrl: String +) : SideEffect { override fun invokeWith(activity: AppCompatActivity) { activity as CoreMainActivity activity.popNavigationBackstack() @@ -42,6 +48,10 @@ class OpenNote() : SideEffect { if (previousInstance == null) { val dialogFragment = AddNoteDialog() + val bundle = Bundle() + bundle.putString(NOTE_FILE_PATH, noteFilePath) + bundle.putString(ARTICLE_URL, zimFileUrl) + dialogFragment.arguments = bundle dialogFragment.show(fragmentTransaction, AddNoteDialog.TAG) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/TestingUtils.java b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/TestingUtils.java index dc84a9d49..3a69873c6 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/TestingUtils.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/TestingUtils.java @@ -28,7 +28,7 @@ public class TestingUtils { private static TestingUtils.IdleListener callback; - private static Set resources = new HashSet<>(); + private static final Set resources = new HashSet<>(); public static void bindResource(Class bindClass) { if (callback != null) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt index d078632c2..fb2aac57b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt @@ -257,6 +257,27 @@ sealed class KiwixDialog( positiveMessage = R.string.delete, negativeMessage = R.string.cancel ) + + object DeleteAllNotes : KiwixDialog( + R.string.delete_notes_confirmation_msg, + R.string.delete_note_dialog_message, + positiveMessage = R.string.delete, + negativeMessage = R.string.cancel + ) + + object DeleteSelectedNotes : KiwixDialog( + R.string.delete_selected_notes, + R.string.delete_note_dialog_message, + positiveMessage = R.string.delete, + negativeMessage = R.string.cancel + ) + + object ShowNoteDialog : KiwixDialog( + R.string.choose_your_action, + null, + positiveMessage = R.string.open_article, + negativeMessage = R.string.open_note + ) } interface HasBodyFormatArgs { diff --git a/core/src/main/res/menu/menu_drawer_main.xml b/core/src/main/res/menu/menu_drawer_main.xml index 9ac7376f0..929dca42a 100644 --- a/core/src/main/res/menu/menu_drawer_main.xml +++ b/core/src/main/res/menu/menu_drawer_main.xml @@ -20,7 +20,7 @@ 1 -1 - - Hello blank fragment - Notes No Notes View Notes From All Books Search Notes + Choose your Action + Open Note + Open Article + Note: Notes are not deleted from your storage + Delete Selected Notes? From eec576c3d060de40e599e042de73f9a8bed87bb3 Mon Sep 17 00:00:00 2001 From: 4shutosh <4shutoshsingh@gmail.com> Date: Sun, 29 Aug 2021 22:18:00 +0530 Subject: [PATCH 04/10] code cleanup --- core/objectbox-models/default.json | 15 ++++++++------- core/objectbox-models/default.json.bak | 15 ++++++++------- .../core/data/local/dao/NetworkLanguageDao.java | 2 +- .../core/data/local/dao/RecentSearchDao.java | 2 +- .../kiwix/kiwixmobile/core/downloader/Chunk.java | 14 +++++++------- .../core/entity/LibraryNetworkEntity.java | 4 +++- .../kiwix/kiwixmobile/core/page/PageFragment.kt | 2 -- .../core/page/bookmark/BookmarksFragment.kt | 2 -- .../core/page/history/HistoryFragment.kt | 2 -- .../kiwixmobile/core/page/notes/NotesFragment.kt | 4 +--- .../kiwixmobile/core/utils/TestingUtils.java | 2 +- 11 files changed, 30 insertions(+), 34 deletions(-) diff --git a/core/objectbox-models/default.json b/core/objectbox-models/default.json index 32e6bed6b..3b758be12 100644 --- a/core/objectbox-models/default.json +++ b/core/objectbox-models/default.json @@ -379,7 +379,7 @@ }, { "id": "10:3205842982118792800", - "lastPropertyId": "8:4304230668306976620", + "lastPropertyId": "9:5286545520416917562", "name": "NotesEntity", "properties": [ { @@ -403,11 +403,6 @@ "name": "noteTitle", "type": 9 }, - { - "id": "6:1899740026144478138", - "name": "noteBody", - "type": 9 - }, { "id": "7:2160052450778801841", "name": "favicon", @@ -417,6 +412,11 @@ "id": "8:4304230668306976620", "name": "noteFilePath", "type": 9 + }, + { + "id": "9:5286545520416917562", + "name": "zimUrl", + "type": 9 } ], "relations": [] @@ -481,7 +481,8 @@ 3582932035787057158, 8819082642546094709, 7233601933599801875, - 4335394620556092321 + 4335394620556092321, + 1899740026144478138 ], "retiredRelationUids": [], "version": 1 diff --git a/core/objectbox-models/default.json.bak b/core/objectbox-models/default.json.bak index 6c243dd87..32e6bed6b 100644 --- a/core/objectbox-models/default.json.bak +++ b/core/objectbox-models/default.json.bak @@ -379,7 +379,7 @@ }, { "id": "10:3205842982118792800", - "lastPropertyId": "7:2160052450778801841", + "lastPropertyId": "8:4304230668306976620", "name": "NotesEntity", "properties": [ { @@ -393,11 +393,6 @@ "name": "zimId", "type": 9 }, - { - "id": "3:4335394620556092321", - "name": "zimName", - "type": 9 - }, { "id": "4:4488566778591049174", "name": "zimFilePath", @@ -417,6 +412,11 @@ "id": "7:2160052450778801841", "name": "favicon", "type": 9 + }, + { + "id": "8:4304230668306976620", + "name": "noteFilePath", + "type": 9 } ], "relations": [] @@ -480,7 +480,8 @@ 8027872860151528568, 3582932035787057158, 8819082642546094709, - 7233601933599801875 + 7233601933599801875, + 4335394620556092321 ], "retiredRelationUids": [], "version": 1 diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/NetworkLanguageDao.java b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/NetworkLanguageDao.java index 752fbdca6..b5a53d114 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/NetworkLanguageDao.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/NetworkLanguageDao.java @@ -28,7 +28,7 @@ import org.kiwix.kiwixmobile.core.zim_manager.Language; @Deprecated public class NetworkLanguageDao { - private final KiwixDatabase mDb; + private KiwixDatabase mDb; @Inject public NetworkLanguageDao(KiwixDatabase kiwixDatabase) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/RecentSearchDao.java b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/RecentSearchDao.java index df6bb96b5..53fc78439 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/RecentSearchDao.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/local/dao/RecentSearchDao.java @@ -32,7 +32,7 @@ import org.kiwix.kiwixmobile.core.data.local.entity.RecentSearch; public class RecentSearchDao { private static final int NUM_RECENT_RESULTS = 5; - private final KiwixDatabase mDb; + private KiwixDatabase mDb; @Inject public RecentSearchDao(KiwixDatabase kiwixDatabase) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java index 7d070be46..84f6383fb 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/Chunk.java @@ -20,13 +20,13 @@ package org.kiwix.kiwixmobile.core.downloader; public class Chunk { public boolean isDownloaded = false; - private final String rangeHeader; - private final String fileName; - private final String url; - private final long contentLength; - private final int notificationID; - private final long startByte; - private final long endByte; + private String rangeHeader; + private String fileName; + private String url; + private long contentLength; + private int notificationID; + private long startByte; + private long endByte; public Chunk(String rangeHeader, String fileName, String url, long contentLength, int notificationID, long startByte, long endByte) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java index 8ea531e70..6953de8d1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/LibraryNetworkEntity.java @@ -157,7 +157,9 @@ public class LibraryNetworkEntity { @Override public boolean equals(Object obj) { if (obj instanceof Book) { - return ((Book) obj).getId() != null && ((Book) obj).getId().equals(getId()); + if (((Book) obj).getId() != null && ((Book) obj).getId().equals(getId())) { + return true; + } } return false; } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt index 55f01bf8f..be37e481f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/PageFragment.kt @@ -66,7 +66,6 @@ abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActiv abstract val switchString: String abstract val searchQueryHint: String abstract val pageAdapter: PageAdapter - abstract val switchVisibility: Int abstract val switchIsChecked: Boolean private val actionModeCallback: ActionMode.Callback = @@ -134,7 +133,6 @@ abstract class PageFragment : OnItemClickListener, BaseFragment(), FragmentActiv no_page.text = noItemsString page_switch.text = switchString - page_switch.visibility = switchVisibility page_switch.isChecked = switchIsChecked compositeDisposable.add(pageViewModel.effects.subscribe { it.invokeWith(activity) }) page_switch.setOnCheckedChangeListener { _, isChecked -> diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksFragment.kt index 0342174ad..7dd278a5b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksFragment.kt @@ -1,6 +1,5 @@ package org.kiwix.kiwixmobile.core.page.bookmark -import android.view.View import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent @@ -16,7 +15,6 @@ class BookmarksFragment : PageFragment() { override val pageAdapter: PageAdapter by lazy { PageAdapter(PageItemDelegate(this)) } - override val switchVisibility: Int = View.VISIBLE override val screenTitle: String by lazy { getString(R.string.bookmarks) } override val noItemsString: String by lazy { getString(R.string.no_bookmarks) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryFragment.kt index 86f7f4344..0ed619f2f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryFragment.kt @@ -1,6 +1,5 @@ package org.kiwix.kiwixmobile.core.page.history -import android.view.View import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent @@ -19,7 +18,6 @@ class HistoryFragment : PageFragment() { override val pageAdapter by lazy { PageAdapter(PageItemDelegate(this), HistoryDateDelegate()) } - override val switchVisibility: Int = View.VISIBLE override val noItemsString: String by lazy { getString(R.string.no_history) } override val switchString: String by lazy { getString(R.string.history_from_current_book) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt index 550f1a500..5f4a93df7 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/NotesFragment.kt @@ -18,7 +18,6 @@ package org.kiwix.kiwixmobile.core.page.notes -import android.view.View import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent @@ -37,11 +36,10 @@ class NotesFragment : PageFragment() { override val pageAdapter: PageAdapter by lazy { PageAdapter(PageDelegate.PageItemDelegate(this)) } - override val switchVisibility: Int = View.GONE override val noItemsString: String by lazy { getString(R.string.no_notes) } override val switchString: String by lazy { getString(R.string.notes_from_all_books) } - override val switchIsChecked: Boolean by lazy(sharedPreferenceUtil::showNotesAllBooks) + override val switchIsChecked: Boolean = true override fun inject(baseActivity: BaseActivity) { baseActivity.cachedComponent.inject(this) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/TestingUtils.java b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/TestingUtils.java index 3a69873c6..dc84a9d49 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/TestingUtils.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/TestingUtils.java @@ -28,7 +28,7 @@ public class TestingUtils { private static TestingUtils.IdleListener callback; - private static final Set resources = new HashSet<>(); + private static Set resources = new HashSet<>(); public static void bindResource(Class bindClass) { if (callback != null) { From b2f2735c8c7c20dd6dc011b98b08f5ce9885bad5 Mon Sep 17 00:00:00 2001 From: 4shutosh <4shutoshsingh@gmail.com> Date: Tue, 28 Jun 2022 13:43:42 +0530 Subject: [PATCH 05/10] note save and title: fixes --- .../kiwix/kiwixmobile/core/dao/NewNoteDao.kt | 8 ++-- .../kiwixmobile/core/main/AddNoteDialog.kt | 44 +++++++++---------- .../effects/ShowDeleteNotesDialog.kt | 3 +- .../viewmodel/effects/ShowOpenNoteDialog.kt | 2 +- .../core/page/viewmodel/effects/OpenNote.kt | 7 +-- core/src/main/res/layout/fragment_page.xml | 5 +-- 6 files changed, 34 insertions(+), 35 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt index 05009a1f1..39f88c662 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt @@ -28,9 +28,11 @@ import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem import javax.inject.Inject class NewNoteDao @Inject constructor(val box: Box) : PageDao { - fun notes(): Flowable> = box.asFlowable(box.query { - order(NotesEntity_.noteTitle) - }).map { it.map(::NoteListItem) } + fun notes(): Flowable> = box.asFlowable( + box.query { + order(NotesEntity_.noteTitle) + } + ).map { it.map(::NoteListItem) } override fun pages(): Flowable> = notes() diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt index fb1fb2735..6bf125b97 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt @@ -118,9 +118,14 @@ class AddNoteDialog : DialogFragment() { zimFavicon = zimReaderContainer.favicon zimId = zimReaderContainer.id - val webView = (activity as WebViewProvider?)?.getCurrentWebView() - articleTitle = webView?.title - zimFileUrl = webView?.url + if (arguments != null) { + articleTitle = arguments?.getString(NOTES_TITLE) + zimFileUrl = arguments?.getString(ARTICLE_URL) + } else { + val webView = (activity as WebViewProvider?)?.getCurrentWebView() + articleTitle = webView?.title + zimFileUrl = webView?.url + } // Corresponds to "ZimFileName" of "{External Storage}/Kiwix/Notes/ZimFileName/ArticleUrl.txt" articleNoteFileName = getArticleNoteFileName() @@ -152,18 +157,18 @@ class AddNoteDialog : DialogFragment() { private fun getArticleNoteFileName(): String { // Returns url of the form: "content://org.kiwix.kiwixmobile.zim.base/A/Main_Page.html" - val articleUrl = if (arguments != null) { - arguments?.getString(ARTICLE_URL) - } else { - (activity as WebViewProvider?)?.getCurrentWebView()?.url + if (arguments != null && arguments?.getString(NOTE_FILE_PATH) != null) { + return getTextAfterLastSlashWithoutExtension(arguments?.getString(NOTE_FILE_PATH)!!) } + + val articleUrl = (activity as WebViewProvider?)?.getCurrentWebView()?.url var noteFileName = "" if (articleUrl == null) { onFailureToCreateAddNoteDialog() } else { noteFileName = getTextAfterLastSlashWithoutExtension(articleUrl) } - return (if (noteFileName.isNotEmpty()) noteFileName else articleTitle) ?: "" + return (noteFileName.ifEmpty { articleTitle }) ?: "" } /* From ".../Kiwix/granbluefantasy_en_all_all_nopic_2018-10.zim", returns "granbluefantasy_en_all_all_nopic_2018-10" @@ -312,7 +317,7 @@ class AddNoteDialog : DialogFragment() { noteEdited = false // As no unsaved changes remain enableDeleteNoteMenuItem() // adding only if saving file is success - addNoteToDao(noteFile.canonicalPath, zimFileTitle.orEmpty()) + addNoteToDao(noteFile.canonicalPath, "${zimFileTitle.orEmpty()}: $articleTitle") } catch (e: IOException) { e.printStackTrace() .also { context.toast(R.string.note_save_unsuccessful, Toast.LENGTH_LONG) } @@ -328,12 +333,12 @@ class AddNoteDialog : DialogFragment() { private fun addNoteToDao(noteFilePath: String?, title: String) { noteFilePath?.let { filePath -> - if (filePath.isNotEmpty() && zimFileUrl.orEmpty().isNotEmpty()) { + if (filePath.isNotEmpty() && zimFileUrl?.isNotEmpty() == true) { val zimReader = zimReaderContainer.zimFileReader if (zimReader != null) { val noteToSave = NoteListItem( title = title, - url = zimFileUrl.orEmpty(), + url = zimFileUrl!!, noteFilePath = noteFilePath, zimFileReader = zimReader ) @@ -367,19 +372,9 @@ class AddNoteDialog : DialogFragment() { * is displayed in the EditText field (note content area) */ private fun displayNote() { - var noteFilePath: String? = "" - noteFilePath = if (arguments != null) { - arguments?.getString(NOTE_FILE_PATH) - } else { - "$zimNotesDirectory$articleNoteFileName.txt" - } - if (noteFilePath != null && noteFilePath.isNotEmpty()) { - val noteFile = File(noteFilePath) - if (noteFile.exists()) { - readNoteFromFile(noteFile) - } else { - onFailureToCreateAddNoteDialog() - } + val noteFile = File("$zimNotesDirectory$articleNoteFileName.txt") + if (noteFile.exists()) { + readNoteFromFile(noteFile) } // No action in case the note file for the currently open article doesn't exist @@ -451,5 +446,6 @@ class AddNoteDialog : DialogFragment() { const val TAG = "AddNoteDialog" const val NOTE_FILE_PATH = "NoteFilePath" const val ARTICLE_URL = "ArticleUrl" + const val NOTES_TITLE = "NotesTitle" } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt index 00db5ce9d..957c4ded2 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowDeleteNotesDialog.kt @@ -44,6 +44,7 @@ data class ShowDeleteNotesDialog( if (state.isInSelectionState) DeleteSelectedNotes else DeleteAllNotes, { effects.offer(DeletePageItems(state, pageDao)) - }) + } + ) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt index f78886d2b..77873906d 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/notes/viewmodel/effects/ShowOpenNoteDialog.kt @@ -47,7 +47,7 @@ data class ShowOpenNoteDialog( val item = page as NoteListItem val file = File(item.zimFilePath.orEmpty()) zimReaderContainer.setZimFile(file) - effects.offer(OpenNote(item.noteFilePath, item.zimUrl)) + effects.offer(OpenNote(item.noteFilePath, item.zimUrl, item.title)) } ) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt index 55cf0102b..b3cab3597 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt @@ -23,19 +23,19 @@ import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction import org.kiwix.kiwixmobile.core.base.SideEffect -import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.popNavigationBackstack import org.kiwix.kiwixmobile.core.main.AddNoteDialog import org.kiwix.kiwixmobile.core.main.AddNoteDialog.Companion.NOTE_FILE_PATH import org.kiwix.kiwixmobile.core.main.AddNoteDialog.Companion.ARTICLE_URL +import org.kiwix.kiwixmobile.core.main.AddNoteDialog.Companion.NOTES_TITLE import org.kiwix.kiwixmobile.core.main.CoreMainActivity class OpenNote( private val noteFilePath: String, - private val zimFileUrl: String + private val zimFileUrl: String, + private val title: String, ) : SideEffect { override fun invokeWith(activity: AppCompatActivity) { activity as CoreMainActivity - activity.popNavigationBackstack() showAddNoteDialog(activity) } @@ -51,6 +51,7 @@ class OpenNote( val bundle = Bundle() bundle.putString(NOTE_FILE_PATH, noteFilePath) bundle.putString(ARTICLE_URL, zimFileUrl) + bundle.putString(NOTES_TITLE, title) dialogFragment.arguments = bundle dialogFragment.show(fragmentTransaction, AddNoteDialog.TAG) } diff --git a/core/src/main/res/layout/fragment_page.xml b/core/src/main/res/layout/fragment_page.xml index 4ef25707f..bdb365b42 100644 --- a/core/src/main/res/layout/fragment_page.xml +++ b/core/src/main/res/layout/fragment_page.xml @@ -13,7 +13,6 @@ app:layout_constraintTop_toTopOf="parent" tools:showIn="@layout/fragment_help"> - + app:layout_constraintTop_toBottomOf="@id/app_bar" + tools:listitem="@layout/item_bookmark_history" /> From 1932e87e0211f0eb28f4359eb0fb00c33087b246 Mon Sep 17 00:00:00 2001 From: 4shutosh <4shutoshsingh@gmail.com> Date: Tue, 28 Jun 2022 14:04:56 +0530 Subject: [PATCH 06/10] unnecessary comment removed --- .../src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt index a0fe1d649..a1e52be12 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt @@ -134,7 +134,7 @@ class Repository @Inject internal constructor( Completable.fromAction { notesDao.deleteNote(noteUniqueKey) } .subscribeOn(io) - // this does note removes notes from storage only the list : remove txt files as well? + // this removes notes from storage only: remove txt files as well? override fun clearNotes(): Completable = Completable.fromAction(notesDao::deleteAllNotes).subscribeOn(io) } From 2548091e09ff156fed8e0d55128890f836898f4c Mon Sep 17 00:00:00 2001 From: 4shutosh <4shutoshsingh@gmail.com> Date: Sun, 3 Jul 2022 16:30:00 +0530 Subject: [PATCH 07/10] code cleanup --- config/detekt/detekt.yml | 5 ++--- .../kiwixmobile/core/main/AddNoteDialog.kt | 20 +++++++++---------- .../core/page/viewmodel/effects/OpenNote.kt | 17 ++++++++-------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml index 64c1c4e16..45ba1cf73 100644 --- a/config/detekt/detekt.yml +++ b/config/detekt/detekt.yml @@ -78,9 +78,8 @@ complexity: active: true threshold: 60 LongParameterList: - active: false - functionThreshold: 9 - constructorThreshold: 9 + active: true + threshold: 6 ignoreDefaultParameters: false MethodOverloading: active: false diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt index 6bf125b97..37c74af59 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt @@ -67,11 +67,10 @@ const val DISABLE_ICON_ITEM_ALPHA = 130 const val ENABLE_ICON_ITEM_ALPHA = 255 class AddNoteDialog : DialogFragment() { - private var zimId: String? = null + private lateinit var zimId: String private var zimFileName: String? = null private var zimFileTitle: String? = null - private var zimFileUrl: String? = null - private var zimFavicon: String? = null + private lateinit var zimFileUrl: String private var articleTitle: String? = null // Corresponds to "ArticleUrl" of "{External Storage}/Kiwix/Notes/ZimFileName/ArticleUrl.txt" @@ -115,16 +114,15 @@ class AddNoteDialog : DialogFragment() { zimFileName = zimReaderContainer.zimCanonicalPath if (zimFileName != null) { // No zim file currently opened zimFileTitle = zimReaderContainer.zimFileTitle - zimFavicon = zimReaderContainer.favicon - zimId = zimReaderContainer.id + zimId = zimReaderContainer.id.orEmpty() if (arguments != null) { articleTitle = arguments?.getString(NOTES_TITLE) - zimFileUrl = arguments?.getString(ARTICLE_URL) + zimFileUrl = arguments?.getString(ARTICLE_URL).orEmpty() } else { val webView = (activity as WebViewProvider?)?.getCurrentWebView() articleTitle = webView?.title - zimFileUrl = webView?.url + zimFileUrl = webView?.url.orEmpty() } // Corresponds to "ZimFileName" of "{External Storage}/Kiwix/Notes/ZimFileName/ArticleUrl.txt" @@ -157,8 +155,8 @@ class AddNoteDialog : DialogFragment() { private fun getArticleNoteFileName(): String { // Returns url of the form: "content://org.kiwix.kiwixmobile.zim.base/A/Main_Page.html" - if (arguments != null && arguments?.getString(NOTE_FILE_PATH) != null) { - return getTextAfterLastSlashWithoutExtension(arguments?.getString(NOTE_FILE_PATH)!!) + arguments?.getString(NOTE_FILE_PATH)?.let { + return@getArticleNoteFileName getTextAfterLastSlashWithoutExtension(it) } val articleUrl = (activity as WebViewProvider?)?.getCurrentWebView()?.url @@ -333,12 +331,12 @@ class AddNoteDialog : DialogFragment() { private fun addNoteToDao(noteFilePath: String?, title: String) { noteFilePath?.let { filePath -> - if (filePath.isNotEmpty() && zimFileUrl?.isNotEmpty() == true) { + if (filePath.isNotEmpty() && zimFileUrl.isNotEmpty()) { val zimReader = zimReaderContainer.zimFileReader if (zimReader != null) { val noteToSave = NoteListItem( title = title, - url = zimFileUrl!!, + url = zimFileUrl, noteFilePath = noteFilePath, zimFileReader = zimReader ) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt index b3cab3597..fe15a416a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/viewmodel/effects/OpenNote.kt @@ -24,9 +24,9 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction import org.kiwix.kiwixmobile.core.base.SideEffect import org.kiwix.kiwixmobile.core.main.AddNoteDialog -import org.kiwix.kiwixmobile.core.main.AddNoteDialog.Companion.NOTE_FILE_PATH import org.kiwix.kiwixmobile.core.main.AddNoteDialog.Companion.ARTICLE_URL import org.kiwix.kiwixmobile.core.main.AddNoteDialog.Companion.NOTES_TITLE +import org.kiwix.kiwixmobile.core.main.AddNoteDialog.Companion.NOTE_FILE_PATH import org.kiwix.kiwixmobile.core.main.CoreMainActivity class OpenNote( @@ -40,18 +40,19 @@ class OpenNote( } private fun showAddNoteDialog(activity: AppCompatActivity) { - val act: CoreMainActivity = activity as CoreMainActivity + val coreMainActivity: CoreMainActivity = activity as CoreMainActivity val fragmentTransaction: FragmentTransaction = - act.supportFragmentManager.beginTransaction() + coreMainActivity.supportFragmentManager.beginTransaction() val previousInstance: Fragment? = - act.supportFragmentManager.findFragmentByTag(AddNoteDialog.TAG) + coreMainActivity.supportFragmentManager.findFragmentByTag(AddNoteDialog.TAG) if (previousInstance == null) { val dialogFragment = AddNoteDialog() - val bundle = Bundle() - bundle.putString(NOTE_FILE_PATH, noteFilePath) - bundle.putString(ARTICLE_URL, zimFileUrl) - bundle.putString(NOTES_TITLE, title) + val bundle = Bundle().apply { + putString(NOTE_FILE_PATH, noteFilePath) + putString(ARTICLE_URL, zimFileUrl) + putString(NOTES_TITLE, title) + } dialogFragment.arguments = bundle dialogFragment.show(fragmentTransaction, AddNoteDialog.TAG) } From f6cd54627f8fc2fc1fb86132f663b5c3fa34ee58 Mon Sep 17 00:00:00 2001 From: 4shutosh <4shutoshsingh@gmail.com> Date: Sun, 3 Jul 2022 16:47:48 +0530 Subject: [PATCH 08/10] detekt issues fix misc --- .../src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt | 1 + .../main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt index a1e52be12..624588fa1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt @@ -47,6 +47,7 @@ import javax.inject.Singleton * A central repository of data which should provide the presenters with the required data. */ +@Suppress("LongParameterList") @Singleton class Repository @Inject internal constructor( @param:IO private val io: Scheduler, diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt index 37c74af59..a47b294ee 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt @@ -166,7 +166,7 @@ class AddNoteDialog : DialogFragment() { } else { noteFileName = getTextAfterLastSlashWithoutExtension(articleUrl) } - return (noteFileName.ifEmpty { articleTitle }) ?: "" + return noteFileName.ifEmpty { articleTitle } ?: "" } /* From ".../Kiwix/granbluefantasy_en_all_all_nopic_2018-10.zim", returns "granbluefantasy_en_all_all_nopic_2018-10" From e86725365ee4190961834b034d9766e3ebd6938a Mon Sep 17 00:00:00 2001 From: 4shutosh <4shutoshsingh@gmail.com> Date: Sat, 16 Jul 2022 13:14:35 +0530 Subject: [PATCH 09/10] moved to kotlin --- .../java/org/kiwix/kiwixmobile/core/data/DataSource.kt | 6 ++++++ .../java/org/kiwix/kiwixmobile/core/data/Repository.kt | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataSource.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataSource.kt index db64071d2..697897462 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataSource.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataSource.kt @@ -23,6 +23,7 @@ import io.reactivex.Single import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem import org.kiwix.kiwixmobile.core.zim_manager.Language import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk @@ -46,4 +47,9 @@ interface DataSource { fun deleteBookmarks(bookmarks: List): Completable fun deleteBookmark(bookmarkUrl: String): Completable? fun booksOnDiskAsListItems(): Flowable> + + fun saveNote(noteListItem: NoteListItem): Completable + fun deleteNote(noteUniqueKey: String): Completable + fun deleteNotes(noteList: List): Completable + fun clearNotes(): Completable } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt index 624588fa1..195b2a1ed 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt @@ -75,7 +75,7 @@ class Repository @Inject internal constructor( { current, next -> current.locale.displayName != next.locale.displayName } ) } - .map { it.toList() } + .map(MutableList::toList) override fun saveBooks(books: List) = Completable.fromAction { bookDao.insert(books) } @@ -123,15 +123,15 @@ class Repository @Inject internal constructor( Completable.fromAction { bookmarksDao.deleteBookmark(bookmarkUrl) } .subscribeOn(io) - override fun saveNote(noteListItem: NoteListItem): Completable? = + override fun saveNote(noteListItem: NoteListItem): Completable = Completable.fromAction { notesDao.saveNote(noteListItem) } .subscribeOn(io) - override fun deleteNotes(noteList: MutableList) = + override fun deleteNotes(noteList: List) = Completable.fromAction { notesDao.deleteNotes(noteList) } .subscribeOn(io) - override fun deleteNote(noteUniqueKey: String): Completable? = + override fun deleteNote(noteUniqueKey: String): Completable = Completable.fromAction { notesDao.deleteNote(noteUniqueKey) } .subscribeOn(io) From eb4f078200c2de81161883e7454587ec6b390707 Mon Sep 17 00:00:00 2001 From: 4shutosh <4shutoshsingh@gmail.com> Date: Sat, 16 Jul 2022 13:30:54 +0530 Subject: [PATCH 10/10] tests added for DAO, code cleanup --- .../kiwix/kiwixmobile/core/dao/NewNoteDao.kt | 4 -- .../core/dao/entities/NotesEntity.kt | 5 +- .../kiwix/kiwixmobile/core/data/DataSource.kt | 1 - .../kiwix/kiwixmobile/core/data/Repository.kt | 4 -- .../kiwixmobile/core/dao/NewNoteDaoTest.kt | 65 +++++++++++++++++++ 5 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 core/src/test/java/org/kiwix/kiwixmobile/core/dao/NewNoteDaoTest.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt index 39f88c662..b0f3c5f56 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewNoteDao.kt @@ -52,8 +52,4 @@ class NewNoteDao @Inject constructor(val box: Box) : PageDao { equal(NotesEntity_.noteTitle, noteUniqueKey) }.remove() } - - fun deleteAllNotes() { - box.removeAll() - } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt index f13fa2a75..92af24604 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/entities/NotesEntity.kt @@ -1,6 +1,6 @@ /* * Kiwix Android - * Copyright (c) 2020 Kiwix + * Copyright (c) 2022 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 @@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.core.dao.entities import io.objectbox.annotation.Entity import io.objectbox.annotation.Id +import io.objectbox.annotation.Unique import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem @Entity @@ -28,7 +29,7 @@ data class NotesEntity( val zimId: String, var zimFilePath: String?, val zimUrl: String, - // @Unique(onConflict = ConflictStrategy.REPLACE) add resolve conflict dependency update required + @Unique var noteTitle: String, var noteFilePath: String, var favicon: String? diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataSource.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataSource.kt index 697897462..e9d4b24e8 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataSource.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/DataSource.kt @@ -51,5 +51,4 @@ interface DataSource { fun saveNote(noteListItem: NoteListItem): Completable fun deleteNote(noteUniqueKey: String): Completable fun deleteNotes(noteList: List): Completable - fun clearNotes(): Completable } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt index 195b2a1ed..2aaf489cb 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/data/Repository.kt @@ -134,8 +134,4 @@ class Repository @Inject internal constructor( override fun deleteNote(noteUniqueKey: String): Completable = Completable.fromAction { notesDao.deleteNote(noteUniqueKey) } .subscribeOn(io) - - // this removes notes from storage only: remove txt files as well? - override fun clearNotes(): Completable = - Completable.fromAction(notesDao::deleteAllNotes).subscribeOn(io) } diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/dao/NewNoteDaoTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/dao/NewNoteDaoTest.kt new file mode 100644 index 000000000..c9eed304a --- /dev/null +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/dao/NewNoteDaoTest.kt @@ -0,0 +1,65 @@ +/* + * Kiwix Android + * Copyright (c) 2022 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.dao + +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import io.objectbox.Box +import io.objectbox.query.Query +import io.objectbox.query.QueryBuilder +import org.junit.jupiter.api.Test +import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity +import org.kiwix.kiwixmobile.core.dao.entities.NotesEntity_ +import org.kiwix.kiwixmobile.core.page.adapter.Page +import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem + +internal class NewNoteDaoTest { + + private val notesBox: Box = mockk(relaxed = true) + private val newNotesDao = NewNoteDao(notesBox) + + @Test + fun deletePages() { + val notesItem: NoteListItem = mockk(relaxed = true) + val notesItemList: List = listOf(notesItem) + val pagesToDelete: List = notesItemList + newNotesDao.deletePages(pagesToDelete) + verify { newNotesDao.deleteNotes(notesItemList) } + } + + @Test + fun deleteNotePage() { + val noteTitle = "abNotesTitle" + val queryBuilder: QueryBuilder = mockk(relaxed = true) + every { notesBox.query() } returns queryBuilder + every { queryBuilder.equal(NotesEntity_.noteTitle, noteTitle) } returns queryBuilder + val query: Query = mockk(relaxed = true) + every { queryBuilder.build() } returns query + newNotesDao.deleteNote(noteTitle) + verify { query.remove() } + } + + @Test + fun saveNotePage() { + val newNote: NoteListItem = mockk(relaxed = true) + newNotesDao.saveNote(newNote) + verify { notesBox.put(NotesEntity(newNote)) } + } +}