Merge pull request #457 from KryptKode/feat/add-multiple-checkitems

Dynamically add checklist items
This commit is contained in:
Tibor Kaputa 2021-10-08 18:25:29 +02:00 committed by GitHub
commit 46adfc6798
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 50 deletions

View File

@ -2,38 +2,67 @@ package com.simplemobiletools.notes.pro.dialogs
import android.app.Activity
import android.content.DialogInterface.BUTTON_POSITIVE
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.showKeyboard
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.extensions.config
import kotlinx.android.synthetic.main.dialog_new_checklist_item.view.*
import kotlinx.android.synthetic.main.item_add_checklist.view.*
class NewChecklistItemDialog(val activity: Activity, callback: (titles: ArrayList<String>) -> Unit) {
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_new_checklist_item, null)
private val titles = mutableListOf<EditText>()
private val textColor = activity.config.textColor
private val view: ViewGroup = activity.layoutInflater.inflate(R.layout.dialog_new_checklist_item, null) as ViewGroup
init {
addNewEditText()
view.add_item.applyColorFilter(activity.getAdjustedPrimaryColor())
view.add_item.background.applyColorFilter(textColor)
view.add_item.setOnClickListener {
addNewEditText()
}
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.add_new_checklist_items) {
showKeyboard(view.checklist_item_title_1)
getButton(BUTTON_POSITIVE).setOnClickListener {
val title1 = view.checklist_item_title_1.value
val title2 = view.checklist_item_title_2.value
val title3 = view.checklist_item_title_3.value
when {
title1.isEmpty() && title2.isEmpty() && title3.isEmpty() -> activity.toast(R.string.empty_name)
else -> {
val titles = arrayListOf(title1, title2, title3).filter { it.isNotEmpty() }.toMutableList() as ArrayList<String>
callback(titles)
dismiss()
}
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.add_new_checklist_items) {
showKeyboard(titles.first())
getButton(BUTTON_POSITIVE).setOnClickListener {
when {
titles.all { it.text.isEmpty() } -> activity.toast(R.string.empty_name)
else -> {
val titles = titles.map { it.text.toString() }.filter { it.isNotEmpty() }.toMutableList() as ArrayList<String>
callback(titles)
dismiss()
}
}
}
}
}
}
private fun addNewEditText() {
activity.layoutInflater.inflate(R.layout.item_add_checklist, null).apply {
title_edit_text.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE || actionId == KeyEvent.KEYCODE_ENTER) {
addNewEditText()
true
} else {
false
}
}
titles.add(title_edit_text)
view.checklist_holder.addView(this)
activity.updateTextColors(view.checklist_holder)
view.dialog_holder.post {
view.dialog_holder.fullScroll(View.FOCUS_DOWN)
activity.showKeyboard(title_edit_text)
}
}
}
}

View File

@ -1,41 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fillViewport="true"
android:paddingStart="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
android:paddingEnd="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/checklist_item_title_1"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/medium_margin"
android:inputType="textCapSentences"
android:maxLength="500"
android:textCursorDrawable="@null"
android:textSize="@dimen/normal_text_size" />
android:orientation="vertical">
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/checklist_item_title_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/medium_margin"
android:inputType="textCapSentences"
android:maxLength="500"
android:textCursorDrawable="@null"
android:textSize="@dimen/normal_text_size" />
<LinearLayout
android:id="@+id/checklist_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/checklist_item_title_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/medium_margin"
android:inputType="textCapSentences"
android:maxLength="500"
android:textCursorDrawable="@null"
android:textSize="@dimen/normal_text_size" />
<ImageView
android:id="@+id/add_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_margin"
android:layout_marginBottom="@dimen/medium_margin"
android:background="@drawable/button_background"
android:paddingStart="@dimen/activity_margin"
android:paddingTop="@dimen/medium_margin"
android:paddingEnd="@dimen/activity_margin"
android:paddingBottom="@dimen/medium_margin"
android:src="@drawable/ic_plus_vector" />
</LinearLayout>
</LinearLayout>
</ScrollView>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<com.simplemobiletools.commons.views.MyEditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/title_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapSentences"
android:maxLength="500"
android:textCursorDrawable="@null"
android:textSize="@dimen/normal_text_size" />