From b4314e8b4e43b57b1b9265628253a34f7db97c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Thu, 27 Jul 2023 11:35:40 +0200 Subject: [PATCH] Add create a new note as an additional item in the list --- .../notes/pro/adapters/OpenNoteAdapter.kt | 77 +++++++++++++------ .../notes/pro/dialogs/OpenNoteDialog.kt | 33 ++++---- app/src/main/res/layout/dialog_open_note.xml | 44 ++++------- app/src/main/res/layout/open_note_item.xml | 2 +- 4 files changed, 89 insertions(+), 67 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/OpenNoteAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/OpenNoteAdapter.kt index 48b826bc..1b1141e7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/OpenNoteAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/OpenNoteAdapter.kt @@ -11,6 +11,8 @@ import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter +import com.simplemobiletools.commons.extensions.beGone +import com.simplemobiletools.commons.extensions.beVisible import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor import com.simplemobiletools.commons.extensions.isBlackAndWhiteTheme import com.simplemobiletools.commons.helpers.LOWER_ALPHA_INT @@ -29,18 +31,25 @@ class OpenNoteAdapter( activity: BaseSimpleActivity, var items: List, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit ) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { + private companion object { + const val NEW_NOTE_ID = -1 + } override fun getActionMenuId() = 0 override fun actionItemPressed(id: Int) {} - override fun getSelectableItemCount() = items.size + override fun getSelectableItemCount() = itemCount override fun getIsItemSelectable(position: Int) = false - override fun getItemSelectionKey(position: Int) = items.getOrNull(position)?.id?.toInt() + override fun getItemSelectionKey(position: Int) = items.getOrNull(position)?.id?.toInt() ?: NEW_NOTE_ID - override fun getItemKeyPosition(key: Int) = items.indexOfFirst { it.id?.toInt() == key } + override fun getItemKeyPosition(key: Int) = if (key == NEW_NOTE_ID) { + items.size + } else { + items.indexOfFirst { it.id?.toInt() == key } + } override fun onActionModeCreated() {} @@ -51,37 +60,29 @@ class OpenNoteAdapter( override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.open_note_item, parent) override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val item = items[position] - holder.bindView(item, true, false) { itemView, layoutPosition -> - setupView(itemView, item) + if (position == items.size) { + holder.bindView(NEW_NOTE_ID, true, false) { itemView, layoutPosition -> + setupNewNoteView(itemView) + } + } else { + val item = items[position] + holder.bindView(item, true, false) { itemView, layoutPosition -> + setupView(itemView, item) + } } bindViewHolder(holder) } - override fun getItemCount() = items.size + override fun getItemCount() = items.size + 1 private fun setupView(view: View, note: Note) { view.apply { - if (context.isBlackAndWhiteTheme()) { - open_note_item_holder.setBackgroundResource(R.drawable.black_dialog_background) - } else { - val cardBackgroundColor = if (backgroundColor == Color.BLACK) { - Color.WHITE - } else { - Color.BLACK - } - val cardBackground = if (context.config.isUsingSystemTheme) { - R.drawable.dialog_you_background - } else { - R.drawable.dialog_bg - } - open_note_item_holder.background = - activity.resources.getColoredDrawableWithColor(cardBackground, cardBackgroundColor, LOWER_ALPHA_INT) - } + setupCard() open_note_item_title.apply { text = note.title setTextColor(properPrimaryColor) } + open_note_item_text.beVisible() open_note_item_text.apply { text = note.getFormattedValue(context) setTextColor(textColor) @@ -89,6 +90,36 @@ class OpenNoteAdapter( } } + private fun setupNewNoteView(view: View) { + view.apply { + setupCard() + open_note_item_title.apply { + setText(R.string.create_new_note) + setTextColor(properPrimaryColor) + } + open_note_item_text.beGone() + } + } + + private fun View.setupCard() { + if (context.isBlackAndWhiteTheme()) { + open_note_item_holder.setBackgroundResource(R.drawable.black_dialog_background) + } else { + val cardBackgroundColor = if (backgroundColor == Color.BLACK) { + Color.WHITE + } else { + Color.BLACK + } + val cardBackground = if (context.config.isUsingSystemTheme) { + R.drawable.dialog_you_background + } else { + R.drawable.dialog_bg + } + open_note_item_holder.background = + activity.resources.getColoredDrawableWithColor(cardBackground, cardBackgroundColor, LOWER_ALPHA_INT) + } + } + private fun Note.getFormattedValue(context: Context): CharSequence? { return when (type) { NoteType.TYPE_TEXT -> getNoteStoredValue(context) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenNoteDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenNoteDialog.kt index abe1401f..d8bf638c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenNoteDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenNoteDialog.kt @@ -12,23 +12,14 @@ import com.simplemobiletools.notes.pro.adapters.OpenNoteAdapter import com.simplemobiletools.notes.pro.helpers.NotesHelper import com.simplemobiletools.notes.pro.models.Note import kotlinx.android.synthetic.main.dialog_open_note.view.dialog_open_note_list -import kotlinx.android.synthetic.main.dialog_open_note.view.dialog_open_note_new_radio class OpenNoteDialog(val activity: BaseSimpleActivity, val callback: (checkedId: Long, newNote: Note?) -> Unit) { private var dialog: AlertDialog? = null init { val view = activity.layoutInflater.inflate(R.layout.dialog_open_note, null) - view.dialog_open_note_new_radio.setOnClickListener { - view.dialog_open_note_new_radio.isChecked = false - NewNoteDialog(activity, setChecklistAsDefault = false) { - callback(0, it) - dialog?.dismiss() - } - } val noteItemWidth = activity.resources.getDimensionPixelSize(R.dimen.grid_note_item_width) - view.dialog_open_note_list.layoutManager = AutoStaggeredGridLayoutManager(noteItemWidth, StaggeredGridLayoutManager.VERTICAL) NotesHelper(activity).getNotes { @@ -38,14 +29,24 @@ class OpenNoteDialog(val activity: BaseSimpleActivity, val callback: (checkedId: private fun initDialog(notes: List, view: View) { view.dialog_open_note_list.adapter = OpenNoteAdapter(activity, notes, view.dialog_open_note_list) { - callback((it as Note).id!!, null) - dialog?.dismiss() - } - - activity.getAlertDialogBuilder().apply { - activity.setupDialogStuff(view, this, R.string.open_note) { alertDialog -> - dialog = alertDialog + if (it is Note) { + callback(it.id!!, null) + dialog?.dismiss() + } else { + // New note + NewNoteDialog(activity, setChecklistAsDefault = false) { + callback(0, it) + dialog?.dismiss() + } } } + + activity.getAlertDialogBuilder() + .setNegativeButton(R.string.cancel, null) + .apply { + activity.setupDialogStuff(view, this, R.string.open_note) { alertDialog -> + dialog = alertDialog + } + } } } diff --git a/app/src/main/res/layout/dialog_open_note.xml b/app/src/main/res/layout/dialog_open_note.xml index 659374c1..5f9e039f 100644 --- a/app/src/main/res/layout/dialog_open_note.xml +++ b/app/src/main/res/layout/dialog_open_note.xml @@ -1,45 +1,35 @@ - - + app:layout_constraintHeight_max="500dp"> + + - - - - - - + diff --git a/app/src/main/res/layout/open_note_item.xml b/app/src/main/res/layout/open_note_item.xml index f317a23f..bbb61d32 100644 --- a/app/src/main/res/layout/open_note_item.xml +++ b/app/src/main/res/layout/open_note_item.xml @@ -14,7 +14,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" - android:lines="1" + android:maxLines="2" android:textSize="@dimen/big_text_size" android:textStyle="bold" tools:text="Title" />