Skip to content

Instantly share code, notes, and snippets.

View zivkesten's full-sized avatar

Ziv Kesten zivkesten

View GitHub Profile
@zivkesten
zivkesten / ArticleViewModel.kt
Created June 8, 2020 06:40
MVI - viewModel event function
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
))
}
}
@zivkesten
zivkesten / ArticleViewModel.kt
Last active June 8, 2020 08:10
Article view model
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() {
@zivkesten
zivkesten / ArticleFragment.kt
Last active June 27, 2022 14:02
This is the view part of the MVI design
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
is Event.AddToFavouritesClicked -> viewAction.postValue(ViewEffect.ShowSnackBar(
R.string.button_result_text
))
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
@zivkesten
zivkesten / GetUsers
Created August 30, 2020 07:24
Simultanious api call
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)
@zivkesten
zivkesten / ComposeBottomSheetActivity.kt
Last active May 30, 2022 18:09
An activity that can be extended and can display composables as a bottom sheet
/*
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 {
@zivkesten
zivkesten / themes.xml
Last active May 30, 2022 15:05
Compose bottom sheet activity style file
<?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>
@zivkesten
zivkesten / showAsBottomSheet.kt
Created May 31, 2022 12:08
An extendion function to pull bottom sheet above your view with jetpack compose content
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) }
@zivkesten
zivkesten / BottomSheetUtils.kt
Last active June 3, 2024 14:36
Extension functions to display a Jetpack Compose bottom sheet over Android view
// 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)