Skip to content

Commit

Permalink
[feat] viewmodels
Browse files Browse the repository at this point in the history
  • Loading branch information
reezung committed Feb 20, 2024
1 parent b2794f0 commit 87769db
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.sungdongwalk.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.sungdongwalk.location.LocationManager
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class LocationViewModel: ViewModel(){

companion object{
val instance = LocationViewModel()
}

private val _location = MutableStateFlow(Pair("",""))
val location : StateFlow<Pair<String, String>> = _location
fun updateLocation(longitude: Double, latitude: Double){
viewModelScope.launch {
_location.emit(Pair(longitude.toString(), latitude.toString()))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.sungdongwalk.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.sungdongwalk.api.Dto
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class PlaceViewModel: ViewModel(){

companion object{
val instance = PlaceViewModel()
}

private val _places = MutableStateFlow<List<Dto.SimplePlaceVo>>(listOf())
val places : StateFlow<List<Dto.SimplePlaceVo>> = _places

fun updatePlaces(newPlaces: List<Dto.SimplePlaceVo>){
viewModelScope.launch {
_places.emit(newPlaces)
}
}

private val _markers = MutableStateFlow<List<Dto.MarkerVo>>(listOf())
val markers : StateFlow<List<Dto.MarkerVo>> = _markers

fun updateMarkers(newMarkers: List<Dto.MarkerVo>){
viewModelScope.launch {
_markers.emit(newMarkers)
println(newMarkers)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.sungdongwalk.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.sungdongwalk.api.Dto
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

enum class SearchActionType {
ENTER ,DELETE
}

class SearchViewModel: ViewModel() {

companion object{
val instance = SearchViewModel()
}

private val _searchKeyword = MutableStateFlow("")
val searchKeyword : StateFlow<String> = _searchKeyword

fun updateSearchKeyword(keyword: String){
viewModelScope.launch {
_searchKeyword.emit(keyword)
}
}

private val _searchResults = MutableStateFlow<List<Dto.MarkerVo>>(listOf())
val searchResults: StateFlow<List<Dto.MarkerVo>> = _searchResults

fun updateSearchResults(actionType: SearchActionType, results: List<Dto.MarkerVo>?){
viewModelScope.launch {
when(actionType){
SearchActionType.ENTER -> _searchResults.emit(results!!)
SearchActionType.DELETE -> _searchResults.emit(listOf())
}
}
}
}

0 comments on commit 87769db

Please sign in to comment.