Add create a new note as an additional item in the list

This commit is contained in:
Ensar Sarajčić 2023-07-27 11:35:40 +02:00
parent 1965df43f5
commit b4314e8b4e
4 changed files with 89 additions and 67 deletions

View File

@ -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<Note>,
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)

View File

@ -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<Note>, 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
}
}
}
}

View File

@ -1,45 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dialog_open_note_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/dialog_open_note_list"
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/dialog_open_note_divider"
android:layout_alignParentTop="true"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="@dimen/small_margin"
android:layout_marginTop="@dimen/medium_margin"
android:layout_marginEnd="@dimen/small_margin"
tools:itemCount="2"
tools:listitem="@layout/open_note_item" />
app:layout_constraintHeight_max="500dp">
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/dialog_open_note_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="10"
tools:listitem="@layout/open_note_item" />
</androidx.core.widget.NestedScrollView>
<ImageView
android:id="@+id/dialog_open_note_divider"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_above="@+id/dialog_open_note_create_new"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/divider_grey"
android:importantForAccessibility="no" />
<RadioGroup
android:id="@+id/dialog_open_note_create_new"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingStart="@dimen/normal_margin"
android:paddingTop="@dimen/small_margin"
android:paddingEnd="@dimen/normal_margin"
android:paddingBottom="@dimen/small_margin">
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/dialog_open_note_new_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/create_new_note" />
</RadioGroup>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -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" />