This file contains 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
GlobalScope.launch { | |
val config = fetchConfigFromServer() // network request | |
updateConfiguration(config) | |
} |
This file contains 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
suspend fun doSomething(): Int { | |
delay(1000L) // pretend we are doing something useful here | |
return 13 | |
} |
This file contains 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
@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 { |
This file contains 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
dependencies { | |
// Google permission lib | |
implementation(libs.permission.lib) | |
} |
This file contains 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
[versions] | |
accompanistVersion = "0.28.0" | |
[libraries] | |
## Google Permissions lib | |
permission-lib = { group = "com.google.accompanist", name = "accompanist-permissions", version.ref = "accompanistVersion" } |
This file contains 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
@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 -> |
This file contains 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 MainAppContent( | |
modifier: Modifier = Modifier, | |
state: ContactUiState | |
) { | |
Box(modifier = modifier | |
.fillMaxSize() | |
) { | |
// the loader indicator | |
AnimatedVisibility(visible = state.loading) { |
This file contains 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
@AndroidEntryPoint | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
MainApp() | |
} | |
} | |
} |
This file contains 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
@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() |
This file contains 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
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 |
NewerOlder