mirror of
				https://github.com/SimpleMobileTools/Simple-Notes.git
				synced 2025-06-05 17:00:23 +02:00 
			
		
		
		
	- fixed style config and code format
This commit is contained in:
		| @@ -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() | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Pavol Franek
					Pavol Franek