Skip to content

Commit

Permalink
[enhancement] Dedicated title/description input fields: #22
Browse files Browse the repository at this point in the history
  • Loading branch information
tuancoltech committed Jan 7, 2024
1 parent c2332f0 commit 7de28e6
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import dev.arkbuilders.arkmemo.ui.activities.MainActivity
import dev.arkbuilders.arkmemo.ui.dialogs.NoteDeleteDialog
import dev.arkbuilders.arkmemo.ui.fragments.EditGraphicNotesFragment
import dev.arkbuilders.arkmemo.ui.fragments.EditTextNotesFragment
import dev.arkbuilders.arkmemo.utils.getAutoTitle
import dev.arkbuilders.arkmemo.utils.replaceFragment

class NotesListAdapter(private val notes: List<Note>):
Expand All @@ -38,7 +39,7 @@ class NotesListAdapter(private val notes: List<Note>):
}

override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {
holder.title.text = notes[position].title
holder.title.text = notes[position].getAutoTitle(activity)
holder.date.text = notes[position].resource?.modified?.toString() ?:
activity.getString(R.string.ark_memo_just_now)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.arkbuilders.arkmemo.ui.fragments

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import by.kirich1409.viewbindingdelegate.viewBinding
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.databinding.FragmentEditNotesBinding

open class BaseEditNoteFragment: Fragment(R.layout.fragment_edit_notes) {

val binding by viewBinding(FragmentEditNotesBinding::bind)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.tvDescription.setOnClickListener {
if (binding.editTextDescription.visibility == View.GONE) {
binding.tvDescription.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_chevron_down, 0)
binding.editTextDescription.visibility = View.VISIBLE
} else {
binding.tvDescription.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_chevron_right, 0)
binding.editTextDescription.visibility = View.GONE
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,23 @@ import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.databinding.FragmentEditNotesBinding
import dev.arkbuilders.arkmemo.models.GraphicNote
import dev.arkbuilders.arkmemo.ui.activities.MainActivity
import dev.arkbuilders.arkmemo.ui.viewmodels.GraphicNotesViewModel
import dev.arkbuilders.arkmemo.ui.viewmodels.NotesViewModel
import dev.arkbuilders.arkmemo.utils.observeSaveResult
import java.time.LocalDate
import java.time.format.DateTimeFormatter

@AndroidEntryPoint
class EditGraphicNotesFragment: Fragment(R.layout.fragment_edit_notes) {
class EditGraphicNotesFragment: BaseEditNoteFragment() {

private val activity by lazy {
requireActivity() as MainActivity
}

private val binding by viewBinding(FragmentEditNotesBinding::bind)

private val graphicNotesViewModel: GraphicNotesViewModel by viewModels()
private val notesViewModel: NotesViewModel by activityViewModels()

Expand All @@ -54,10 +47,6 @@ class EditGraphicNotesFragment: Fragment(R.layout.fragment_edit_notes) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val defaultTitle = getString(
R.string.ark_memo_graphic_note,
LocalDate.now().format(DateTimeFormatter.ISO_DATE)
)
var title = note.title
val notesCanvas = binding.notesCanvas
val saveButton = binding.saveNote
Expand All @@ -67,6 +56,9 @@ class EditGraphicNotesFragment: Fragment(R.layout.fragment_edit_notes) {

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
title = s?.toString() ?: ""
if (title.isEmpty()) {
binding.noteTitle.hint = getString(R.string.hint_new_graphical_note)
}
}

override fun afterTextChanged(s: Editable?) {}
Expand All @@ -77,22 +69,25 @@ class EditGraphicNotesFragment: Fragment(R.layout.fragment_edit_notes) {
activity.supportActionBar?.setDisplayHomeAsUpEnabled(true)
activity.showSettingsButton(false)

noteTitle.hint = defaultTitle
noteTitle.hint = getString(R.string.hint_new_graphical_note)
noteTitle.setText(title)
noteTitle.addTextChangedListener(noteTitleChangeListener)
notesCanvas.isVisible = true
notesCanvas.setViewModel(graphicNotesViewModel)
saveButton.setOnClickListener {
val svg = graphicNotesViewModel.svg()
val note = GraphicNote(
title = title.ifEmpty { defaultTitle },
title = binding.noteTitle.text.toString(),
svg = svg,
description = binding.editTextDescription.text.toString(),
resource = note.resource
)
notesViewModel.onSaveClick(note) { show ->
activity.showProgressBar(show)
}
}

binding.editTextDescription.setText(this.note.description)
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,23 @@ import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.ui.viewmodels.NotesViewModel
import dev.arkbuilders.arkmemo.databinding.FragmentEditNotesBinding
import dev.arkbuilders.arkmemo.models.TextNote
import dev.arkbuilders.arkmemo.ui.activities.MainActivity
import dev.arkbuilders.arkmemo.ui.viewmodels.NotesViewModel
import dev.arkbuilders.arkmemo.utils.observeSaveResult
import java.time.LocalDate
import java.time.format.DateTimeFormatter

@AndroidEntryPoint
class EditTextNotesFragment: Fragment(R.layout.fragment_edit_notes) {
class EditTextNotesFragment: BaseEditNoteFragment() {

private val activity: MainActivity by lazy{
requireActivity() as MainActivity
}

private val notesViewModel: NotesViewModel by activityViewModels()

private val binding by viewBinding(FragmentEditNotesBinding::bind)

private var note = TextNote()
private var noteStr: String? = null

Expand All @@ -51,10 +44,6 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_notes) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val defaultTitle = getString(
R.string.ark_memo_text_note,
LocalDate.now().format(DateTimeFormatter.ISO_DATE)
)
var title = this.note.title
var data = note.text
val editTextListener = object: TextWatcher{
Expand All @@ -74,6 +63,9 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_notes) {

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
title = s?.toString() ?: ""
if (title.isEmpty()) {
binding.noteTitle.hint = getString(R.string.hint_new_text_note)
}
}

override fun afterTextChanged(s: Editable?) {}
Expand All @@ -83,7 +75,6 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_notes) {
activity.supportActionBar?.setDisplayHomeAsUpEnabled(true)
activity.showSettingsButton(false)

noteTitle.hint = defaultTitle
noteTitle.setText(this.note.title)
noteTitle.addTextChangedListener(noteTitleChangeListener)
editNote.isVisible = true
Expand All @@ -96,14 +87,17 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_notes) {

saveNoteButton.setOnClickListener {
val note = TextNote(
title = title.ifEmpty { defaultTitle },
title = title,
description = binding.editTextDescription.text.toString(),
text = data,
resource = note.resource
)
notesViewModel.onSaveClick(note) { show ->
activity.showProgressBar(show)
}
}

binding.editTextDescription.setText(this.note.description)
}

companion object{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import android.widget.Button
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import by.kirich1409.viewbindingdelegate.viewBinding
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
import com.google.android.material.floatingactionbutton.FloatingActionButton
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.ui.viewmodels.NotesViewModel
import dev.arkbuilders.arkmemo.databinding.FragmentNotesBinding
Expand Down Expand Up @@ -48,11 +45,13 @@ class NotesFragment: Fragment(R.layout.fragment_notes) {
private val newTextNoteClickListener = View.OnClickListener {
activity.fragment = EditTextNotesFragment()
activity.replaceFragment(activity.fragment, EditTextNotesFragment.TAG)
showFabs = false
}

private val newGraphicNoteClickListener = View.OnClickListener{
activity.fragment = EditGraphicNotesFragment.newInstance()
activity.replaceFragment(activity.fragment, EditGraphicNotesFragment.TAG)
showFabs = false
}

private val pasteNoteClickListener = View.OnClickListener {
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/dev/arkbuilders/arkmemo/utils/NoteExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.arkbuilders.arkmemo.utils

import android.content.Context
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.models.GraphicNote
import dev.arkbuilders.arkmemo.models.Note
import dev.arkbuilders.arkmemo.models.TextNote

fun Note.getAutoTitle(context: Context? = null): String {

return if (this is TextNote) {
this.title.ifEmpty { this.text.take(20) }
} else if (this is GraphicNote && context != null) {
this.title.ifEmpty {
String.format(context.getString(R.string.ark_memo_graphic_note), this.resource?.id)
}
} else {
""
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_chevron_down.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M7.41,8.59L12,13.17l4.59,-4.58L18,10l-6,6 -6,-6 1.41,-1.41z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_chevron_right.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M8.59,16.59L13.17,12 8.59,7.41 10,6l6,6 -6,6 -1.41,-1.41z"/>
</vector>
Loading

0 comments on commit 7de28e6

Please sign in to comment.