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
fun event(event: Event) { | |
when(event) { | |
is Event.DataReceived -> handleScreenLoadState(event.data) | |
is Event.LinkClicked -> viewAction.postValue(ViewEffect.NavigateToLink(data?.url)) | |
is Event.AddToFavouritesClicked -> viewAction.postValue(ViewEffect.ShowSnackBar( | |
R.string.button_result_text | |
)) | |
} | |
} |
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
package com.zk.samplenewsapp.viewModel | |
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import com.zk.samplenewsapp.R | |
import com.zk.samplenewsapp.model.* | |
class ArticleViewModel : ViewModel() { |
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
package com.zk.samplenewsapp.views.detail | |
import android.content.Intent | |
import android.net.Uri | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.fragment.app.Fragment | |
import androidx.lifecycle.Observer |
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
is Event.AddToFavouritesClicked -> viewAction.postValue(ViewEffect.ShowSnackBar( | |
R.string.button_result_text | |
)) |
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
sealed class Lce<T> { | |
class Loading<T> : Lce<T>() | |
data class Content<T>(val packet: T) : Lce<T>() | |
data class Error<T>(val packet: T) : Lce<T>() | |
} | |
//Test code |
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
private fun getUsersFromApi(numOfUsers: Int) { | |
resultToViewState(Lce.Loading()) | |
try { | |
viewModelScope.launch(Dispatchers.IO) { | |
val usersFromApi = mutableListOf<UsersResponse?>() | |
repeat(numOfUsers) { | |
//Wait and execute together async | |
val usersFromApiDeferred = async { repository.getUsers() } | |
val userFromApi = usersFromApiDeferred.await() | |
usersFromApi.add(userFromApi) |
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
/* | |
To extend this class, make sure it's descendants | |
add android:theme="@style/Theme.Transparent" to it's manifest tag | |
*/ | |
@OptIn(ExperimentalMaterialApi::class) | |
abstract class ComposeBottomSheetActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { |
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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<style name="Theme.Transparent" parent="Theme.AppCompat"> | |
<!-- We want the activity to be transparent --> | |
<item name="android:windowBackground">@android:color/transparent</item> | |
<!-- Make sure there is no action bar/title --> | |
<item name="android:windowNoTitle">true</item> | |
<item name="windowActionBar">false</item> |
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
private fun Activity.showAsBottomSheet(content: @Composable () -> Unit) { | |
val view = this.findViewById(android.R.id.content) as ViewGroup | |
val bottomSheet = ComposeView(this).apply { | |
val composeView = this | |
setContent { | |
val coroutineScope = rememberCoroutineScope() | |
val modalBottomSheetState = | |
rememberModalBottomSheetState(ModalBottomSheetValue.Hidden) | |
val isSheetOpened = remember { mutableStateOf(false) } |
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
// Extension for Activity | |
fun Activity.showAsBottomSheet(content: @Composable (() -> Unit) -> Unit) { | |
val viewGroup = this.findViewById(android.R.id.content) as ViewGroup | |
addContentToView(viewGroup, content) | |
} | |
// Extension for Fragment | |
fun Fragment.showAsBottomSheet(content: @Composable (() -> Unit) -> Unit) { | |
val viewGroup = requireActivity().findViewById(android.R.id.content) as ViewGroup | |
addContentToView(viewGroup, content) |