Skip to content

Commit 27855d5

Browse files
authored
Merge pull request #651 from esensar/feature/641-open-note-ui
Update UI of OpenNodeDialog
2 parents 88100ea + 1a8871b commit 27855d5

File tree

7 files changed

+251
-90
lines changed

7 files changed

+251
-90
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ android {
6666
}
6767

6868
dependencies {
69-
implementation 'com.github.SimpleMobileTools:Simple-Commons:91763fe00f'
69+
implementation 'com.github.SimpleMobileTools:Simple-Commons:2a2c17151e'
7070
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
7171
implementation 'androidx.documentfile:documentfile:1.0.1'
7272

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.simplemobiletools.notes.pro.adapters
2+
3+
import android.content.Context
4+
import android.graphics.Color
5+
import android.text.SpannableString
6+
import android.text.style.StrikethroughSpan
7+
import android.view.Menu
8+
import android.view.View
9+
import android.view.ViewGroup
10+
import com.google.gson.Gson
11+
import com.google.gson.reflect.TypeToken
12+
import com.simplemobiletools.commons.activities.BaseSimpleActivity
13+
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
14+
import com.simplemobiletools.commons.extensions.beGoneIf
15+
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
16+
import com.simplemobiletools.commons.extensions.isBlackAndWhiteTheme
17+
import com.simplemobiletools.commons.helpers.LOWER_ALPHA_INT
18+
import com.simplemobiletools.commons.helpers.SORT_BY_CUSTOM
19+
import com.simplemobiletools.commons.views.MyRecyclerView
20+
import com.simplemobiletools.notes.pro.R
21+
import com.simplemobiletools.notes.pro.extensions.config
22+
import com.simplemobiletools.notes.pro.models.ChecklistItem
23+
import com.simplemobiletools.notes.pro.models.Note
24+
import com.simplemobiletools.notes.pro.models.NoteType
25+
import kotlinx.android.synthetic.main.open_note_item.view.open_note_item_holder
26+
import kotlinx.android.synthetic.main.open_note_item.view.open_note_item_text
27+
import kotlinx.android.synthetic.main.open_note_item.view.open_note_item_title
28+
29+
class OpenNoteAdapter(
30+
activity: BaseSimpleActivity, var items: List<Note>,
31+
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit
32+
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
33+
override fun getActionMenuId() = 0
34+
35+
override fun actionItemPressed(id: Int) {}
36+
37+
override fun getSelectableItemCount() = itemCount
38+
39+
override fun getIsItemSelectable(position: Int) = false
40+
41+
override fun getItemSelectionKey(position: Int) = items.getOrNull(position)?.id?.toInt()
42+
43+
override fun getItemKeyPosition(key: Int) = items.indexOfFirst { it.id?.toInt() == key }
44+
45+
override fun onActionModeCreated() {}
46+
47+
override fun onActionModeDestroyed() {}
48+
49+
override fun prepareActionMode(menu: Menu) {}
50+
51+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
52+
return createViewHolder(R.layout.open_note_item, parent)
53+
}
54+
55+
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
56+
val item = items[position]
57+
holder.bindView(item, true, false) { itemView, layoutPosition ->
58+
setupView(itemView, item)
59+
}
60+
bindViewHolder(holder)
61+
}
62+
63+
override fun getItemCount() = items.size
64+
65+
private fun setupView(view: View, note: Note) {
66+
view.apply {
67+
setupCard(open_note_item_holder)
68+
open_note_item_title.apply {
69+
text = note.title
70+
setTextColor(properPrimaryColor)
71+
}
72+
val formattedText = note.getFormattedValue(context)
73+
open_note_item_text.beGoneIf(formattedText.isNullOrBlank())
74+
open_note_item_text.apply {
75+
text = formattedText
76+
setTextColor(textColor)
77+
}
78+
}
79+
}
80+
81+
private fun View.setupCard(holder: View) {
82+
if (context.isBlackAndWhiteTheme()) {
83+
holder.setBackgroundResource(R.drawable.black_dialog_background)
84+
} else {
85+
val cardBackgroundColor = if (backgroundColor == Color.BLACK) {
86+
Color.WHITE
87+
} else {
88+
Color.BLACK
89+
}
90+
val cardBackground = if (context.config.isUsingSystemTheme) {
91+
R.drawable.dialog_you_background
92+
} else {
93+
R.drawable.dialog_bg
94+
}
95+
holder.background =
96+
activity.resources.getColoredDrawableWithColor(cardBackground, cardBackgroundColor, LOWER_ALPHA_INT)
97+
}
98+
}
99+
100+
private fun Note.getFormattedValue(context: Context): CharSequence? {
101+
return when (type) {
102+
NoteType.TYPE_TEXT -> getNoteStoredValue(context)
103+
NoteType.TYPE_CHECKLIST -> {
104+
val checklistItemType = object : TypeToken<List<ChecklistItem>>() {}.type
105+
var items = Gson().fromJson<List<ChecklistItem>>(getNoteStoredValue(context), checklistItemType) ?: listOf()
106+
items = items.filter { it.title != null }.let {
107+
val sorting = context.config.sorting
108+
ChecklistItem.sorting = sorting
109+
if (ChecklistItem.sorting and SORT_BY_CUSTOM == 0) {
110+
it.sorted().let {
111+
if (context.config.moveDoneChecklistItems) {
112+
it.sortedBy { it.isDone }
113+
} else {
114+
it
115+
}
116+
}
117+
} else {
118+
it
119+
}
120+
}
121+
val linePrefix = ""
122+
val stringifiedItems = items.joinToString(separator = System.lineSeparator()) {
123+
"${linePrefix}${it.title}"
124+
}
125+
126+
val formattedText = SpannableString(stringifiedItems)
127+
var currentPos = 0
128+
items.forEach { item ->
129+
currentPos += linePrefix.length
130+
if (item.isDone) {
131+
formattedText.setSpan(StrikethroughSpan(), currentPos, currentPos + item.title.length, 0)
132+
}
133+
currentPos += item.title.length
134+
currentPos += System.lineSeparator().length
135+
}
136+
formattedText
137+
}
138+
}
139+
}
140+
}
Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,53 @@
11
package com.simplemobiletools.notes.pro.dialogs
22

3-
import android.app.Activity
43
import android.view.View
5-
import android.view.ViewGroup
6-
import android.widget.RadioGroup
74
import androidx.appcompat.app.AlertDialog
8-
import com.simplemobiletools.commons.extensions.*
5+
import androidx.recyclerview.widget.StaggeredGridLayoutManager
6+
import com.simplemobiletools.commons.activities.BaseSimpleActivity
7+
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
8+
import com.simplemobiletools.commons.extensions.setupDialogStuff
9+
import com.simplemobiletools.commons.views.AutoStaggeredGridLayoutManager
910
import com.simplemobiletools.notes.pro.R
10-
import com.simplemobiletools.notes.pro.extensions.config
11+
import com.simplemobiletools.notes.pro.adapters.OpenNoteAdapter
1112
import com.simplemobiletools.notes.pro.helpers.NotesHelper
1213
import com.simplemobiletools.notes.pro.models.Note
13-
import kotlinx.android.synthetic.main.dialog_open_note.view.*
14-
import kotlinx.android.synthetic.main.open_note_item.view.*
14+
import kotlinx.android.synthetic.main.dialog_open_note.view.dialog_open_note_list
15+
import kotlinx.android.synthetic.main.dialog_open_note.view.new_note_fab
1516

16-
class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Long, newNote: Note?) -> Unit) {
17+
class OpenNoteDialog(val activity: BaseSimpleActivity, val callback: (checkedId: Long, newNote: Note?) -> Unit) {
1718
private var dialog: AlertDialog? = null
1819

1920
init {
2021
val view = activity.layoutInflater.inflate(R.layout.dialog_open_note, null)
22+
23+
val noteItemWidth = activity.resources.getDimensionPixelSize(R.dimen.grid_note_item_width)
24+
view.dialog_open_note_list.layoutManager = AutoStaggeredGridLayoutManager(noteItemWidth, StaggeredGridLayoutManager.VERTICAL)
25+
2126
NotesHelper(activity).getNotes {
2227
initDialog(it, view)
2328
}
29+
}
2430

25-
view.dialog_open_note_new_radio.setOnClickListener {
26-
view.dialog_open_note_new_radio.isChecked = false
31+
private fun initDialog(notes: List<Note>, view: View) {
32+
view.dialog_open_note_list.adapter = OpenNoteAdapter(activity, notes, view.dialog_open_note_list) {
33+
it as Note
34+
callback(it.id!!, null)
35+
dialog?.dismiss()
36+
}
37+
38+
view.new_note_fab.setOnClickListener {
2739
NewNoteDialog(activity, setChecklistAsDefault = false) {
2840
callback(0, it)
2941
dialog?.dismiss()
3042
}
3143
}
32-
}
3344

34-
private fun initDialog(notes: List<Note>, view: View) {
35-
val textColor = activity.getProperTextColor()
36-
notes.forEach {
37-
activity.layoutInflater.inflate(R.layout.open_note_item, null).apply {
38-
val note = it
39-
open_note_item_radio_button.apply {
40-
text = note.title
41-
isChecked = note.id == activity.config.currentNoteId
42-
id = note.id!!.toInt()
43-
44-
setOnClickListener {
45-
callback(note.id!!, null)
46-
dialog?.dismiss()
47-
}
45+
activity.getAlertDialogBuilder()
46+
.setNegativeButton(R.string.cancel, null)
47+
.apply {
48+
activity.setupDialogStuff(view, this, R.string.open_note) { alertDialog ->
49+
dialog = alertDialog
4850
}
49-
open_note_item_icon.apply {
50-
beVisibleIf(note.path.isNotEmpty())
51-
applyColorFilter(textColor)
52-
setOnClickListener {
53-
activity.toast(note.path)
54-
}
55-
}
56-
view.dialog_open_note_linear.addView(this, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
5751
}
58-
}
59-
60-
activity.getAlertDialogBuilder().apply {
61-
activity.setupDialogStuff(view, this, R.string.open_note) { alertDialog ->
62-
dialog = alertDialog
63-
}
64-
}
6552
}
6653
}
Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,36 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
2+
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
35
android:id="@+id/dialog_open_note_holder"
46
android:layout_width="match_parent"
57
android:layout_height="wrap_content">
68

7-
<RelativeLayout
8-
android:id="@+id/dialog_open_note_wrapper"
9+
<androidx.core.widget.NestedScrollView
910
android:layout_width="match_parent"
10-
android:layout_height="wrap_content">
11+
android:layout_height="match_parent"
12+
android:layout_marginStart="@dimen/small_margin"
13+
android:layout_marginTop="@dimen/medium_margin"
14+
android:layout_marginEnd="@dimen/small_margin"
15+
android:minHeight="@dimen/min_open_note_popup_height"
16+
app:layout_constraintHeight_max="@dimen/max_open_note_popup_height">
1117

12-
<LinearLayout
13-
android:id="@+id/dialog_open_note_linear"
18+
<com.simplemobiletools.commons.views.MyRecyclerView
19+
android:id="@+id/dialog_open_note_list"
1420
android:layout_width="match_parent"
15-
android:layout_height="wrap_content"
16-
android:layout_marginTop="@dimen/medium_margin"
17-
android:orientation="vertical" />
21+
android:layout_height="match_parent"
22+
tools:itemCount="10"
23+
android:paddingBottom="@dimen/open_note_popup_bottom_extra_padding"
24+
tools:listitem="@layout/open_note_item" />
1825

19-
<ImageView
20-
android:id="@+id/dialog_open_note_divider"
21-
android:layout_width="match_parent"
22-
android:layout_height="1px"
23-
android:layout_below="@+id/dialog_open_note_linear"
24-
android:background="@color/divider_grey"
25-
android:importantForAccessibility="no" />
26-
27-
<RadioGroup
28-
android:id="@+id/dialog_open_note_create_new"
29-
android:layout_width="match_parent"
30-
android:layout_height="wrap_content"
31-
android:layout_below="@+id/dialog_open_note_divider"
32-
android:paddingStart="@dimen/normal_margin"
33-
android:paddingTop="@dimen/small_margin"
34-
android:paddingEnd="@dimen/normal_margin"
35-
android:paddingBottom="@dimen/small_margin">
26+
</androidx.core.widget.NestedScrollView>
3627

37-
<com.simplemobiletools.commons.views.MyCompatRadioButton
38-
android:id="@+id/dialog_open_note_new_radio"
39-
android:layout_width="match_parent"
40-
android:layout_height="wrap_content"
41-
android:text="@string/create_new_note" />
28+
<com.simplemobiletools.commons.views.MyFloatingActionButton
29+
android:id="@+id/new_note_fab"
30+
android:layout_width="@dimen/fab_size"
31+
android:layout_height="@dimen/fab_size"
32+
android:layout_gravity="bottom|end"
33+
android:layout_margin="@dimen/activity_margin"
34+
android:src="@drawable/ic_plus_vector" />
4235

43-
</RadioGroup>
44-
</RelativeLayout>
45-
</ScrollView>
36+
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/open_new_note_item_holder"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:layout_margin="@dimen/medium_margin"
7+
android:background="@drawable/widget_round_background"
8+
android:orientation="vertical"
9+
android:padding="@dimen/normal_margin">
10+
11+
<com.simplemobiletools.commons.views.MyTextView
12+
android:id="@+id/open_new_note_item_title"
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:ellipsize="end"
16+
android:maxLines="2"
17+
android:textSize="@dimen/big_text_size"
18+
android:textStyle="bold"
19+
android:layout_gravity="center"
20+
android:textAlignment="center"
21+
android:text="@string/create_new_note" />
22+
23+
<ImageView
24+
android:id="@+id/open_new_note_icon"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:src="@drawable/ic_plus_vector"
28+
android:layout_gravity="center"
29+
android:importantForAccessibility="no" />
30+
31+
</LinearLayout>
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
34
android:id="@+id/open_note_item_holder"
45
android:layout_width="match_parent"
5-
android:layout_height="match_parent"
6-
android:paddingStart="@dimen/normal_margin"
7-
android:paddingEnd="@dimen/normal_margin">
6+
android:layout_height="wrap_content"
7+
android:layout_margin="@dimen/medium_margin"
8+
android:background="@drawable/widget_round_background"
9+
android:orientation="vertical"
10+
android:padding="@dimen/normal_margin">
811

9-
<com.simplemobiletools.commons.views.MyCompatRadioButton
10-
android:id="@+id/open_note_item_radio_button"
11-
android:layout_width="match_parent"
12+
<com.simplemobiletools.commons.views.MyTextView
13+
android:id="@+id/open_note_item_title"
14+
android:layout_width="wrap_content"
1215
android:layout_height="wrap_content"
13-
android:layout_toStartOf="@+id/open_note_item_icon" />
16+
android:ellipsize="end"
17+
android:lines="1"
18+
android:textSize="@dimen/big_text_size"
19+
android:textStyle="bold"
20+
tools:text="Title" />
1421

15-
<ImageView
16-
android:id="@+id/open_note_item_icon"
22+
<com.simplemobiletools.commons.views.MyTextView
23+
android:id="@+id/open_note_item_text"
1724
android:layout_width="wrap_content"
1825
android:layout_height="wrap_content"
19-
android:layout_alignParentEnd="true"
20-
android:layout_marginEnd="@dimen/small_margin"
21-
android:padding="@dimen/small_margin"
22-
android:src="@drawable/ic_attach_file_vector" />
26+
android:layout_marginTop="@dimen/medium_margin"
27+
android:ellipsize="end"
28+
android:maxHeight="@dimen/grid_note_item_max_height"
29+
tools:text="text" />
2330

24-
</RelativeLayout>
31+
</LinearLayout>

0 commit comments

Comments
 (0)