removed butterknife and wrap optional

This commit is contained in:
gouri-panda 2020-08-23 05:32:51 +05:30
parent 225cede56e
commit cf882e5520

View File

@ -34,20 +34,18 @@ import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import butterknife.BindView import kotlinx.android.synthetic.main.dialog_add_note.add_note_edit_text
import butterknife.ButterKnife import kotlinx.android.synthetic.main.dialog_add_note.add_note_text_view
import butterknife.Unbinder import kotlinx.android.synthetic.main.dialog_add_note.view.add_note_edit_text
import kotlinx.android.synthetic.main.dialog_add_note.view.add_note_text_view
import kotlinx.android.synthetic.main.layout_toolbar.view.toolbar
import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent
import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance
import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.R2
import org.kiwix.kiwixmobile.core.extensions.toast import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
@ -73,16 +71,6 @@ const val DISABLE_ICON_ITEM_ALPHA = 130
const val ENABLE_ICON_ITEM_ALPHA = 255 const val ENABLE_ICON_ITEM_ALPHA = 255
class AddNoteDialog : DialogFragment() { class AddNoteDialog : DialogFragment() {
@JvmField @BindView(R2.id.toolbar)
var toolbar: Toolbar? = null // Displays options for the note dialog
@JvmField @BindView(R2.id.add_note_text_view)
var addNoteTextView: TextView? = null // Displays article title
@JvmField @BindView(R2.id.add_note_edit_text)
var addNoteEditText: EditText? = null // Displays the note text
private lateinit var unbinder: Unbinder
private var zimFileName: String? = null private var zimFileName: String? = null
private var zimFileTitle: String? = null private var zimFileTitle: String? = null
private var articleTitle: String? = null private var articleTitle: String? = null
@ -92,9 +80,11 @@ class AddNoteDialog : DialogFragment() {
private var noteFileExists = false private var noteFileExists = false
private var noteEdited = false private var noteEdited = false
private lateinit var root: View
// Keeps track of state of the note (whether edited since last save) // Keeps track of state of the note (whether edited since last save)
private var zimNotesDirectory: String? = // Stores path to directory for the currently open zim's notes
null // Stores path to directory for the currently open zim's notes private var zimNotesDirectory: String? = null
@JvmField @Inject @JvmField @Inject
var sharedPreferenceUtil: SharedPreferenceUtil? = null var sharedPreferenceUtil: SharedPreferenceUtil? = null
@ -113,10 +103,10 @@ class AddNoteDialog : DialogFragment() {
.inject(this) .inject(this)
// Returns name of the form ".../Kiwix/granbluefantasy_en_all_all_nopic_2018-10.zim" // Returns name of the form ".../Kiwix/granbluefantasy_en_all_all_nopic_2018-10.zim"
zimFileName = zimReaderContainer!!.zimCanonicalPath zimFileName = zimReaderContainer?.zimCanonicalPath
if (zimFileName != null) { // No zim file currently opened if (zimFileName != null) { // No zim file currently opened
zimFileTitle = zimReaderContainer!!.zimFileTitle zimFileTitle = zimReaderContainer?.zimFileTitle
articleTitle = (activity as WebViewProvider?)!!.getCurrentWebView()?.title articleTitle = (activity as WebViewProvider?)?.getCurrentWebView()?.title
// Corresponds to "ZimFileName" of "{External Storage}/Kiwix/Notes/ZimFileName/ArticleUrl.txt" // Corresponds to "ZimFileName" of "{External Storage}/Kiwix/Notes/ZimFileName/ArticleUrl.txt"
val zimNoteDirectoryName = zimNoteDirectoryName val zimNoteDirectoryName = zimNoteDirectoryName
@ -139,21 +129,20 @@ class AddNoteDialog : DialogFragment() {
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
super.onCreateView(inflater, container, savedInstanceState) super.onCreateView(inflater, container, savedInstanceState)
val view = inflater.inflate(R.layout.dialog_add_note, container, false) root = inflater.inflate(R.layout.dialog_add_note, container, false)
unbinder = ButterKnife.bind(this, view) root.toolbar.setTitle(R.string.note)
toolbar!!.setTitle(R.string.note) root.toolbar.setNavigationIcon(R.drawable.ic_close_white_24dp)
toolbar!!.setNavigationIcon(R.drawable.ic_close_white_24dp) root.toolbar.setNavigationOnClickListener {
toolbar!!.setNavigationOnClickListener {
exitAddNoteDialog() exitAddNoteDialog()
closeKeyboard() closeKeyboard()
} }
toolbar!!.setOnMenuItemClickListener { item: MenuItem -> root.toolbar.setOnMenuItemClickListener { item: MenuItem ->
when (item.itemId) { when (item.itemId) {
R.id.share_note -> { // Opens app-chooser for sharing the note text file R.id.share_note -> { // Opens app-chooser for sharing the note text file
shareNote() shareNote()
} }
R.id.save_note -> { // Saves the note as a text file R.id.save_note -> { // Saves the note as a text file
saveNote(addNoteEditText!!.text.toString()) saveNote(root.add_note_edit_text.text.toString())
} }
R.id.delete_note -> { R.id.delete_note -> {
deleteNote() deleteNote()
@ -161,14 +150,14 @@ class AddNoteDialog : DialogFragment() {
} }
true true
} }
toolbar!!.inflateMenu(R.menu.menu_add_note_dialog) root.toolbar.inflateMenu(R.menu.menu_add_note_dialog)
// 'Share' disabled for empty notes, 'Save' disabled for unedited notes // 'Share' disabled for empty notes, 'Save' disabled for unedited notes
disableMenuItems() disableMenuItems()
addNoteTextView!!.text = articleTitle add_note_text_view.text = articleTitle
// Show the previously saved note if it exists // Show the previously saved note if it exists
displayNote() displayNote()
addNoteEditText!!.addTextChangedListener(object : TextWatcher { add_note_edit_text.addTextChangedListener(object : TextWatcher {
@SuppressWarnings("EmptyFunctionBlock") @SuppressWarnings("EmptyFunctionBlock")
override fun beforeTextChanged( override fun beforeTextChanged(
s: CharSequence, s: CharSequence,
@ -193,7 +182,7 @@ class AddNoteDialog : DialogFragment() {
override fun afterTextChanged(s: Editable) { override fun afterTextChanged(s: Editable) {
} }
}) })
return view return root
} }
private val zimNoteDirectoryName: String private val zimNoteDirectoryName: String
@ -204,7 +193,7 @@ class AddNoteDialog : DialogFragment() {
private fun getArticleNotefileName(): String { private fun getArticleNotefileName(): String {
// Returns url of the form: "content://org.kiwix.kiwixmobile.zim.base/A/Main_Page.html" // Returns url of the form: "content://org.kiwix.kiwixmobile.zim.base/A/Main_Page.html"
val articleUrl = (activity as WebViewProvider?)!!.getCurrentWebView()!!.url val articleUrl = (activity as WebViewProvider?)?.getCurrentWebView()?.url
var noteFileName = "" var noteFileName = ""
if (articleUrl == null) { if (articleUrl == null) {
onFailureToCreateAddNoteDialog() onFailureToCreateAddNoteDialog()
@ -242,7 +231,7 @@ class AddNoteDialog : DialogFragment() {
private fun exitAddNoteDialog() { private fun exitAddNoteDialog() {
if (noteEdited) { if (noteEdited) {
alertDialogShower!!.show( alertDialogShower?.show(
KiwixDialog.NotesDiscardConfirmation, KiwixDialog.NotesDiscardConfirmation,
::dismissAddNoteDialog ::dismissAddNoteDialog
) )
@ -253,10 +242,10 @@ class AddNoteDialog : DialogFragment() {
} }
private fun disableMenuItems() { private fun disableMenuItems() {
if (toolbar!!.menu != null) { if (root.toolbar.menu != null) {
val saveItem = toolbar!!.menu.findItem(R.id.save_note) val saveItem = root.toolbar.menu.findItem(R.id.save_note)
val shareItem = toolbar!!.menu.findItem(R.id.share_note) val shareItem = root.toolbar.menu.findItem(R.id.share_note)
val deleteItem = toolbar!!.menu.findItem(R.id.delete_note) val deleteItem = root.toolbar.menu.findItem(R.id.delete_note)
saveItem.isEnabled = false saveItem.isEnabled = false
shareItem.isEnabled = false shareItem.isEnabled = false
deleteItem.isEnabled = false deleteItem.isEnabled = false
@ -269,8 +258,8 @@ class AddNoteDialog : DialogFragment() {
} }
private fun enableSaveNoteMenuItem() { private fun enableSaveNoteMenuItem() {
if (toolbar!!.menu != null) { if (root.toolbar.menu != null) {
val saveItem = toolbar!!.menu.findItem(R.id.save_note) val saveItem = root.toolbar.menu.findItem(R.id.save_note)
saveItem.isEnabled = true saveItem.isEnabled = true
saveItem.icon.alpha = ENABLE_ICON_ITEM_ALPHA saveItem.icon.alpha = ENABLE_ICON_ITEM_ALPHA
} else { } else {
@ -279,8 +268,8 @@ class AddNoteDialog : DialogFragment() {
} }
private fun enableDeleteNoteMenuItem() { private fun enableDeleteNoteMenuItem() {
if (toolbar!!.menu != null) { if (root.toolbar.menu != null) {
val deleteItem = toolbar!!.menu.findItem(R.id.delete_note) val deleteItem = root.toolbar.menu.findItem(R.id.delete_note)
deleteItem.isEnabled = true deleteItem.isEnabled = true
deleteItem.icon.alpha = ENABLE_ICON_ITEM_ALPHA deleteItem.icon.alpha = ENABLE_ICON_ITEM_ALPHA
} else { } else {
@ -289,8 +278,8 @@ class AddNoteDialog : DialogFragment() {
} }
private fun enableShareNoteMenuItem() { private fun enableShareNoteMenuItem() {
if (toolbar!!.menu != null) { if (root.toolbar.menu != null) {
val shareItem = toolbar!!.menu.findItem(R.id.share_note) val shareItem = root.toolbar.menu.findItem(R.id.share_note)
shareItem.isEnabled = true shareItem.isEnabled = true
shareItem.icon.alpha = ENABLE_ICON_ITEM_ALPHA shareItem.icon.alpha = ENABLE_ICON_ITEM_ALPHA
} else { } else {
@ -305,7 +294,7 @@ class AddNoteDialog : DialogFragment() {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
if (!noteFileExists) { if (!noteFileExists) {
// Prepare for input in case of empty/new note // Prepare for input in case of empty/new note
addNoteEditText!!.requestFocus() root.add_note_edit_text.requestFocus()
showKeyboard() showKeyboard()
} }
} }
@ -380,7 +369,7 @@ class AddNoteDialog : DialogFragment() {
File(notesFolder.absolutePath, "$articleNoteFileName.txt") File(notesFolder.absolutePath, "$articleNoteFileName.txt")
val noteDeleted = noteFile.delete() val noteDeleted = noteFile.delete()
if (noteDeleted) { if (noteDeleted) {
addNoteEditText!!.text.clear() root.add_note_edit_text.text.clear()
disableMenuItems() disableMenuItems()
context.toast(R.string.note_delete_successful, Toast.LENGTH_LONG) context.toast(R.string.note_delete_successful, Toast.LENGTH_LONG)
} else { } else {
@ -417,8 +406,8 @@ class AddNoteDialog : DialogFragment() {
e.printStackTrace() e.printStackTrace()
Log.d(TAG, "Error reading line with BufferedReader") Log.d(TAG, "Error reading line with BufferedReader")
} }
addNoteEditText!!.setText("$contents") // Display the note content root.add_note_edit_text.setText("$contents") // Display the note content
addNoteEditText!!.setSelection(addNoteEditText!!.text.length - 1) root.add_note_edit_text.setSelection(root.add_note_edit_text.text.length - 1)
enableShareNoteMenuItem() // As note content exists which can be shared enableShareNoteMenuItem() // As note content exists which can be shared
enableDeleteNoteMenuItem() enableDeleteNoteMenuItem()
} }
@ -430,7 +419,8 @@ class AddNoteDialog : DialogFragment() {
* is shared via an app-chooser intent * is shared via an app-chooser intent
* */ * */
if (noteEdited) { if (noteEdited) {
saveNote(addNoteEditText!!.text.toString()) // Save edited note before sharing the text file // Save edited note before sharing the text file
saveNote(root.add_note_edit_text.text.toString())
} }
val noteFile = File("$zimNotesDirectory$articleNoteFileName.txt") val noteFile = File("$zimNotesDirectory$articleNoteFileName.txt")
var noteFileUri: Uri? = null var noteFileUri: Uri? = null
@ -479,11 +469,6 @@ class AddNoteDialog : DialogFragment() {
} }
} }
override fun onDestroyView() {
super.onDestroyView()
unbinder.unbind()
}
companion object { companion object {
@JvmField val NOTES_DIRECTORY = @JvmField val NOTES_DIRECTORY =
Environment.getExternalStorageDirectory().toString() + "/Kiwix/Notes/" Environment.getExternalStorageDirectory().toString() + "/Kiwix/Notes/"