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
@Composable | |
fun LazyList() { | |
val items = listOf("Dog", "Cat", "Monkey")//1000 of these | |
LazyColumnFor(items = items) { item -> | |
Text(text = item) | |
} | |
} |
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
@Composable | |
fun LazyExampleWithIndex() { | |
val items = listOf("Cat", "Dog", "Monkey")//1000 of these | |
val lastIndex = items.lastIndex | |
LazyColumnForIndexed(items = items) { index, item -> | |
if (lastIndex == index) { | |
onActive { | |
//fetch more items from API | |
} | |
} |
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
@ExperimentalCoroutinesApi | |
@Composable | |
fun LatestNewsFeed(viewModel: NewsViewModel) { | |
val state = viewModel.newsState.collectAsState() | |
val lastIndex = state.value.articles.lastIndex | |
LazyColumnForIndexed(items = state.value.articles) { i, newsItem -> | |
if (lastIndex == i) { | |
onActive { | |
viewModel.getMoreNews() | |
} |
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
class MainActivity : ComponentActivity() { | |
private val viewModel by viewModels<MainViewModel>() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
ComposeStateTestTheme { | |
val state: MainState by viewModel.state.collectAsState() | |
MainScaffold( |
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
val snackBarState = remember { SnackbarHostState() } | |
Scaffold( | |
modifier = Modifier.fillMaxSize(), | |
snackbarHost = { | |
SnackbarHost(hostState = snackBarState) { | |
Snackbar { | |
Text(text = it.visuals.message) | |
} | |
} | |
}, |
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
import androidx.compose.ui.text.input.OffsetMapping | |
class CurrencyOffsetMapping(originalText: String, formattedText: String) : OffsetMapping { | |
private val originalLength: Int = originalText.length | |
private val indexes = findDigitIndexes(originalText, formattedText) | |
private fun findDigitIndexes(firstString: String, secondString: String): List<Int> { | |
val digitIndexes = mutableListOf<Int>() | |
var currentIndex = 0 | |
for (digit in firstString) { |
OlderNewer