Skip to content

Instantly share code, notes, and snippets.

View LloydBlv's full-sized avatar
🏠
Working from home

Reza Najafi LloydBlv

🏠
Working from home
View GitHub Profile
@LloydBlv
LloydBlv / MutableStateExpose.kt
Created April 7, 2024 04:45
Bad practice of exposing mutable state flow from viewmodel
class RatesViewModel constructor(
private val ratesRepository: RatesRepository,
) : ViewModel() {
val state = MutableStateFlow(RatesUiState(isLoading = true))
}
@LloydBlv
LloydBlv / immutableStateExpose.kt
Created April 7, 2024 04:44
Expose Immutable State
class RatesViewModel constructor(
private val ratesRepository: RatesRepository,
) : ViewModel() {
private val _state = MutableStateFlow(RatesUiState(isLoading = true))
val state: StateFlow<RatesUiState>
get() = _state.asStateFlow()
}
@LloydBlv
LloydBlv / SearchViewModel.kt
Created March 13, 2024 13:32
Better implementation of ViewModel, eliminating init{} block
class SearchViewModel @Inject constructor(
private val searchUseCase: dagger.Lazy<SearchUseCase>,
) : ViewModel() {
private val searchQuery = MutableStateFlow("")
val uiState: LiveData<SearchUiState> = searchQuery
.debounce(DEBOUNCE_TIME_IN_MILLIS)
.asLiveData()
.switchMap(::createUiState)
@LloydBlv
LloydBlv / SearchViewModel.kt
Created March 13, 2024 13:30
Bad implementation of ViewModel
class SearchViewModel @Inject constructor(
private val searchUseCase: SearchUseCase,
@IoDispatcher val ioDispatcher: CoroutineDispatcher
) : ViewModel() {
private val searchQuery = MutableStateFlow("")
private val _uiState = MutableLiveData<SearchUiState>()
val uiState = _uiState
@LloydBlv
LloydBlv / SearchViewModel.kt
Created March 13, 2024 13:14
Avoid using init{} block in ViewModels
class SearchViewModel @Inject constructor(
private val searchUseCase: dagger.Lazy<SearchUseCase>,
private val wordsUseCase: GetWordsUseCase,
) : ViewModel() {
data class UiState(
val isLoading: Boolean = true,
val words: List<String> = emptyList()
)
@LloydBlv
LloydBlv / SearchViewModel.kt
Created March 13, 2024 13:07
Demonstration of bad implementation in ViewModels
class SearchViewModel @Inject constructor(
private val searchUseCase: dagger.Lazy<SearchUseCase>,
private val wordsUseCase: GetWordsUseCase,
) : ViewModel() {
data class UiState(
val isLoading: Boolean,
val words: List<String> = emptyList()
)
context(Fragment)
inline fun <T> Flow<T>.collectInFragment(
crossinline onCollect: suspend (T) -> Unit
) = viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
collectLatest {
onCollect(it)
}
}
}
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@RunWith(RobolectricTestRunner::class)
class BookmarkButtonScreenshotTest {
@get:Rule
val composeRule = createComposeRule()
@Test
fun bookmarkedStateSnapshot() {
composeRule.setContent {
ScreenshotBox {
@RunWith(AndroidJUnit4::class)
class BookmarkButtonTransitionTest {
@get:Rule
val composeTestRule = createComposeRule()
@Before
@Throws(Exception::class)
fun setUp() {
ShadowLog.stream = System.out // Redirect Logcat to console
@RunWith(AndroidJUnit4::class)
class BookmarkButtonTest {
@get:Rule
val composeTestRule = createComposeRule()
@Before
@Throws(Exception::class)
fun setUp() {