persistence/sqldelight support

This commit is contained in:
John O'Reilly 2020-01-25 15:27:17 +00:00
parent 735a049bba
commit 1ca3284e10
13 changed files with 115 additions and 9 deletions

View file

@ -1,6 +1,7 @@
package com.surrus.peopleinspace
import android.app.Application
import com.surrus.common.repository.appContext
import com.surrus.peopleinspace.di.appModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
@ -11,6 +12,8 @@ class PeopleInSpaceApplication : Application() {
override fun onCreate() {
super.onCreate()
appContext = this
startKoin {
androidLogger()
androidContext(this@PeopleInSpaceApplication)

View file

@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.surrus.common.remote.Assignment
import com.surrus.common.repository.PeopleInSpaceRepository
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
class PeopleInSpaceViewModel(peopleInSpaceRepository: PeopleInSpaceRepository) : ViewModel() {
@ -12,8 +13,10 @@ class PeopleInSpaceViewModel(peopleInSpaceRepository: PeopleInSpaceRepository) :
init {
viewModelScope.launch {
val people = peopleInSpaceRepository.fetchPeople()
peopleInSpace.value = people
peopleInSpaceRepository.fetchPeopleAsFlow().collect {
peopleInSpace.value = it
}
}
}
}

View file

@ -10,6 +10,7 @@ buildscript {
classpath "com.android.tools.build:gradle:${Versions.androidBuildToolsVersion}"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}"
classpath "org.jetbrains.kotlin:kotlin-serialization:${Versions.kotlin}"
classpath "com.squareup.sqldelight:gradle-plugin:${Versions.sqlDelight}"
}
}

View file

@ -1,13 +1,13 @@
object Versions {
const val androidBuildToolsVersion = "4.0.0-alpha08"
const val androidBuildToolsVersion = "4.0.0-alpha09"
const val kotlin = "1.3.61"
const val kotlinCoroutines = "1.3.3"
const val koin = "2.0.0"
const val ktor = "1.3.0-rc"
const val kotlinxSerialization = "0.14.0"
const val sqlDelight = "1.2.0"
const val sqlDelight = "1.2.2"
const val retrofit = "2.4.0"
const val okHttp = "3.12.0"
const val ktx = "1.0.1"

View file

@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'
apply plugin: 'org.jetbrains.kotlin.native.cocoapods'
apply plugin: "com.squareup.sqldelight"
android {
@ -74,6 +74,10 @@ kotlin {
// Serialize
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:${Versions.kotlinxSerialization}"
// SQL Delight
implementation "com.squareup.sqldelight:runtime:${Versions.sqlDelight}"
implementation "com.squareup.sqldelight:coroutines-extensions:${Versions.sqlDelight}"
}
androidMain.dependencies {
@ -93,6 +97,10 @@ kotlin {
// Serialize
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinxSerialization}"
// SQL Delight
implementation "com.squareup.sqldelight:android-driver:${Versions.sqlDelight}"
implementation "com.squareup.sqldelight:coroutines-extensions-jvm:${Versions.sqlDelight}"
}
iOSMain.dependencies {
@ -113,6 +121,9 @@ kotlin {
// Serialize
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:${Versions.kotlinxSerialization}"
// SQL Delight
implementation "com.squareup.sqldelight:native-driver:${Versions.sqlDelight}"
}
watchMain.dependencies {
@ -132,6 +143,16 @@ kotlin {
// Serialize
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:${Versions.kotlinxSerialization}"
// SQL Delight
implementation "com.squareup.sqldelight:native-driver:${Versions.sqlDelight}"
}
}
}
sqldelight {
PeopleInSpaceDatabase {
packageName = "com.surrus.peopleinspace.db"
sourceFolders = ["sqldelight"]
}
}

View file

@ -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)
}

View file

@ -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
suspend fun fetchPeople() : List<Assignment> {
val result = peopleInSpaceApi.fetchPeople()
return result.people
init {
GlobalScope.launch (Dispatchers.Main) {
fetchAndStorePeople()
}
}
fun fetchPeopleAsFlow() = peopleInSpaceQueries.selectAll(mapper = { name, craft ->
Assignment(name = name, craft = craft)
}).asFlow().mapToList()
suspend fun fetchAndStorePeople() {
val result = peopleInSpaceApi.fetchPeople()
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)
}
}
}
}

View file

@ -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;

View file

@ -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)
}

View file

@ -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)
}

View file

@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
1A40FE1923DB5C35008428A6 /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A40FE1823DB5C35008428A6 /* libsqlite3.tbd */; };
1ABD44FA23B00008008387E3 /* ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ABD44F923B00008008387E3 /* ViewModel.swift */; };
1ABFB8BE23AFF5CC003D807E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ABFB8BD23AFF5CC003D807E /* AppDelegate.swift */; };
1ABFB8C023AFF5CC003D807E /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ABFB8BF23AFF5CC003D807E /* SceneDelegate.swift */; };
@ -18,6 +19,7 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
1A40FE1823DB5C35008428A6 /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = usr/lib/libsqlite3.tbd; sourceTree = SDKROOT; };
1ABD44F923B00008008387E3 /* ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModel.swift; sourceTree = "<group>"; };
1ABFB8BA23AFF5CC003D807E /* PeopleInSpaceSwiftUI.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PeopleInSpaceSwiftUI.app; sourceTree = BUILT_PRODUCTS_DIR; };
1ABFB8BD23AFF5CC003D807E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@ -37,6 +39,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
1A40FE1923DB5C35008428A6 /* libsqlite3.tbd in Frameworks */,
1E1255057DE614781855FC02 /* libPods-PeopleInSpaceSwiftUI.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -97,6 +100,7 @@
D0F1ED5C992B526EA33236F9 /* Frameworks */ = {
isa = PBXGroup;
children = (
1A40FE1823DB5C35008428A6 /* libsqlite3.tbd */,
28681F6577A67864E2C2D9B4 /* libPods-PeopleInSpaceSwiftUI.a */,
);
name = Frameworks;

View file

@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */
085909C1847583547B705454 /* libPods-PeopleInSpaceWatch WatchKit Extension.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FD0B69EEAC732E1562EBD3CE /* libPods-PeopleInSpaceWatch WatchKit Extension.a */; };
1A40FE1E23DB5FC5008428A6 /* libsqlite3.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A40FE1A23DB5C89008428A6 /* libsqlite3.tbd */; };
FD254F9A23BE528600C19A05 /* PeopleInSpaceWatch WatchKit App.app in Embed Watch Content */ = {isa = PBXBuildFile; fileRef = FD254F9923BE528600C19A05 /* PeopleInSpaceWatch WatchKit App.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
FD254FA023BE528600C19A05 /* Interface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FD254F9E23BE528600C19A05 /* Interface.storyboard */; };
FD254FA223BE528700C19A05 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FD254FA123BE528700C19A05 /* Assets.xcassets */; };
@ -63,6 +64,8 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
1A40FE1A23DB5C89008428A6 /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = usr/lib/libsqlite3.tbd; sourceTree = SDKROOT; };
1A40FE1C23DB5D26008428A6 /* libsqlite3.0.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.0.tbd; path = usr/lib/libsqlite3.0.tbd; sourceTree = SDKROOT; };
484BF1F88A8D35F5F047B20A /* Pods-PeopleInSpaceWatch WatchKit Extension.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PeopleInSpaceWatch WatchKit Extension.release.xcconfig"; path = "Target Support Files/Pods-PeopleInSpaceWatch WatchKit Extension/Pods-PeopleInSpaceWatch WatchKit Extension.release.xcconfig"; sourceTree = "<group>"; };
BCCDA205D8481581F21A6528 /* Pods-PeopleInSpaceWatch WatchKit Extension.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PeopleInSpaceWatch WatchKit Extension.debug.xcconfig"; path = "Target Support Files/Pods-PeopleInSpaceWatch WatchKit Extension/Pods-PeopleInSpaceWatch WatchKit Extension.debug.xcconfig"; sourceTree = "<group>"; };
FD0B69EEAC732E1562EBD3CE /* libPods-PeopleInSpaceWatch WatchKit Extension.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-PeopleInSpaceWatch WatchKit Extension.a"; sourceTree = BUILT_PRODUCTS_DIR; };
@ -94,6 +97,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
1A40FE1E23DB5FC5008428A6 /* libsqlite3.tbd in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -111,6 +115,8 @@
B39C8188A1363A891BEDD9F2 /* Frameworks */ = {
isa = PBXGroup;
children = (
1A40FE1C23DB5D26008428A6 /* libsqlite3.0.tbd */,
1A40FE1A23DB5C89008428A6 /* libsqlite3.tbd */,
FD0B69EEAC732E1562EBD3CE /* libPods-PeopleInSpaceWatch WatchKit Extension.a */,
);
name = Frameworks;
@ -576,6 +582,7 @@
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = PBH8V487HB;
MARKETING_VERSION = 1.0;
OTHER_LDFLAGS = "-lsqlite3";
PRODUCT_BUNDLE_IDENTIFIER = com.surrus.PeopleInSpaceWatch;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
@ -589,6 +596,7 @@
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = PBH8V487HB;
MARKETING_VERSION = 1.0;
OTHER_LDFLAGS = "-lsqlite3";
PRODUCT_BUNDLE_IDENTIFIER = com.surrus.PeopleInSpaceWatch;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;