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] 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" />