Skip to content

Instantly share code, notes, and snippets.

View teegarcs's full-sized avatar

Clinton teegarcs

  • CapTech Ventures
  • Washington, D.C.
View GitHub Profile
@teegarcs
teegarcs / LazyList.kt
Created December 13, 2023 14:06
Extension function genericList
fun <Intent : Any> LazyListScope.genericList(
items: List<GenericLazyItem<Intent>>,
processIntent: (Intent) -> Unit
) {
items.forEachIndexed { index, genericLazyItem ->
/*
Draw Header if the section is different than the previous one.
*/
if (index == 0 || genericLazyItem.sectionMatcher() != items[index - 1].sectionMatcher()) {
@teegarcs
teegarcs / TextImageLeft.kt
Created December 13, 2023 14:05
Example use of base class GenericLazyItem
data class TextImageLeft(
private val imageUrl: String,
private val title: String,
private val description: String,
private val groupTitle: String
) : GenericLazyItem<ItemIntent>() {
override fun sectionMatcher() = groupTitle.hashCode()
@Composable
@teegarcs
teegarcs / GenericLazyItem.kt
Created December 13, 2023 14:04
Generic Lazy Item base class.
abstract class GenericLazyItem<Intent : Any> {
open fun itemKey(): Int = hashCode()
open fun sectionMatcher(): Int? = null
@Composable
open fun BuildHeaderItem(
processIntent: (Intent) -> Unit
) {
//by default no header.
}
@Composable
@teegarcs
teegarcs / SimpleGenericList.kt
Created December 13, 2023 14:03
Example use of genericList function
LazyColumn {
genericList(state.lazyItems) {
//list item intent interactions
}
}
@teegarcs
teegarcs / LessSimpleLazy.kt
Created December 13, 2023 14:02
Lazy List with multiple content types.
LazyColumn {
items(contentList) { content ->
when(content){
is ImageOnly -> MessageRow(content)
is ImageTextRight -> ImageTextRight(content)
is ImageTextLeft -> ImageTextLeft(content)
is ImageTextTop -> ImageTextTop(content)
//...
}
}
@teegarcs
teegarcs / SimpleLazy.kt
Created December 13, 2023 14:00
simple example of a LazyColumn with a single CMS driven content type.
LazyColumn {
items(contentList) { content ->
ImageOnly(content)
}
}
protected fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
when (resultCode) {
Constants.LOGIN_SUCCESS -> {
if (requestCode == Constants.REQUEST_CODE_MY_ACCOUNT) {
onLoginSuccess()
}
}
Constants.LOGOUT_SUCCESS -> {
if (requestCode == Constants.REQUEST_CODE_MY_ACCOUNT) {
onLogoutSuccess()
private val messageLauncher =
registerForActivityResult(MessageContract()) {
Toast.makeText(this, it, Toast.LENGTH_SHORT).show()
}
class MessageContract : ActivityResultContract<Unit, String>() {
companion object {
const val MESSAGE = "MESSAGE_CONTRACT"
}
override fun createIntent(context: Context, input: Unit?) =
Intent(context, MessageActivity::class.java)
/**
class MessageActivity : AppCompatActivity() {
.......
/**
* launched via onClick in the XML
* sets this finishing activity's result to the text string
* entered by the user
*/
fun onSubmitMessage(view: View) {
val result = Intent().putExtra(MessageContract.MESSAGE,