1- /*
2- * This file is a part of SensorSpot (https://www.github.com/UmerCodez/SensorSpot)
3- * Copyright (C) 2025 Umer Farooq ([email protected] ) 4- *
5- * SensorSpot is free software: you can redistribute it and/or modify
6- * it under the terms of the GNU General Public License as published by
7- * the Free Software Foundation, either version 3 of the License, or
8- * (at your option) any later version.
9- *
10- * SensorSpot is distributed in the hope that it will be useful,
11- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13- * GNU General Public License for more details.
14- *
15- * You should have received a copy of the GNU General Public License
16- * along with SensorSpot. If not, see <https://www.gnu.org/licenses/>.
17- *
18- */
19- package com.github.umercodez.sensorspot.ui.screens.sensors
20-
21- import android.util.Log
22- import androidx.lifecycle.ViewModel
23- import androidx.lifecycle.viewModelScope
24- import com.github.umercodez.sensorspot.data.repositories.settings.sensor.SensorsRepository
25- import com.github.umercodez.sensorspot.data.utils.LocationPermissionUtil
26- import dagger.hilt.android.lifecycle.HiltViewModel
27- import kotlinx.coroutines.flow.MutableStateFlow
28- import kotlinx.coroutines.flow.asStateFlow
29- import kotlinx.coroutines.flow.update
30- import kotlinx.coroutines.launch
31- import javax.inject.Inject
32-
33-
34- @HiltViewModel
35- class SensorScreenViewModel @Inject constructor(
36- private val sensorsRepository : SensorsRepository ,
37- private val locationPermissionUtil : LocationPermissionUtil
38- ): ViewModel(){
39-
40- private val _uiState = MutableStateFlow (SensorsScreenState ())
41- val uiState = _uiState .asStateFlow()
42- val tag = " SensorScreenViewModel"
43-
44- init {
45-
46- Log .d(tag, " init() ${hashCode()} " )
47-
48-
49- viewModelScope.launch{
50- _uiState .value.allSensors.clear()
51- _uiState .value.allSensors.addAll(sensorsRepository.getAvailableSensors())
52-
53- sensorsRepository.getSelectedSensors().forEach { sensor ->
54- _uiState .value.sensorSelectionState[sensor] = true
55- }
56-
57- _uiState .update {
58- it.copy(locationPermissionGranted = locationPermissionUtil.isLocationPermissionGranted())
59- }
60-
61- }
62-
63- }
64-
65- fun onEvent (event : SensorsScreenEvent ) {
66- when (event) {
67- is SensorsScreenEvent .OnSensorItemCheckedChange -> {
68- if (event.checked) {
69- _uiState .value.sensorSelectionState[event.sensor] = true
70- } else {
71- _uiState .value.sensorSelectionState[event.sensor] = false
72- }
73-
74- viewModelScope.launch {
75- val selectedSensors = _uiState .value.sensorSelectionState.filterValues { it == true }.keys.toList()
76- sensorsRepository.saveSelectedSensors(selectedSensors)
77- }
78- }
79-
80- is SensorsScreenEvent .OnGpsItemCheckedChange -> {
81- viewModelScope.launch {
82-
83- val gpsChecked = event.checked && locationPermissionUtil.isLocationPermissionGranted()
84-
85- _uiState .update {
86- it.copy(gpsChecked = gpsChecked)
87- }
88- sensorsRepository.saveGpsSelectionState(gpsChecked)
89- }
90- }
91- is SensorsScreenEvent .OnGrantLocationPermissionClick -> {
92-
93- }
94- }
95- }
96-
97- fun onLocationPermissionStateChange () {
98- _uiState .update {
99- it.copy(locationPermissionGranted = locationPermissionUtil.isLocationPermissionGranted())
100- }
101-
102- }
103-
1+ /*
2+ * This file is a part of SensorSpot (https://www.github.com/UmerCodez/SensorSpot)
3+ * Copyright (C) 2025 Umer Farooq ([email protected] ) 4+ *
5+ * SensorSpot is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * (at your option) any later version.
9+ *
10+ * SensorSpot is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU General Public License
16+ * along with SensorSpot. If not, see <https://www.gnu.org/licenses/>.
17+ *
18+ */
19+ package com.github.umercodez.sensorspot.ui.screens.sensors
20+
21+ import android.util.Log
22+ import androidx.lifecycle.ViewModel
23+ import androidx.lifecycle.viewModelScope
24+ import com.github.umercodez.sensorspot.data.repositories.settings.SettingsRepository
25+ import com.github.umercodez.sensorspot.data.repositories.settings.sensor.SensorsRepository
26+ import com.github.umercodez.sensorspot.data.utils.LocationPermissionUtil
27+ import dagger.hilt.android.lifecycle.HiltViewModel
28+ import kotlinx.coroutines.flow.MutableStateFlow
29+ import kotlinx.coroutines.flow.asStateFlow
30+ import kotlinx.coroutines.flow.update
31+ import kotlinx.coroutines.launch
32+ import javax.inject.Inject
33+
34+
35+ @HiltViewModel
36+ class SensorScreenViewModel @Inject constructor(
37+ private val sensorsRepository : SensorsRepository ,
38+ private val locationPermissionUtil : LocationPermissionUtil ,
39+ private val settingsRepository : SettingsRepository
40+ ): ViewModel(){
41+
42+ private val _uiState = MutableStateFlow (SensorsScreenState ())
43+ val uiState = _uiState .asStateFlow()
44+ val tag = " SensorScreenViewModel"
45+
46+ init {
47+
48+ Log .d(tag, " init() ${hashCode()} " )
49+
50+
51+ viewModelScope.launch{
52+ _uiState .value.allSensors.clear()
53+ _uiState .value.allSensors.addAll(sensorsRepository.getAvailableSensors())
54+
55+ sensorsRepository.getSelectedSensors().forEach { sensor ->
56+ _uiState .value.sensorSelectionState[sensor] = true
57+ }
58+
59+ _uiState .update {
60+ it.copy(locationPermissionGranted = locationPermissionUtil.isLocationPermissionGranted())
61+ }
62+ }
63+
64+ viewModelScope.launch {
65+ settingsRepository.settings.collect { settings ->
66+ _uiState .update {
67+ it.copy(dedicatedTopics = settings.dedicatedTopics)
68+ }
69+ }
70+ }
71+
72+ }
73+
74+ fun onEvent (event : SensorsScreenEvent ) {
75+ when (event) {
76+ is SensorsScreenEvent .OnSensorItemCheckedChange -> {
77+ if (event.checked) {
78+ _uiState .value.sensorSelectionState[event.sensor] = true
79+ } else {
80+ _uiState .value.sensorSelectionState[event.sensor] = false
81+ }
82+
83+ viewModelScope.launch {
84+ val selectedSensors = _uiState .value.sensorSelectionState.filterValues { it == true }.keys.toList()
85+ sensorsRepository.saveSelectedSensors(selectedSensors)
86+ }
87+ }
88+
89+ is SensorsScreenEvent .OnGpsItemCheckedChange -> {
90+ viewModelScope.launch {
91+
92+ val gpsChecked = event.checked && locationPermissionUtil.isLocationPermissionGranted()
93+
94+ _uiState .update {
95+ it.copy(gpsChecked = gpsChecked)
96+ }
97+ sensorsRepository.saveGpsSelectionState(gpsChecked)
98+ }
99+ }
100+ is SensorsScreenEvent .OnGrantLocationPermissionClick -> {
101+
102+ }
103+ }
104+ }
105+
106+ fun onLocationPermissionStateChange () {
107+ _uiState .update {
108+ it.copy(locationPermissionGranted = locationPermissionUtil.isLocationPermissionGranted())
109+ }
110+
111+ }
112+
104113}
0 commit comments