mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-04-02 18:00:12 +02:00
- updated code style format and removed debug extension
This commit is contained in:
parent
7b12650db9
commit
f66c90e495
app/src/main/kotlin/com/simplemobiletools/notes/pro
@ -2,11 +2,8 @@ package com.simplemobiletools.notes.pro.extensions
|
|||||||
|
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.FragmentActivity
|
import androidx.fragment.app.FragmentActivity
|
||||||
import com.simplemobiletools.notes.pro.BuildConfig
|
|
||||||
import com.simplemobiletools.notes.pro.helpers.Config
|
import com.simplemobiletools.notes.pro.helpers.Config
|
||||||
|
|
||||||
val Fragment.config: Config? get() = if (context != null) Config.newInstance(context!!) else null
|
val Fragment.config: Config? get() = if (context != null) Config.newInstance(context!!) else null
|
||||||
|
|
||||||
val Fragment.requiredActivity: FragmentActivity get() = this.activity!!
|
val Fragment.requiredActivity: FragmentActivity get() = this.activity!!
|
||||||
|
|
||||||
val Fragment.isDebug get(): Boolean = BuildConfig.DEBUG
|
|
@ -23,150 +23,160 @@ import kotlinx.android.synthetic.main.fragment_checklist.view.*
|
|||||||
|
|
||||||
class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
|
class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
|
||||||
|
|
||||||
private var noteId = 0L
|
private var noteId = 0L
|
||||||
private var items = ArrayList<ChecklistItem>()
|
private var items = ArrayList<ChecklistItem>()
|
||||||
private var note: Note? = null
|
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? {
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||||
view = inflater.inflate(R.layout.fragment_checklist, container, false) as ViewGroup
|
view = inflater.inflate(R.layout.fragment_checklist, container, false) as ViewGroup
|
||||||
noteId = arguments!!.getLong(NOTE_ID, 0L)
|
noteId = arguments!!.getLong(NOTE_ID, 0L)
|
||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
|
|
||||||
loadNoteById(noteId)
|
loadNoteById(noteId)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setMenuVisibility(menuVisible: Boolean) {
|
override fun setMenuVisibility(menuVisible: Boolean) {
|
||||||
super.setMenuVisibility(menuVisible)
|
super.setMenuVisibility(menuVisible)
|
||||||
|
|
||||||
if (menuVisible) activity?.hideKeyboard()
|
if (menuVisible) {
|
||||||
}
|
activity?.hideKeyboard()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun loadNoteById(noteId: Long) {
|
private fun loadNoteById(noteId: Long) {
|
||||||
NotesHelper(requiredActivity).getNoteWithId(noteId) { storedNote ->
|
NotesHelper(requiredActivity).getNoteWithId(noteId) { storedNote ->
|
||||||
if (storedNote != null && activity?.isDestroyed == false) {
|
if (storedNote != null && activity?.isDestroyed == false) {
|
||||||
note = storedNote
|
note = storedNote
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val checklistItemType = object: TypeToken<List<ChecklistItem>>(){}.type
|
val checklistItemType = object : TypeToken<List<ChecklistItem>>() {}.type
|
||||||
items = Gson().fromJson<ArrayList<ChecklistItem>>(storedNote.value, checklistItemType) ?: ArrayList(1)
|
items = Gson().fromJson<ArrayList<ChecklistItem>>(storedNote.value, checklistItemType)
|
||||||
} catch (e: Exception) {
|
?: ArrayList(1)
|
||||||
migrateCheckListOnFailure(storedNote)
|
} catch (e: Exception) {
|
||||||
if (isDebug) e.printStackTrace()
|
migrateCheckListOnFailure(storedNote)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config?.moveUndoneChecklistItems == true) {
|
||||||
|
items.sortBy { it.isDone }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config?.moveUndoneChecklistItems == true) items.sortBy { it.isDone }
|
requiredActivity.updateTextColors(view.checklist_holder)
|
||||||
requiredActivity.updateTextColors(view.checklist_holder)
|
setupFragment()
|
||||||
setupFragment()
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private fun migrateCheckListOnFailure(note: Note) {
|
private fun migrateCheckListOnFailure(note: Note) {
|
||||||
items.clear()
|
items.clear()
|
||||||
|
|
||||||
note.value.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEachIndexed { index, value ->
|
note.value.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEachIndexed { index, value ->
|
||||||
items.add(ChecklistItem(
|
items.add(ChecklistItem(
|
||||||
id = index,
|
id = index,
|
||||||
title = value,
|
title = value,
|
||||||
isDone = false
|
isDone = false
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
saveChecklist()
|
saveChecklist()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupFragment() {
|
private fun setupFragment() {
|
||||||
val plusIcon = resources.getColoredDrawableWithColor(R.drawable.ic_plus_vector, if (requiredActivity.isBlackAndWhiteTheme()) Color.BLACK else Color.WHITE)
|
val plusIcon = resources.getColoredDrawableWithColor(R.drawable.ic_plus_vector, if (requiredActivity.isBlackAndWhiteTheme()) Color.BLACK else Color.WHITE)
|
||||||
|
|
||||||
view.apply {
|
view.apply {
|
||||||
with(checklist_fab) {
|
with(checklist_fab) {
|
||||||
setImageDrawable(plusIcon)
|
setImageDrawable(plusIcon)
|
||||||
background.applyColorFilter(requiredActivity.getAdjustedPrimaryColor())
|
background.applyColorFilter(requiredActivity.getAdjustedPrimaryColor())
|
||||||
setOnClickListener {
|
setOnClickListener {
|
||||||
showNewItemDialog()
|
showNewItemDialog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
with(fragment_placeholder_2) {
|
with(fragment_placeholder_2) {
|
||||||
setTextColor(requiredActivity.getAdjustedPrimaryColor())
|
setTextColor(requiredActivity.getAdjustedPrimaryColor())
|
||||||
underlineText()
|
underlineText()
|
||||||
setOnClickListener {
|
setOnClickListener {
|
||||||
showNewItemDialog()
|
showNewItemDialog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setupAdapter()
|
setupAdapter()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showNewItemDialog() {
|
private fun showNewItemDialog() {
|
||||||
NewChecklistItemDialog(activity as SimpleActivity) { titles ->
|
NewChecklistItemDialog(activity as SimpleActivity) { titles ->
|
||||||
var currentMaxId = items.maxBy { item -> item.id }?.id ?: 0
|
var currentMaxId = items.maxBy { item -> item.id }?.id ?: 0
|
||||||
|
|
||||||
titles.forEach { title ->
|
titles.forEach { title ->
|
||||||
title.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEach { row ->
|
title.split("\n").map { it.trim() }.filter { it.isNotBlank() }.forEach { row ->
|
||||||
items.add(ChecklistItem(currentMaxId + 1, row, false))
|
items.add(ChecklistItem(currentMaxId + 1, row, false))
|
||||||
currentMaxId++
|
currentMaxId++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
saveNote()
|
saveNote()
|
||||||
setupAdapter()
|
setupAdapter()
|
||||||
|
|
||||||
(view.checklist_list.adapter as? ChecklistAdapter)?.notifyDataSetChanged()
|
(view.checklist_list.adapter as? ChecklistAdapter)?.notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupAdapter() {
|
private fun setupAdapter() {
|
||||||
with(view) {
|
with(view) {
|
||||||
fragment_placeholder.beVisibleIf(items.isEmpty())
|
fragment_placeholder.beVisibleIf(items.isEmpty())
|
||||||
fragment_placeholder_2.beVisibleIf(items.isEmpty())
|
fragment_placeholder_2.beVisibleIf(items.isEmpty())
|
||||||
checklist_list.beVisibleIf(items.isNotEmpty())
|
checklist_list.beVisibleIf(items.isNotEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
ChecklistAdapter(
|
ChecklistAdapter(
|
||||||
activity = activity as SimpleActivity,
|
activity = activity as SimpleActivity,
|
||||||
items = items,
|
items = items,
|
||||||
listener = this,
|
listener = this,
|
||||||
recyclerView = view.checklist_list,
|
recyclerView = view.checklist_list,
|
||||||
showIcons = true
|
showIcons = true
|
||||||
) { item ->
|
) { item ->
|
||||||
val clickedNote = item as ChecklistItem
|
val clickedNote = item as ChecklistItem
|
||||||
clickedNote.isDone = !clickedNote.isDone
|
clickedNote.isDone = !clickedNote.isDone
|
||||||
|
|
||||||
saveNote(items.indexOfFirst { it.id == clickedNote.id })
|
saveNote(items.indexOfFirst { it.id == clickedNote.id })
|
||||||
context?.updateWidgets()
|
context?.updateWidgets()
|
||||||
}.apply {
|
}.apply {
|
||||||
view.checklist_list.adapter = this
|
view.checklist_list.adapter = this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveNote(refreshIndex: Int = -1) {
|
private fun saveNote(refreshIndex: Int = -1) {
|
||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
context?.let { ctx ->
|
context?.let { ctx ->
|
||||||
note?.let { currentNote ->
|
note?.let { currentNote ->
|
||||||
if (refreshIndex != -1) {
|
if (refreshIndex != -1) {
|
||||||
view.checklist_list.post {
|
view.checklist_list.post {
|
||||||
view.checklist_list.adapter?.notifyItemChanged(refreshIndex)
|
view.checklist_list.adapter?.notifyItemChanged(refreshIndex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
currentNote.value = checklistItems
|
currentNote.value = checklistItems
|
||||||
ctx.notesDB.insertOrUpdate(currentNote)
|
ctx.notesDB.insertOrUpdate(currentNote)
|
||||||
ctx.updateWidgets()
|
ctx.updateWidgets()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun saveChecklist() { saveNote() }
|
override fun saveChecklist() {
|
||||||
override fun refreshItems() { setupAdapter() }
|
saveNote()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun refreshItems() {
|
||||||
|
setupAdapter()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user