-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
161 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
51 changes: 50 additions & 1 deletion
51
app/src/main/java/com/obrekht/coffeeshops/coffeeshops/ui/map/CoffeeShopsMapViewModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,57 @@ | ||
package com.obrekht.coffeeshops.coffeeshops.ui.map | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.obrekht.coffeeshops.coffeeshops.data.model.CoffeeShopPoint | ||
import com.obrekht.coffeeshops.coffeeshops.data.repository.CoffeeShopsRepository | ||
import com.obrekht.coffeeshops.coffeeshops.ui.model.CoffeeShop | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class CoffeeShopsMapViewModel @Inject constructor(): ViewModel() | ||
class CoffeeShopsMapViewModel @Inject constructor( | ||
private val coffeeShopsRepository: CoffeeShopsRepository, | ||
private val savedStateHandle: SavedStateHandle | ||
) : ViewModel() { | ||
|
||
private val args = CoffeeShopsMapFragmentArgs.fromSavedStateHandle(savedStateHandle) | ||
|
||
private val _uiState = MutableStateFlow(UiState()) | ||
val uiState = _uiState.asStateFlow() | ||
|
||
init { | ||
if (args.coffeeShopId != 0L) { | ||
viewModelScope.launch { | ||
coffeeShopsRepository.getCoffeeShopById(args.coffeeShopId)?.let { coffeeShop -> | ||
_uiState.update { | ||
it.copy(moveCameraTo = coffeeShop.point) | ||
} | ||
} | ||
} | ||
} | ||
|
||
viewModelScope.launch { | ||
coffeeShopsRepository.getCoffeeShopsStream().collect { coffeeShops -> | ||
_uiState.update { | ||
it.copy(coffeeShops = coffeeShops) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun onCameraMoved() { | ||
_uiState.update { | ||
it.copy(moveCameraTo = null) | ||
} | ||
} | ||
} | ||
|
||
data class UiState( | ||
val coffeeShops: List<CoffeeShop> = emptyList(), | ||
val moveCameraTo: CoffeeShopPoint? = null | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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