- fixed style config and code format

This commit is contained in:
Pavol Franek 2020-03-03 09:47:01 +01:00
parent f66c90e495
commit 5c7674744d
2 changed files with 142 additions and 123 deletions

19
.editorconfig Normal file
View File

@ -0,0 +1,19 @@
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided this notice is
# preserved. This file is offered as-is, without any warranty.
# Names of contributors must not be used to endorse or promote products
# derived from this file without specific prior written permission.
# EditorConfig
# http://EditorConfig.org
# top-most EditorConfig file
root = true
# LF end-of-line, insert an empty new line and UTF-8
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 4

View File

@ -23,160 +23,160 @@ import kotlinx.android.synthetic.main.fragment_checklist.view.*
class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
private var noteId = 0L
private var items = ArrayList<ChecklistItem>()
private var note: Note? = null
private var noteId = 0L
private var items = ArrayList<ChecklistItem>()
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) {
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<List<ChecklistItem>>() {}.type
items = Gson().fromJson<ArrayList<ChecklistItem>>(storedNote.value, checklistItemType)
?: ArrayList(1)
} catch (e: Exception) {
migrateCheckListOnFailure(storedNote)
}
try {
val checklistItemType = object : TypeToken<List<ChecklistItem>>() {}.type
items = Gson().fromJson<ArrayList<ChecklistItem>>(storedNote.value, checklistItemType)
?: ArrayList(1)
} catch (e: Exception) {
migrateCheckListOnFailure(storedNote)
}
if (config?.moveUndoneChecklistItems == true) {
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 saveChecklist() {
saveNote()
}
override fun refreshItems() {
setupAdapter()
}
override fun refreshItems() {
setupAdapter()
}
}