mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-22 16:47:15 +00:00
add recyclerview.alerts project
This commit is contained in:
parent
e15034bfa4
commit
e985631621
@ -5,6 +5,9 @@
|
||||
* `Android`
|
||||
* `Alerts`
|
||||
* Project has been created
|
||||
* `RecyclerView`
|
||||
* `Alerts`
|
||||
* Project has been created
|
||||
* `Common`
|
||||
* Annotation `PreviewFeature` has been added
|
||||
* `Android`
|
||||
|
18
android/recyclerview/alerts/build.gradle
Normal file
18
android/recyclerview/alerts/build.gradle
Normal file
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1
android/recyclerview/alerts/src/main/AndroidManifest.xml
Normal file
1
android/recyclerview/alerts/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1 @@
|
||||
<manifest package="dev.inmo.micro_utils.android.recyclerview.alerts"/>
|
@ -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<AlertAction>(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<AlertAction>,
|
||||
private val dialogInterfaceGetter: () -> DialogInterface
|
||||
) : RecyclerViewAdapter<AlertAction>(data) {
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AbstractViewHolder<AlertAction> = ActionViewHolder(
|
||||
parent, dialogInterfaceGetter
|
||||
)
|
||||
}
|
||||
|
||||
fun Context.createActionsAlertDialog(
|
||||
actions: List<AlertAction>,
|
||||
title: Int? = null,
|
||||
positivePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
negativePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
show: Boolean = true
|
||||
): AlertDialog {
|
||||
lateinit var dialogInterface: DialogInterface
|
||||
|
||||
return createRecyclerViewDialog(
|
||||
title, positivePair, neutralPair, negativePair, show
|
||||
) {
|
||||
ActionsRecyclerViewAdapter(
|
||||
actions
|
||||
) {
|
||||
dialogInterface
|
||||
}
|
||||
}.also { dialogInterface = it }
|
||||
}
|
@ -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<Int, AlertDialogCallback?>? = null,
|
||||
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
|
||||
negativePair: Pair<Int, AlertDialogCallback?>? = 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
|
||||
}
|
||||
}
|
@ -17,8 +17,9 @@ String[] includes = [
|
||||
":ktor:common",
|
||||
":ktor:client",
|
||||
":coroutines",
|
||||
":android:recyclerview",
|
||||
":android:alerts",
|
||||
":android:recyclerview",
|
||||
":android:recyclerview:alerts",
|
||||
|
||||
":dokka"
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user