From f66c90e49581841ae261458a3860af9481b6020c Mon Sep 17 00:00:00 2001 From: Pavol Franek <> Date: Mon, 2 Mar 2020 06:51:30 +0100 Subject: [PATCH] - updated code style format and removed debug extension --- .../notes/pro/extensions/Fragment.kt | 3 - .../notes/pro/fragments/ChecklistFragment.kt | 246 +++++++++--------- 2 files changed, 128 insertions(+), 121 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Fragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Fragment.kt index 70732c47..3524f4f5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Fragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Fragment.kt @@ -2,11 +2,8 @@ package com.simplemobiletools.notes.pro.extensions import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity -import com.simplemobiletools.notes.pro.BuildConfig import com.simplemobiletools.notes.pro.helpers.Config val Fragment.config: Config? get() = if (context != null) Config.newInstance(context!!) else null val Fragment.requiredActivity: FragmentActivity get() = this.activity!! - -val Fragment.isDebug get(): Boolean = BuildConfig.DEBUG \ No newline at end of file diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt index f4d22532..2efc106c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt @@ -23,150 +23,160 @@ import kotlinx.android.synthetic.main.fragment_checklist.view.* class ChecklistFragment : NoteFragment(), ChecklistItemsListener { - private var noteId = 0L - private var items = ArrayList() - private var note: Note? = null + private var noteId = 0L + private var items = ArrayList() + private var note: Note? = null - lateinit var view: ViewGroup + lateinit var view: ViewGroup - val checklistItems get(): String = Gson().toJson(items) + val checklistItems get(): String = Gson().toJson(items) - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - view = inflater.inflate(R.layout.fragment_checklist, container, false) as ViewGroup - noteId = arguments!!.getLong(NOTE_ID, 0L) - return view - } + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + view = inflater.inflate(R.layout.fragment_checklist, container, false) as ViewGroup + noteId = arguments!!.getLong(NOTE_ID, 0L) + return view + } - override fun onResume() { - super.onResume() + override fun onResume() { + super.onResume() - loadNoteById(noteId) - } + loadNoteById(noteId) + } - override fun setMenuVisibility(menuVisible: Boolean) { - super.setMenuVisibility(menuVisible) + override fun setMenuVisibility(menuVisible: Boolean) { + super.setMenuVisibility(menuVisible) - if (menuVisible) activity?.hideKeyboard() - } + if (menuVisible) { + activity?.hideKeyboard() + } + } - private fun loadNoteById(noteId: Long) { - NotesHelper(requiredActivity).getNoteWithId(noteId) { storedNote -> - if (storedNote != null && activity?.isDestroyed == false) { - note = storedNote + private fun loadNoteById(noteId: Long) { + NotesHelper(requiredActivity).getNoteWithId(noteId) { storedNote -> + if (storedNote != null && activity?.isDestroyed == false) { + note = storedNote - try { - val checklistItemType = object: TypeToken>(){}.type - items = Gson().fromJson>(storedNote.value, checklistItemType) ?: ArrayList(1) - } catch (e: Exception) { - migrateCheckListOnFailure(storedNote) - if (isDebug) e.printStackTrace() + try { + val checklistItemType = object : TypeToken>() {}.type + items = Gson().fromJson>(storedNote.value, checklistItemType) + ?: ArrayList(1) + } catch (e: Exception) { + migrateCheckListOnFailure(storedNote) + } + + if (config?.moveUndoneChecklistItems == true) { + items.sortBy { it.isDone } } - if (config?.moveUndoneChecklistItems == true) items.sortBy { it.isDone } - requiredActivity.updateTextColors(view.checklist_holder) - setupFragment() - } - } - } + requiredActivity.updateTextColors(view.checklist_holder) + setupFragment() + } + } + } - private fun migrateCheckListOnFailure(note: Note) { - items.clear() + private fun migrateCheckListOnFailure(note: Note) { + items.clear() - note.value.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEachIndexed { index, value -> - items.add(ChecklistItem( - id = index, - title = value, - isDone = false - )) - } + note.value.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEachIndexed { index, value -> + items.add(ChecklistItem( + id = index, + title = value, + isDone = false + )) + } - saveChecklist() - } + saveChecklist() + } - private fun setupFragment() { - val plusIcon = resources.getColoredDrawableWithColor(R.drawable.ic_plus_vector, if (requiredActivity.isBlackAndWhiteTheme()) Color.BLACK else Color.WHITE) + private fun setupFragment() { + val plusIcon = resources.getColoredDrawableWithColor(R.drawable.ic_plus_vector, if (requiredActivity.isBlackAndWhiteTheme()) Color.BLACK else Color.WHITE) - view.apply { - with(checklist_fab) { - setImageDrawable(plusIcon) - background.applyColorFilter(requiredActivity.getAdjustedPrimaryColor()) - setOnClickListener { - showNewItemDialog() - } - } + view.apply { + with(checklist_fab) { + setImageDrawable(plusIcon) + background.applyColorFilter(requiredActivity.getAdjustedPrimaryColor()) + setOnClickListener { + showNewItemDialog() + } + } - with(fragment_placeholder_2) { - setTextColor(requiredActivity.getAdjustedPrimaryColor()) - underlineText() - setOnClickListener { - showNewItemDialog() - } - } - } + with(fragment_placeholder_2) { + setTextColor(requiredActivity.getAdjustedPrimaryColor()) + underlineText() + setOnClickListener { + showNewItemDialog() + } + } + } - setupAdapter() - } + setupAdapter() + } - private fun showNewItemDialog() { - NewChecklistItemDialog(activity as SimpleActivity) { titles -> - var currentMaxId = items.maxBy { item -> item.id }?.id ?: 0 + private fun showNewItemDialog() { + NewChecklistItemDialog(activity as SimpleActivity) { titles -> + var currentMaxId = items.maxBy { item -> item.id }?.id ?: 0 - titles.forEach { title -> - title.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEach { row -> - items.add(ChecklistItem(currentMaxId + 1, row, false)) - currentMaxId++ - } - } + titles.forEach { title -> + title.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEach { row -> + items.add(ChecklistItem(currentMaxId + 1, row, false)) + currentMaxId++ + } + } - saveNote() - setupAdapter() + saveNote() + setupAdapter() - (view.checklist_list.adapter as? ChecklistAdapter)?.notifyDataSetChanged() - } - } + (view.checklist_list.adapter as? ChecklistAdapter)?.notifyDataSetChanged() + } + } - private fun setupAdapter() { - with(view) { - fragment_placeholder.beVisibleIf(items.isEmpty()) - fragment_placeholder_2.beVisibleIf(items.isEmpty()) - checklist_list.beVisibleIf(items.isNotEmpty()) - } + private fun setupAdapter() { + with(view) { + fragment_placeholder.beVisibleIf(items.isEmpty()) + fragment_placeholder_2.beVisibleIf(items.isEmpty()) + checklist_list.beVisibleIf(items.isNotEmpty()) + } - ChecklistAdapter( - activity = activity as SimpleActivity, - items = items, - listener = this, - recyclerView = view.checklist_list, - showIcons = true - ) { item -> - val clickedNote = item as ChecklistItem - clickedNote.isDone = !clickedNote.isDone + ChecklistAdapter( + activity = activity as SimpleActivity, + items = items, + listener = this, + recyclerView = view.checklist_list, + showIcons = true + ) { item -> + val clickedNote = item as ChecklistItem + clickedNote.isDone = !clickedNote.isDone - saveNote(items.indexOfFirst { it.id == clickedNote.id }) - context?.updateWidgets() - }.apply { - view.checklist_list.adapter = this - } - } + saveNote(items.indexOfFirst { it.id == clickedNote.id }) + context?.updateWidgets() + }.apply { + view.checklist_list.adapter = this + } + } - private fun saveNote(refreshIndex: Int = -1) { - ensureBackgroundThread { - context?.let { ctx -> - note?.let { currentNote -> - if (refreshIndex != -1) { - view.checklist_list.post { - view.checklist_list.adapter?.notifyItemChanged(refreshIndex) - } - } + private fun saveNote(refreshIndex: Int = -1) { + ensureBackgroundThread { + context?.let { ctx -> + note?.let { currentNote -> + if (refreshIndex != -1) { + view.checklist_list.post { + view.checklist_list.adapter?.notifyItemChanged(refreshIndex) + } + } - currentNote.value = checklistItems - ctx.notesDB.insertOrUpdate(currentNote) - ctx.updateWidgets() - } - } - } - } + currentNote.value = checklistItems + ctx.notesDB.insertOrUpdate(currentNote) + ctx.updateWidgets() + } + } + } + } - override fun saveChecklist() { saveNote() } - override fun refreshItems() { setupAdapter() } + override fun saveChecklist() { + saveNote() + } + + override fun refreshItems() { + setupAdapter() + } }