mirror of
https://github.com/rt-bishop/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Created separate ViewModel for EntriesFragment.kt
This commit is contained in:
1 parent
af7a4d03d2
commit
f370aef0a7
3 files changed
+86
-50
No files matched your search
@@ -17,13 +17,10 @@
|
||||
*/
|
||||
package com.rtbishop.look4sat.ui
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.*
|
||||
import com.github.amsacode.predict4java.Satellite
|
||||
import com.rtbishop.look4sat.data.model.Result
|
||||
import com.rtbishop.look4sat.data.model.SatItem
|
||||
import com.rtbishop.look4sat.data.model.SatPass
|
||||
import com.rtbishop.look4sat.data.model.TleSource
|
||||
import com.rtbishop.look4sat.data.repository.PrefsRepo
|
||||
import com.rtbishop.look4sat.data.repository.SatelliteRepo
|
||||
import com.rtbishop.look4sat.utility.getPredictor
|
||||
@@ -31,12 +28,10 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.system.measureTimeMillis
|
||||
|
||||
@FlowPreview
|
||||
@HiltViewModel
|
||||
@@ -45,47 +40,16 @@ class SharedViewModel @Inject constructor(
|
||||
private val satelliteRepo: SatelliteRepo,
|
||||
) : ViewModel() {
|
||||
|
||||
private val satDataState = MutableSharedFlow<Result<List<SatItem>>>(replay = 0)
|
||||
private val satDataFlow = satelliteRepo.satDataFlow.map { Result.Success(it) }
|
||||
val satData = flowOf(satDataState, satDataFlow).flattenMerge().asLiveData()
|
||||
|
||||
private val _passes = MutableStateFlow<Result<MutableList<SatPass>>>(Result.InProgress)
|
||||
val passes: LiveData<Result<MutableList<SatPass>>> = _passes.asLiveData()
|
||||
|
||||
init {
|
||||
if (prefsRepo.isFirstLaunch()) {
|
||||
updateSatDataFromSources()
|
||||
prefsRepo.setFirstLaunchDone()
|
||||
}
|
||||
calculatePasses()
|
||||
}
|
||||
|
||||
fun updateSatDataFromFile(uri: Uri) {
|
||||
viewModelScope.launch {
|
||||
satDataState.emit(Result.InProgress)
|
||||
try {
|
||||
satelliteRepo.updateSatDataFromFile(uri)
|
||||
} catch (exception: Exception) {
|
||||
satDataState.emit(Result.Error(exception))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateSatDataFromSources(sources: List<TleSource> = prefsRepo.loadTleSources()) {
|
||||
viewModelScope.launch {
|
||||
satDataState.emit(Result.InProgress)
|
||||
val updateMillis = measureTimeMillis {
|
||||
try {
|
||||
satelliteRepo.updateSatDataFromWeb(sources)
|
||||
prefsRepo.saveTleSources(sources)
|
||||
} catch (exception: Exception) {
|
||||
satDataState.emit(Result.Error(exception))
|
||||
}
|
||||
}
|
||||
Timber.d("Update from WEB took $updateMillis ms")
|
||||
}
|
||||
}
|
||||
|
||||
fun getAppTimer() = liveData {
|
||||
while (true) {
|
||||
emit(System.currentTimeMillis())
|
||||
@@ -93,7 +57,6 @@ class SharedViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun getSources() = prefsRepo.loadTleSources()
|
||||
fun getTransmittersForSat(satId: Int) = satelliteRepo.getTransmittersForSat(satId).asLiveData()
|
||||
|
||||
fun calculatePasses(dateNow: Date = Date(System.currentTimeMillis())) {
|
||||
@@ -108,15 +71,6 @@ class SharedViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun updateItemsSelection(items: List<SatItem>) {
|
||||
_passes.value = Result.InProgress
|
||||
viewModelScope.launch {
|
||||
val selectedEntries = items.filter { it.isSelected }.map { it.catNum }
|
||||
satelliteRepo.updateEntriesSelection(selectedEntries)
|
||||
calculatePasses()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getPasses(satellite: Satellite, dateNow: Date): MutableList<SatPass> {
|
||||
val predictor = satellite.getPredictor(prefsRepo.getStationPosition())
|
||||
val passes = predictor.getPasses(dateNow, prefsRepo.getHoursAhead(), true)
|
||||
|
||||
@@ -21,7 +21,7 @@ import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.navigation.findNavController
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
@@ -30,7 +30,6 @@ import com.rtbishop.look4sat.R
|
||||
import com.rtbishop.look4sat.data.model.Result
|
||||
import com.rtbishop.look4sat.data.model.TleSource
|
||||
import com.rtbishop.look4sat.databinding.FragmentEntriesBinding
|
||||
import com.rtbishop.look4sat.ui.SharedViewModel
|
||||
import com.rtbishop.look4sat.utility.RecyclerDivider
|
||||
import com.rtbishop.look4sat.utility.getNavResult
|
||||
import com.rtbishop.look4sat.utility.navigateSafe
|
||||
@@ -42,7 +41,7 @@ import kotlinx.coroutines.FlowPreview
|
||||
@AndroidEntryPoint
|
||||
class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
||||
|
||||
private val viewModel: SharedViewModel by activityViewModels()
|
||||
private val viewModel: EntriesViewModel by viewModels()
|
||||
private val filePicker =
|
||||
registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||
uri?.let { viewModel.updateSatDataFromFile(uri) }
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Look4Sat. Amateur radio satellite tracker and pass predictor.
|
||||
* Copyright (C) 2019-2021 Arty Bishop (bishop.arty@gmail.com)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.rtbishop.look4sat.ui.entriesScreen
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.asLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.rtbishop.look4sat.data.model.Result
|
||||
import com.rtbishop.look4sat.data.model.SatItem
|
||||
import com.rtbishop.look4sat.data.model.TleSource
|
||||
import com.rtbishop.look4sat.data.repository.PrefsRepo
|
||||
import com.rtbishop.look4sat.data.repository.SatelliteRepo
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.flattenMerge
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import kotlin.system.measureTimeMillis
|
||||
|
||||
@FlowPreview
|
||||
@HiltViewModel
|
||||
class EntriesViewModel @Inject constructor(
|
||||
private val prefsRepo: PrefsRepo,
|
||||
private val satelliteRepo: SatelliteRepo
|
||||
) : ViewModel() {
|
||||
|
||||
private val satDataState = MutableSharedFlow<Result<List<SatItem>>>(replay = 0)
|
||||
private val satDataFlow = satelliteRepo.satDataFlow.map { Result.Success(it) }
|
||||
val satData = flowOf(satDataState, satDataFlow).flattenMerge().asLiveData()
|
||||
|
||||
fun updateSatDataFromFile(uri: Uri) {
|
||||
viewModelScope.launch {
|
||||
satDataState.emit(Result.InProgress)
|
||||
try {
|
||||
satelliteRepo.updateSatDataFromFile(uri)
|
||||
} catch (exception: Exception) {
|
||||
satDataState.emit(Result.Error(exception))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateSatDataFromSources(sources: List<TleSource> = prefsRepo.loadTleSources()) {
|
||||
viewModelScope.launch {
|
||||
satDataState.emit(Result.InProgress)
|
||||
val updateMillis = measureTimeMillis {
|
||||
try {
|
||||
satelliteRepo.updateSatDataFromWeb(sources)
|
||||
prefsRepo.saveTleSources(sources)
|
||||
} catch (exception: Exception) {
|
||||
satDataState.emit(Result.Error(exception))
|
||||
}
|
||||
}
|
||||
Timber.d("Update from WEB took $updateMillis ms")
|
||||
}
|
||||
}
|
||||
|
||||
fun updateItemsSelection(items: List<SatItem>) {
|
||||
viewModelScope.launch {
|
||||
val selectedEntries = items.filter { it.isSelected }.map { it.catNum }
|
||||
satelliteRepo.updateEntriesSelection(selectedEntries)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user