scope updates

This commit is contained in:
John O'Reilly 2020-11-01 17:24:19 +00:00
parent 1ce49b81f4
commit 77b05a07ff
8 changed files with 35 additions and 34 deletions

View file

@ -55,14 +55,20 @@ class PeopleInSpaceRepository() : KoinComponent {
}
// called from Kotlin/Native clients
fun fetchPeople(success: (List<Assignment>) -> Unit) {
GlobalScope.launch(Dispatchers.Main) {
fun startObservingPeopleUpdates(success: (List<Assignment>) -> Unit) {
logger.d { "startObservingPeopleUpdates" }
coroutineScope.launch {
fetchPeopleAsFlow()?.collect {
success(it)
}
}
}
fun stopObservingPeopleUpdates() {
logger.d { "stopObservingPeopleUpdates" }
coroutineScope.cancel()
}
@Throws(Exception::class)
suspend fun fetchISSPosition() = peopleInSpaceApi.fetchISSPosition().iss_position
}

View file

@ -2,6 +2,7 @@ package com.surrus.common.repository
import co.touchlab.kermit.CommonLogger
import co.touchlab.kermit.Logger
import co.touchlab.kermit.NSLogLogger
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase
import kotlinx.coroutines.Dispatchers
@ -13,4 +14,4 @@ actual fun createDb(): PeopleInSpaceDatabase? {
return PeopleInSpaceDatabase(driver)
}
actual fun getLogger(): Logger = CommonLogger()
actual fun getLogger(): Logger = NSLogLogger()

View file

@ -13,9 +13,11 @@ struct ContentView: View {
}
}
.navigationBarTitle(Text("PeopleInSpace"), displayMode: .large)
.onAppear(perform: {
self.peopleInSpaceViewModel.fetch()
})
.onAppear {
self.peopleInSpaceViewModel.startObserving()
}.onDisappear {
self.peopleInSpaceViewModel.stopObserving()
}
}
}
}

View file

@ -10,10 +10,14 @@ class PeopleInSpaceViewModel: ObservableObject {
self.repository = repository
}
func fetch() {
repository.fetchPeople(success: { data in
func startObserving() {
repository.startObservingPeopleUpdates(success: { data in
self.people = data
})
})
}
func stopObserving() {
repository.stopObservingPeopleUpdates()
}
func getPersonBio(personName: String) -> String {

View file

@ -3,22 +3,4 @@
uuid = "9B1A8AB5-ABE0-4036-ADC6-CBD485F83CF8"
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "9FD78963-EFC5-4783-859E-F836DD285338"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "PeopleInSpace/ViewModel.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "15"
endingLineNumber = "15"
landmarkName = "fetchPeople()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>

View file

@ -15,10 +15,12 @@ struct ContentView: View {
.onReceive(timer) { _ in
self.peopleInSpaceViewModel.fetchISSPosition()
}
.onAppear(perform: {
self.peopleInSpaceViewModel.fetchPeople()
.onAppear {
self.peopleInSpaceViewModel.startObserving()
self.peopleInSpaceViewModel.fetchISSPosition()
})
}.onDisappear {
self.peopleInSpaceViewModel.stopObserving()
}
MapView(coordinate: CLLocationCoordinate2DMake(peopleInSpaceViewModel.issPosition.latitude, peopleInSpaceViewModel.issPosition.longitude))
}

View file

@ -11,12 +11,16 @@ class PeopleInSpaceViewModel: ObservableObject {
self.repository = repository
}
func fetchPeople() {
repository.fetchPeople(success: { data in
func startObserving() {
repository.startObservingPeopleUpdates(success: { data in
self.people = data
})
})
}
func stopObserving() {
repository.stopObservingPeopleUpdates()
}
func fetchISSPosition() {
repository.fetchISSPosition { (data, error) in
if let issPosition = data {

View file

@ -10,7 +10,7 @@ class PeopleInSpaceViewModel: ObservableObject {
}
func fetch() {
repository.fetchPeople(success: { data in
repository.startObservingPeopleUpdates(success: { data in
self.people = data
})
}