Skip to content

Commit b4314e8

Browse files
committed
Add create a new note as an additional item in the list
1 parent 1965df4 commit b4314e8

File tree

4 files changed

+88
-66
lines changed

4 files changed

+88
-66
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/OpenNoteAdapter.kt

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import com.google.gson.Gson
1111
import com.google.gson.reflect.TypeToken
1212
import com.simplemobiletools.commons.activities.BaseSimpleActivity
1313
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
14+
import com.simplemobiletools.commons.extensions.beGone
15+
import com.simplemobiletools.commons.extensions.beVisible
1416
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
1517
import com.simplemobiletools.commons.extensions.isBlackAndWhiteTheme
1618
import com.simplemobiletools.commons.helpers.LOWER_ALPHA_INT
@@ -29,18 +31,25 @@ class OpenNoteAdapter(
2931
activity: BaseSimpleActivity, var items: List<Note>,
3032
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit
3133
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
34+
private companion object {
35+
const val NEW_NOTE_ID = -1
36+
}
3237

3338
override fun getActionMenuId() = 0
3439

3540
override fun actionItemPressed(id: Int) {}
3641

37-
override fun getSelectableItemCount() = items.size
42+
override fun getSelectableItemCount() = itemCount
3843

3944
override fun getIsItemSelectable(position: Int) = false
4045

41-
override fun getItemSelectionKey(position: Int) = items.getOrNull(position)?.id?.toInt()
46+
override fun getItemSelectionKey(position: Int) = items.getOrNull(position)?.id?.toInt() ?: NEW_NOTE_ID
4247

43-
override fun getItemKeyPosition(key: Int) = items.indexOfFirst { it.id?.toInt() == key }
48+
override fun getItemKeyPosition(key: Int) = if (key == NEW_NOTE_ID) {
49+
items.size
50+
} else {
51+
items.indexOfFirst { it.id?.toInt() == key }
52+
}
4453

4554
override fun onActionModeCreated() {}
4655

@@ -51,44 +60,66 @@ class OpenNoteAdapter(
5160
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.open_note_item, parent)
5261

5362
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
54-
val item = items[position]
55-
holder.bindView(item, true, false) { itemView, layoutPosition ->
56-
setupView(itemView, item)
63+
if (position == items.size) {
64+
holder.bindView(NEW_NOTE_ID, true, false) { itemView, layoutPosition ->
65+
setupNewNoteView(itemView)
66+
}
67+
} else {
68+
val item = items[position]
69+
holder.bindView(item, true, false) { itemView, layoutPosition ->
70+
setupView(itemView, item)
71+
}
5772
}
5873
bindViewHolder(holder)
5974
}
6075

61-
override fun getItemCount() = items.size
76+
override fun getItemCount() = items.size + 1
6277

6378
private fun setupView(view: View, note: Note) {
6479
view.apply {
65-
if (context.isBlackAndWhiteTheme()) {
66-
open_note_item_holder.setBackgroundResource(R.drawable.black_dialog_background)
67-
} else {
68-
val cardBackgroundColor = if (backgroundColor == Color.BLACK) {
69-
Color.WHITE
70-
} else {
71-
Color.BLACK
72-
}
73-
val cardBackground = if (context.config.isUsingSystemTheme) {
74-
R.drawable.dialog_you_background
75-
} else {
76-
R.drawable.dialog_bg
77-
}
78-
open_note_item_holder.background =
79-
activity.resources.getColoredDrawableWithColor(cardBackground, cardBackgroundColor, LOWER_ALPHA_INT)
80-
}
80+
setupCard()
8181
open_note_item_title.apply {
8282
text = note.title
8383
setTextColor(properPrimaryColor)
8484
}
85+
open_note_item_text.beVisible()
8586
open_note_item_text.apply {
8687
text = note.getFormattedValue(context)
8788
setTextColor(textColor)
8889
}
8990
}
9091
}
9192

93+
private fun setupNewNoteView(view: View) {
94+
view.apply {
95+
setupCard()
96+
open_note_item_title.apply {
97+
setText(R.string.create_new_note)
98+
setTextColor(properPrimaryColor)
99+
}
100+
open_note_item_text.beGone()
101+
}
102+
}
103+
104+
private fun View.setupCard() {
105+
if (context.isBlackAndWhiteTheme()) {
106+
open_note_item_holder.setBackgroundResource(R.drawable.black_dialog_background)
107+
} else {
108+
val cardBackgroundColor = if (backgroundColor == Color.BLACK) {
109+
Color.WHITE
110+
} else {
111+
Color.BLACK
112+
}
113+
val cardBackground = if (context.config.isUsingSystemTheme) {
114+
R.drawable.dialog_you_background
115+
} else {
116+
R.drawable.dialog_bg
117+
}
118+
open_note_item_holder.background =
119+
activity.resources.getColoredDrawableWithColor(cardBackground, cardBackgroundColor, LOWER_ALPHA_INT)
120+
}
121+
}
122+
92123
private fun Note.getFormattedValue(context: Context): CharSequence? {
93124
return when (type) {
94125
NoteType.TYPE_TEXT -> getNoteStoredValue(context)

app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenNoteDialog.kt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,14 @@ import com.simplemobiletools.notes.pro.adapters.OpenNoteAdapter
1212
import com.simplemobiletools.notes.pro.helpers.NotesHelper
1313
import com.simplemobiletools.notes.pro.models.Note
1414
import kotlinx.android.synthetic.main.dialog_open_note.view.dialog_open_note_list
15-
import kotlinx.android.synthetic.main.dialog_open_note.view.dialog_open_note_new_radio
1615

1716
class OpenNoteDialog(val activity: BaseSimpleActivity, val callback: (checkedId: Long, newNote: Note?) -> Unit) {
1817
private var dialog: AlertDialog? = null
1918

2019
init {
2120
val view = activity.layoutInflater.inflate(R.layout.dialog_open_note, null)
22-
view.dialog_open_note_new_radio.setOnClickListener {
23-
view.dialog_open_note_new_radio.isChecked = false
24-
NewNoteDialog(activity, setChecklistAsDefault = false) {
25-
callback(0, it)
26-
dialog?.dismiss()
27-
}
28-
}
2921

3022
val noteItemWidth = activity.resources.getDimensionPixelSize(R.dimen.grid_note_item_width)
31-
3223
view.dialog_open_note_list.layoutManager = AutoStaggeredGridLayoutManager(noteItemWidth, StaggeredGridLayoutManager.VERTICAL)
3324

3425
NotesHelper(activity).getNotes {
@@ -38,14 +29,24 @@ class OpenNoteDialog(val activity: BaseSimpleActivity, val callback: (checkedId:
3829

3930
private fun initDialog(notes: List<Note>, view: View) {
4031
view.dialog_open_note_list.adapter = OpenNoteAdapter(activity, notes, view.dialog_open_note_list) {
41-
callback((it as Note).id!!, null)
42-
dialog?.dismiss()
32+
if (it is Note) {
33+
callback(it.id!!, null)
34+
dialog?.dismiss()
35+
} else {
36+
// New note
37+
NewNoteDialog(activity, setChecklistAsDefault = false) {
38+
callback(0, it)
39+
dialog?.dismiss()
40+
}
41+
}
4342
}
4443

45-
activity.getAlertDialogBuilder().apply {
46-
activity.setupDialogStuff(view, this, R.string.open_note) { alertDialog ->
47-
dialog = alertDialog
44+
activity.getAlertDialogBuilder()
45+
.setNegativeButton(R.string.cancel, null)
46+
.apply {
47+
activity.setupDialogStuff(view, this, R.string.open_note) { alertDialog ->
48+
dialog = alertDialog
49+
}
4850
}
49-
}
5051
}
5152
}
Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,35 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
45
android:id="@+id/dialog_open_note_holder"
56
android:layout_width="match_parent"
67
android:layout_height="wrap_content">
78

8-
<com.simplemobiletools.commons.views.MyRecyclerView
9-
android:id="@+id/dialog_open_note_list"
9+
<androidx.core.widget.NestedScrollView
1010
android:layout_width="match_parent"
11-
android:layout_height="match_parent"
12-
android:layout_above="@+id/dialog_open_note_divider"
13-
android:layout_alignParentTop="true"
11+
android:layout_height="0dp"
12+
app:layout_constraintStart_toStartOf="parent"
13+
app:layout_constraintEnd_toEndOf="parent"
14+
app:layout_constraintTop_toTopOf="parent"
1415
android:layout_marginStart="@dimen/small_margin"
1516
android:layout_marginTop="@dimen/medium_margin"
1617
android:layout_marginEnd="@dimen/small_margin"
17-
tools:itemCount="2"
18-
tools:listitem="@layout/open_note_item" />
18+
app:layout_constraintHeight_max="500dp">
19+
<com.simplemobiletools.commons.views.MyRecyclerView
20+
android:id="@+id/dialog_open_note_list"
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent"
23+
tools:itemCount="10"
24+
tools:listitem="@layout/open_note_item" />
25+
</androidx.core.widget.NestedScrollView>
1926

2027
<ImageView
2128
android:id="@+id/dialog_open_note_divider"
2229
android:layout_width="match_parent"
2330
android:layout_height="1px"
24-
android:layout_above="@+id/dialog_open_note_create_new"
31+
app:layout_constraintBottom_toBottomOf="parent"
2532
android:background="@color/divider_grey"
2633
android:importantForAccessibility="no" />
2734

28-
<RadioGroup
29-
android:id="@+id/dialog_open_note_create_new"
30-
android:layout_width="match_parent"
31-
android:layout_height="wrap_content"
32-
android:layout_alignParentBottom="true"
33-
android:paddingStart="@dimen/normal_margin"
34-
android:paddingTop="@dimen/small_margin"
35-
android:paddingEnd="@dimen/normal_margin"
36-
android:paddingBottom="@dimen/small_margin">
37-
38-
<com.simplemobiletools.commons.views.MyCompatRadioButton
39-
android:id="@+id/dialog_open_note_new_radio"
40-
android:layout_width="match_parent"
41-
android:layout_height="wrap_content"
42-
android:text="@string/create_new_note" />
43-
44-
</RadioGroup>
45-
</RelativeLayout>
35+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/open_note_item.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
android:layout_width="wrap_content"
1515
android:layout_height="wrap_content"
1616
android:ellipsize="end"
17-
android:lines="1"
17+
android:maxLines="2"
1818
android:textSize="@dimen/big_text_size"
1919
android:textStyle="bold"
2020
tools:text="Title" />

0 commit comments

Comments
 (0)