From e985631621e6808d21ce08ece285ca6cf9860ac9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 23 Nov 2020 18:10:53 +0600 Subject: [PATCH] add recyclerview.alerts project --- CHANGELOG.md | 3 + android/recyclerview/alerts/build.gradle | 18 +++++ .../alerts/src/main/AndroidManifest.xml | 1 + .../recyclerview/alerts/ActionsAlerts.kt | 65 +++++++++++++++++++ .../recyclerview/alerts/RecyclerViewDialog.kt | 44 +++++++++++++ settings.gradle | 3 +- 6 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 android/recyclerview/alerts/build.gradle create mode 100644 android/recyclerview/alerts/src/main/AndroidManifest.xml create mode 100644 android/recyclerview/alerts/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/alerts/ActionsAlerts.kt create mode 100644 android/recyclerview/alerts/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/alerts/RecyclerViewDialog.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 96ee10d565d..d042aaa7e3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ * `Android` * `Alerts` * Project has been created + * `RecyclerView` + * `Alerts` + * Project has been created * `Common` * Annotation `PreviewFeature` has been added * `Android` diff --git a/android/recyclerview/alerts/build.gradle b/android/recyclerview/alerts/build.gradle new file mode 100644 index 00000000000..b658bed4b93 --- /dev/null +++ b/android/recyclerview/alerts/build.gradle @@ -0,0 +1,18 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" + id "com.android.library" +} + +apply from: "$mppAndroidProjectPresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + api internalProject("micro_utils.android.recyclerview") + api internalProject("micro_utils.android.alerts") + } + } + } +} diff --git a/android/recyclerview/alerts/src/main/AndroidManifest.xml b/android/recyclerview/alerts/src/main/AndroidManifest.xml new file mode 100644 index 00000000000..952927d0f89 --- /dev/null +++ b/android/recyclerview/alerts/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/android/recyclerview/alerts/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/alerts/ActionsAlerts.kt b/android/recyclerview/alerts/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/alerts/ActionsAlerts.kt new file mode 100644 index 00000000000..be4abb0c667 --- /dev/null +++ b/android/recyclerview/alerts/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/alerts/ActionsAlerts.kt @@ -0,0 +1,65 @@ +@file:Suppress("unused") + +package dev.inmo.micro_utils.android.recyclerview.alerts + +import android.app.AlertDialog +import android.content.Context +import android.content.DialogInterface +import android.view.ViewGroup +import android.widget.TextView +import dev.inmo.micro_utils.android.alerts.AlertDialogCallback +import dev.inmo.micro_utils.android.recyclerview.* + +data class AlertAction( + val title: String, + val callback: (DialogInterface) -> Unit +) + +private class ActionViewHolder( + container: ViewGroup, dialogInterfaceGetter: () -> DialogInterface +) : AbstractStandardViewHolder(container, android.R.layout.simple_list_item_1) { + private lateinit var action: AlertAction + private val textView: TextView + get() = itemView.findViewById(android.R.id.text1) + + init { + itemView.setOnClickListener { + action.callback(dialogInterfaceGetter()) + } + } + + override fun onBind(item: AlertAction) { + action = item + textView.text = item.title + } +} + +private class ActionsRecyclerViewAdapter( + data: List, + private val dialogInterfaceGetter: () -> DialogInterface +) : RecyclerViewAdapter(data) { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AbstractViewHolder = ActionViewHolder( + parent, dialogInterfaceGetter + ) +} + +fun Context.createActionsAlertDialog( + actions: List, + title: Int? = null, + positivePair: Pair? = null, + neutralPair: Pair? = null, + negativePair: Pair? = null, + show: Boolean = true +): AlertDialog { + lateinit var dialogInterface: DialogInterface + + return createRecyclerViewDialog( + title, positivePair, neutralPair, negativePair, show + ) { + ActionsRecyclerViewAdapter( + actions + ) { + dialogInterface + } + }.also { dialogInterface = it } +} diff --git a/android/recyclerview/alerts/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/alerts/RecyclerViewDialog.kt b/android/recyclerview/alerts/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/alerts/RecyclerViewDialog.kt new file mode 100644 index 00000000000..cd706deeb36 --- /dev/null +++ b/android/recyclerview/alerts/src/main/kotlin/dev/inmo/micro_utils/android/recyclerview/alerts/RecyclerViewDialog.kt @@ -0,0 +1,44 @@ +package dev.inmo.micro_utils.android.recyclerview.alerts + +import android.app.AlertDialog +import android.content.Context +import android.content.DialogInterface +import android.widget.LinearLayout +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import dev.inmo.micro_utils.android.alerts.AlertDialogCallback +import dev.inmo.micro_utils.android.alerts.createCustomViewAlertDialogWithResources + +fun Context.createRecyclerViewDialog( + title: Int? = null, + positivePair: Pair? = null, + neutralPair: Pair? = null, + negativePair: Pair? = null, + show: Boolean = true, + layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(this), + marginOfRecyclerView: Int = 8, // dp + recyclerViewSetUp: RecyclerView.() -> Unit = {}, + adapterFactory: () -> RecyclerView.Adapter<*> +): AlertDialog { + val recyclerView = RecyclerView(this).apply { + layoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ).apply { + setMargins(marginOfRecyclerView, marginOfRecyclerView, marginOfRecyclerView, marginOfRecyclerView) + } + this.layoutManager = layoutManager + adapter = adapterFactory() + recyclerViewSetUp() + } + + return createCustomViewAlertDialogWithResources( + title, + positivePair, + neutralPair, + negativePair, + show + ) { + recyclerView + } +} diff --git a/settings.gradle b/settings.gradle index 32dc161d3eb..64c4d626af0 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,8 +17,9 @@ String[] includes = [ ":ktor:common", ":ktor:client", ":coroutines", - ":android:recyclerview", ":android:alerts", + ":android:recyclerview", + ":android:recyclerview:alerts", ":dokka" ]