mirror of
				https://github.com/SimpleMobileTools/Simple-Notes.git
				synced 2025-06-05 17:00:23 +02:00 
			
		
		
		
	Dynamically add checklist items
This commit is contained in:
		@@ -2,38 +2,73 @@ 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.notes.pro.R
 | 
			
		||||
import kotlinx.android.synthetic.main.dialog_new_checklist_item.view.*
 | 
			
		||||
import kotlinx.android.synthetic.main.dialog_new_checklist_item.view.add_item
 | 
			
		||||
import kotlinx.android.synthetic.main.dialog_new_checklist_item.view.checklist_holder
 | 
			
		||||
import kotlinx.android.synthetic.main.dialog_new_checklist_item.view.dialog_holder
 | 
			
		||||
import kotlinx.android.synthetic.main.item_add_checklist.view.delete_item
 | 
			
		||||
import kotlinx.android.synthetic.main.item_add_checklist.view.title_edit_text
 | 
			
		||||
 | 
			
		||||
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 view: ViewGroup = activity.layoutInflater.inflate(R.layout.dialog_new_checklist_item, null) as ViewGroup
 | 
			
		||||
 | 
			
		||||
    init {
 | 
			
		||||
        addNewEditText()
 | 
			
		||||
        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, cancelOnTouchOutside = false) {
 | 
			
		||||
                    activity.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
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            val currentIndex = titles.size
 | 
			
		||||
            delete_item.setOnClickListener {
 | 
			
		||||
                view.checklist_holder.removeViewAt(currentIndex)
 | 
			
		||||
                titles.removeAt(currentIndex)
 | 
			
		||||
            }
 | 
			
		||||
            titles.add(title_edit_text)
 | 
			
		||||
            view.checklist_holder.addView(this)
 | 
			
		||||
            view.dialog_holder.post {
 | 
			
		||||
                view.dialog_holder.fullScroll(View.FOCUS_DOWN)
 | 
			
		||||
                activity.showKeyboard(title_edit_text)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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:fillViewport="true"
 | 
			
		||||
    android:orientation="vertical"
 | 
			
		||||
    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" />
 | 
			
		||||
        <com.simplemobiletools.commons.views.MyTextView
 | 
			
		||||
            android:id="@+id/add_item"
 | 
			
		||||
            android:layout_width="wrap_content"
 | 
			
		||||
            android:layout_height="wrap_content"
 | 
			
		||||
            android:layout_marginTop="@dimen/medium_margin"
 | 
			
		||||
            android:background="?selectableItemBackground"
 | 
			
		||||
            android:drawableStart="@drawable/ic_plus_vector"
 | 
			
		||||
            android:drawablePadding="@dimen/small_margin"
 | 
			
		||||
            android:gravity="center_vertical"
 | 
			
		||||
            android:padding="@dimen/small_margin"
 | 
			
		||||
            android:text="@string/add_new_checklist_item" />
 | 
			
		||||
 | 
			
		||||
</LinearLayout>
 | 
			
		||||
    </LinearLayout>
 | 
			
		||||
 | 
			
		||||
</ScrollView>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										32
									
								
								app/src/main/res/layout/item_add_checklist.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								app/src/main/res/layout/item_add_checklist.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
<?xml version="1.0" encoding="utf-8"?>
 | 
			
		||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 | 
			
		||||
    xmlns:app="http://schemas.android.com/apk/res-auto"
 | 
			
		||||
    android:layout_width="match_parent"
 | 
			
		||||
    android:layout_height="wrap_content">
 | 
			
		||||
 | 
			
		||||
    <com.simplemobiletools.commons.views.MyEditText
 | 
			
		||||
        android:id="@+id/title_edit_text"
 | 
			
		||||
        android:layout_width="0dp"
 | 
			
		||||
        android:layout_height="wrap_content"
 | 
			
		||||
        android:layout_marginEnd="@dimen/medium_margin"
 | 
			
		||||
        android:layout_marginBottom="@dimen/medium_margin"
 | 
			
		||||
        android:inputType="textCapSentences"
 | 
			
		||||
        android:maxLength="500"
 | 
			
		||||
        android:textCursorDrawable="@null"
 | 
			
		||||
        android:textSize="@dimen/normal_text_size"
 | 
			
		||||
        app:layout_constraintEnd_toStartOf="@id/delete_item"
 | 
			
		||||
        app:layout_constraintStart_toStartOf="parent"
 | 
			
		||||
        app:layout_constraintTop_toTopOf="parent" />
 | 
			
		||||
 | 
			
		||||
    <ImageView
 | 
			
		||||
        android:id="@+id/delete_item"
 | 
			
		||||
        android:layout_width="@dimen/checklist_delete_size"
 | 
			
		||||
        android:layout_height="@dimen/checklist_delete_size"
 | 
			
		||||
        android:background="?selectableItemBackgroundBorderless"
 | 
			
		||||
        android:padding="@dimen/medium_margin"
 | 
			
		||||
        android:src="@drawable/ic_delete_vector"
 | 
			
		||||
        app:layout_constraintBottom_toBottomOf="parent"
 | 
			
		||||
        app:layout_constraintEnd_toEndOf="parent"
 | 
			
		||||
        app:layout_constraintTop_toTopOf="parent" />
 | 
			
		||||
 | 
			
		||||
</androidx.constraintlayout.widget.ConstraintLayout>
 | 
			
		||||
@@ -1,3 +1,4 @@
 | 
			
		||||
<resources>
 | 
			
		||||
    <dimen name="checklist_image_size">56dp</dimen>
 | 
			
		||||
    <dimen name="checklist_delete_size">36dp</dimen>
 | 
			
		||||
</resources>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user