Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
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
@@ -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) | ||
} |
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
@@ -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) | ||
} | ||
} | ||
} | ||
} |
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
@@ -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; | ||
|
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
@@ -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
@@ -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 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