Last active
July 27, 2025 15:41
-
-
Save mahdiPourkazemi/2752ac97305a277da3928e23c47d2a04 to your computer and use it in GitHub Desktop.
SAM applications in android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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