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 / adbcommand.sh
Created July 26, 2024 05:19
Open Leanback launcher applications on (non-leanback) devices
adb shell monkey -p packagename -c android.intent.category.LEANBACK_LAUNCHER 1
// In a Fragment
class UserFragment : Fragment() {
private val userViewModel: UserViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val userId = "12345" // Get this from arguments or somewhere else
userViewModel.fetchUserData(userId) // Called from Fragment
}
}
class SearchViewModel @Inject constructor(private val searchRepo: SearchRepository): ViewModel() {
private val _searchQuery = MutableStateFlow("")
val searchResults: StateFlow<List<SearchResult>> = _searchQuery
.debounce(300) // Add a debounce to limit requests
.filter(String::isNotEmpty) // Ignore empty queries
.flatMapLatest(searchRepository::search)
.stateIn(viewModelScope, SharingStarted.Lazily, emptyList())
class SearchViewModel‌ @Inject constructor(private val searchRepo: SearchRepository) : ViewModel() {
val searchResults: StateFlow<List<SearchResult>>
field = MutableStateFlow<List<SearchResult>>()
fun search(query: String) {
viewModelScope.launch {
val results = searchRepository.search(query)
searchResults.update { results }
}
@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()
)