Skip to content

Instantly share code, notes, and snippets.

@mahdiPourkazemi
Last active July 27, 2025 15:41
Show Gist options
  • Save mahdiPourkazemi/2752ac97305a277da3928e23c47d2a04 to your computer and use it in GitHub Desktop.
Save mahdiPourkazemi/2752ac97305a277da3928e23c47d2a04 to your computer and use it in GitHub Desktop.
SAM applications in android
fun interface OnAction {
fun execute(context: Context)
companion object {
fun withLogging(tag: String = "MyAction",
message: String = "Button clicked",
action: (Context) -> Unit
): OnAction {
return OnAction { context ->
Log.d(tag, message)
action(context)
}
}
}
}
@Composable
fun MyActionButton(label: String, action: OnAction) {
val context = LocalContext.current
Button(onClick = { action.execute(context) }) {
Text(label)
}
}
//###############################android_example#########################################
fun interface OnItemClickListener {
fun onItemClick(item: String)
companion object {
fun withLogging(
tag: String = "ListItemClick",
messagePrefix: String = "Clicked on: ",
action: (String) -> Unit
): OnItemClickListener {
return OnItemClickListener { item ->
Log.d(tag, "$messagePrefix$item")
action(item)
}
}
}
}
@Composable
fun ItemList(items: List<String>, listener: OnItemClickListener) {
LazyColumn {
items(items) { item ->
ListItem(item, listener)
}
}
}
@Composable
fun ListItem(item: String, listener: OnItemClickListener) {
Text(
text = item,
modifier = Modifier
.fillMaxWidth()
.clickable { listener.onItemClick(item) }
.padding(16.dp)
)
}
@Composable
fun MainScreen() {
val items = listOf("Kotlin", "Compose", "Coroutines", "ViewModel")
val itemClickListener = OnItemClickListener.withLogging { item ->
// واکنش سفارشی هنگام کلیک
// مثلاً: نمایش Toast یا ناوبری
}
ItemList(items, itemClickListener)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment