-
Notifications
You must be signed in to change notification settings - Fork 268
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
13 changed files
with
114 additions
and
8 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
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
13 changes: 13 additions & 0 deletions
13
common/src/androidMain/kotlin/com/surrus/common/repository/createDb.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.surrus.common.repository | ||
|
||
import android.content.Context | ||
import com.squareup.sqldelight.android.AndroidSqliteDriver | ||
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase | ||
|
||
|
||
lateinit var appContext: Context | ||
|
||
actual fun createDb(): PeopleInSpaceDatabase { | ||
val driver = AndroidSqliteDriver(PeopleInSpaceDatabase.Schema, appContext, "peopleinspace.db") | ||
return PeopleInSpaceDatabase(driver) | ||
} |
29 changes: 26 additions & 3 deletions
29
common/src/commonMain/kotlin/com/surrus/common/repository/PeopleInSpaceRepository.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,22 +1,45 @@ | ||
package com.surrus.common.repository | ||
|
||
import com.squareup.sqldelight.runtime.coroutines.asFlow | ||
import com.squareup.sqldelight.runtime.coroutines.mapToList | ||
import com.surrus.common.remote.Assignment | ||
import com.surrus.common.remote.PeopleInSpaceApi | ||
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.launch | ||
|
||
expect fun createDb() : PeopleInSpaceDatabase | ||
|
||
class PeopleInSpaceRepository { | ||
private val peopleInSpaceApi = PeopleInSpaceApi() | ||
private val peopleInSpaceDatabase = createDb() | ||
private val peopleInSpaceQueries = peopleInSpaceDatabase.peopleInSpaceQueries | ||
|
||
init { | ||
GlobalScope.launch (Dispatchers.Main) { | ||
fetchAndStorePeople() | ||
} | ||
} | ||
|
||
fun fetchPeopleAsFlow() = peopleInSpaceQueries.selectAll(mapper = { name, craft -> | ||
Assignment(name = name, craft = craft) | ||
}).asFlow().mapToList() | ||
|
||
suspend fun fetchPeople() : List<Assignment> { | ||
suspend fun fetchAndStorePeople() { | ||
val result = peopleInSpaceApi.fetchPeople() | ||
return result.people | ||
result.people.forEach { | ||
peopleInSpaceQueries.insertItem(it.name, it.craft) | ||
} | ||
} | ||
|
||
// called from iOS/watchOS client | ||
fun fetchPeople(success: (List<Assignment>) -> Unit) { | ||
GlobalScope.launch(Dispatchers.Main) { | ||
success(fetchPeople()) | ||
fetchPeopleAsFlow().collect { | ||
success(it) | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
common/src/commonMain/sqldelight/com/surrus/peopleinspace/db/PeopleInSpace.sq
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
CREATE TABLE People( | ||
name TEXT NOT NULL PRIMARY KEY, | ||
craft TEXT NOT NULL | ||
); | ||
|
||
insertItem: | ||
INSERT OR REPLACE INTO People(name, craft)VALUES(?,?); | ||
|
||
selectAll: | ||
SELECT * FROM People; | ||
|
9 changes: 9 additions & 0 deletions
9
common/src/iOSMain/kotlin/com/surrus/common/repository/createDb.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.surrus.common.repository | ||
|
||
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver | ||
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase | ||
|
||
actual fun createDb(): PeopleInSpaceDatabase { | ||
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db") | ||
return PeopleInSpaceDatabase(driver) | ||
} |
9 changes: 9 additions & 0 deletions
9
common/src/watchMain/kotlin/com/surrus/common/repository/createDb.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.surrus.common.repository | ||
|
||
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver | ||
import com.surrus.peopleinspace.db.PeopleInSpaceDatabase | ||
|
||
actual fun createDb(): PeopleInSpaceDatabase { | ||
val driver = NativeSqliteDriver(PeopleInSpaceDatabase.Schema, "peopleinspace.db") | ||
return PeopleInSpaceDatabase(driver) | ||
} |
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
Binary file modified
BIN
+6.6 KB
(140%)
...InSpaceSwiftUI.xcworkspace/xcuserdata/joreilly.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
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