Skip to content

Instantly share code, notes, and snippets.

View anitaa1990's full-sized avatar
🎯
Focusing

Anitaa Murthy anitaa1990

🎯
Focusing
  • Chennai
View GitHub Profile
GlobalScope.launch {
val config = fetchConfigFromServer() // network request
updateConfiguration(config)
}
suspend fun doSomething(): Int {
delay(1000L) // pretend we are doing something useful here
return 13
}
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ContactsList(
modifier: Modifier = Modifier,
contacts: Map<String, List<ContactModel>> = Collections.emptyMap()
){
LazyColumn(modifier) {
contacts.map { entry ->
// Our stick header displays the alphabet letters
stickyHeader {
dependencies {
// Google permission lib
implementation(libs.permission.lib)
}
[versions]
accompanistVersion = "0.28.0"
[libraries]
## Google Permissions lib
permission-lib = { group = "com.google.accompanist", name = "accompanist-permissions", version.ref = "accompanistVersion" }
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun MainApp() {
// The theme of our app
ContactsAppTheme {
// Defines a default Scaffold with a default TopAppBar called `MainTopAppBar()`
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = { MainTopAppBar() }
) { innerPadding ->
@Composable
fun MainAppContent(
modifier: Modifier = Modifier,
state: ContactUiState
) {
Box(modifier = modifier
.fillMaxSize()
) {
// the loader indicator
AnimatedVisibility(visible = state.loading) {
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainApp()
}
}
}
@HiltViewModel
class ContactsViewModel @Inject constructor(
private val contactsRepository: ContactsRepository
) : ViewModel() {
// We are defining a MutableStateFlow for the `ContactUiState` with an
// initial value of loading = true. So when the app is first launched,
// a loading screen will be displayed while we fetch the contacts list.
private val _uiState = MutableStateFlow(ContactUiState(loading = true))
val uiState = _uiState.asStateFlow()
data class ContactUiState(
val loading: Boolean = false,
val contacts: GroupedContacts = Collections.emptyMap()
)
/**
* Type aliases provide alternative names for existing types.
* If the type name is too long you can introduce a different shorter name
* and use the new one instead. In this example, we've created a typealias
* for `ContactModel` to convert it into a Map which includes the alphabets