Skip to content

Commit 46adfc6

Browse files
authored
Merge pull request #457 from KryptKode/feat/add-multiple-checkitems
Dynamically add checklist items
2 parents 6006a32 + 0e04566 commit 46adfc6

File tree

3 files changed

+84
-50
lines changed

3 files changed

+84
-50
lines changed

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

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,67 @@ package com.simplemobiletools.notes.pro.dialogs
22

33
import android.app.Activity
44
import android.content.DialogInterface.BUTTON_POSITIVE
5+
import android.view.KeyEvent
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import android.view.inputmethod.EditorInfo
9+
import android.widget.EditText
510
import androidx.appcompat.app.AlertDialog
6-
import com.simplemobiletools.commons.extensions.setupDialogStuff
7-
import com.simplemobiletools.commons.extensions.showKeyboard
8-
import com.simplemobiletools.commons.extensions.toast
9-
import com.simplemobiletools.commons.extensions.value
11+
import com.simplemobiletools.commons.extensions.*
1012
import com.simplemobiletools.notes.pro.R
13+
import com.simplemobiletools.notes.pro.extensions.config
1114
import kotlinx.android.synthetic.main.dialog_new_checklist_item.view.*
15+
import kotlinx.android.synthetic.main.item_add_checklist.view.*
1216

1317
class NewChecklistItemDialog(val activity: Activity, callback: (titles: ArrayList<String>) -> Unit) {
14-
init {
15-
val view = activity.layoutInflater.inflate(R.layout.dialog_new_checklist_item, null)
18+
private val titles = mutableListOf<EditText>()
19+
private val textColor = activity.config.textColor
20+
private val view: ViewGroup = activity.layoutInflater.inflate(R.layout.dialog_new_checklist_item, null) as ViewGroup
1621

22+
init {
23+
addNewEditText()
24+
view.add_item.applyColorFilter(activity.getAdjustedPrimaryColor())
25+
view.add_item.background.applyColorFilter(textColor)
26+
view.add_item.setOnClickListener {
27+
addNewEditText()
28+
}
1729
AlertDialog.Builder(activity)
18-
.setPositiveButton(R.string.ok, null)
19-
.setNegativeButton(R.string.cancel, null)
20-
.create().apply {
21-
activity.setupDialogStuff(view, this, R.string.add_new_checklist_items) {
22-
showKeyboard(view.checklist_item_title_1)
23-
getButton(BUTTON_POSITIVE).setOnClickListener {
24-
val title1 = view.checklist_item_title_1.value
25-
val title2 = view.checklist_item_title_2.value
26-
val title3 = view.checklist_item_title_3.value
27-
when {
28-
title1.isEmpty() && title2.isEmpty() && title3.isEmpty() -> activity.toast(R.string.empty_name)
29-
else -> {
30-
val titles = arrayListOf(title1, title2, title3).filter { it.isNotEmpty() }.toMutableList() as ArrayList<String>
31-
callback(titles)
32-
dismiss()
33-
}
30+
.setPositiveButton(R.string.ok, null)
31+
.setNegativeButton(R.string.cancel, null)
32+
.create().apply {
33+
activity.setupDialogStuff(view, this, R.string.add_new_checklist_items) {
34+
showKeyboard(titles.first())
35+
getButton(BUTTON_POSITIVE).setOnClickListener {
36+
when {
37+
titles.all { it.text.isEmpty() } -> activity.toast(R.string.empty_name)
38+
else -> {
39+
val titles = titles.map { it.text.toString() }.filter { it.isNotEmpty() }.toMutableList() as ArrayList<String>
40+
callback(titles)
41+
dismiss()
3442
}
3543
}
3644
}
3745
}
46+
}
47+
}
48+
49+
private fun addNewEditText() {
50+
activity.layoutInflater.inflate(R.layout.item_add_checklist, null).apply {
51+
title_edit_text.setOnEditorActionListener { _, actionId, _ ->
52+
if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE || actionId == KeyEvent.KEYCODE_ENTER) {
53+
addNewEditText()
54+
true
55+
} else {
56+
false
57+
}
58+
}
59+
titles.add(title_edit_text)
60+
view.checklist_holder.addView(this)
61+
activity.updateTextColors(view.checklist_holder)
62+
view.dialog_holder.post {
63+
view.dialog_holder.fullScroll(View.FOCUS_DOWN)
64+
activity.showKeyboard(title_edit_text)
65+
}
66+
}
3867
}
3968
}
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
33
android:id="@+id/dialog_holder"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6-
android:orientation="vertical"
6+
android:fillViewport="true"
77
android:paddingStart="@dimen/activity_margin"
88
android:paddingTop="@dimen/activity_margin"
99
android:paddingEnd="@dimen/activity_margin">
1010

11-
<com.simplemobiletools.commons.views.MyEditText
12-
android:id="@+id/checklist_item_title_1"
11+
<LinearLayout
1312
android:layout_width="match_parent"
1413
android:layout_height="wrap_content"
15-
android:layout_marginBottom="@dimen/medium_margin"
16-
android:inputType="textCapSentences"
17-
android:maxLength="500"
18-
android:textCursorDrawable="@null"
19-
android:textSize="@dimen/normal_text_size" />
14+
android:orientation="vertical">
2015

21-
<com.simplemobiletools.commons.views.MyEditText
22-
android:id="@+id/checklist_item_title_2"
23-
android:layout_width="match_parent"
24-
android:layout_height="wrap_content"
25-
android:layout_marginBottom="@dimen/medium_margin"
26-
android:inputType="textCapSentences"
27-
android:maxLength="500"
28-
android:textCursorDrawable="@null"
29-
android:textSize="@dimen/normal_text_size" />
16+
<LinearLayout
17+
android:id="@+id/checklist_holder"
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
android:orientation="vertical" />
3021

31-
<com.simplemobiletools.commons.views.MyEditText
32-
android:id="@+id/checklist_item_title_3"
33-
android:layout_width="match_parent"
34-
android:layout_height="wrap_content"
35-
android:layout_marginBottom="@dimen/medium_margin"
36-
android:inputType="textCapSentences"
37-
android:maxLength="500"
38-
android:textCursorDrawable="@null"
39-
android:textSize="@dimen/normal_text_size" />
22+
<ImageView
23+
android:id="@+id/add_item"
24+
android:layout_width="wrap_content"
25+
android:layout_height="wrap_content"
26+
android:layout_marginTop="@dimen/medium_margin"
27+
android:layout_marginBottom="@dimen/medium_margin"
28+
android:background="@drawable/button_background"
29+
android:paddingStart="@dimen/activity_margin"
30+
android:paddingTop="@dimen/medium_margin"
31+
android:paddingEnd="@dimen/activity_margin"
32+
android:paddingBottom="@dimen/medium_margin"
33+
android:src="@drawable/ic_plus_vector" />
34+
35+
</LinearLayout>
4036

41-
</LinearLayout>
37+
</ScrollView>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.simplemobiletools.commons.views.MyEditText xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/title_edit_text"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:inputType="textCapSentences"
7+
android:maxLength="500"
8+
android:textCursorDrawable="@null"
9+
android:textSize="@dimen/normal_text_size" />

0 commit comments

Comments
 (0)