This commit is contained in:
John O'Reilly 2021-12-05 08:02:46 +00:00
parent 26575b2e33
commit 6a0d192d80
5 changed files with 218 additions and 75 deletions

View file

@ -1,20 +1,63 @@
package com.surrus.peopleinspace.ui
import androidx.constraintlayout.widget.ConstraintAttribute
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
import aws.sdk.kotlin.services.dynamodb.model.QueryRequest
import co.touchlab.kermit.Kermit
import com.surrus.common.remote.Assignment
import com.surrus.common.repository.PeopleInSpaceRepository
import com.surrus.common.repository.PeopleInSpaceRepositoryInterface
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import aws.sdk.kotlin.services.dynamodb.model.ScanRequest
class PeopleInSpaceViewModel(
private val peopleInSpaceRepository: PeopleInSpaceRepositoryInterface
) : ViewModel() {
val peopleInSpace = peopleInSpaceRepository.fetchPeopleAsFlow()
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())
val staticCredentials = StaticCredentialsProvider {
accessKeyId = ""
secretAccessKey = ""
}
val dynamoDbClient = DynamoDbClient{
region = "eu-west-1"
credentialsProvider = staticCredentials
}
val table = "PeopleInSpace"
val peopleInSpace = MutableStateFlow<List<Assignment>>(emptyList())
init {
viewModelScope.launch {
val scanRequest = ScanRequest {
tableName = table
}
val people = mutableListOf<Assignment>()
val result = dynamoDbClient.scan(scanRequest)
result.items?.forEach { item ->
val id = (item["id"] as AttributeValue.S).value
val craft = (item["craft"] as AttributeValue.S).value
val personImageUrl = (item["personImageUrl"] as AttributeValue.S).value
val person = Assignment(id, craft, personImageUrl)
people.add(person)
}
peopleInSpace.value = people
}
}
// val peopleInSpace = peopleInSpaceRepository.fetchPeopleAsFlow()
// .stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())
val issPosition = peopleInSpaceRepository.pollISSPosition()

View file

@ -1,5 +1,5 @@
object Versions {
const val androidMinSdk = 21
const val androidMinSdk = 24
const val androidCompileSdk = 31
const val androidTargetSdk = androidCompileSdk

View file

@ -4,10 +4,10 @@ plugins {
kotlin("multiplatform")
id("kotlinx-serialization")
id("com.android.library")
id("org.jetbrains.kotlin.native.cocoapods")
//id("org.jetbrains.kotlin.native.cocoapods")
id("com.squareup.sqldelight")
id("com.rickclephas.kmp.nativecoroutines")
id("com.chromaticnoise.multiplatform-swiftpackage") version "2.0.3"
//id("com.chromaticnoise.multiplatform-swiftpackage") version "2.0.3"
}
// CocoaPods requires the podspec to have a version.
@ -28,49 +28,49 @@ android {
}
// Workaround for https://youtrack.jetbrains.com/issue/KT-43944
android {
configurations {
create("androidTestApi")
create("androidTestDebugApi")
create("androidTestReleaseApi")
create("testApi")
create("testDebugApi")
create("testReleaseApi")
}
}
//android {
// configurations {
// create("androidTestApi")
// create("androidTestDebugApi")
// create("androidTestReleaseApi")
// create("testApi")
// create("testDebugApi")
// create("testReleaseApi")
// }
//}
kotlin {
val sdkName: String? = System.getenv("SDK_NAME")
// val sdkName: String? = System.getenv("SDK_NAME")
//
// val isiOSDevice = sdkName.orEmpty().startsWith("iphoneos")
// if (isiOSDevice) {
// iosArm64("iOS")
// } else {
// iosX64("iOS")
// }
//
// val isWatchOSDevice = sdkName.orEmpty().startsWith("watchos")
// if (isWatchOSDevice) {
// watchosArm64("watch")
// } else {
// watchosX64("watch")
// }
val isiOSDevice = sdkName.orEmpty().startsWith("iphoneos")
if (isiOSDevice) {
iosArm64("iOS")
} else {
iosX64("iOS")
}
val isWatchOSDevice = sdkName.orEmpty().startsWith("watchos")
if (isWatchOSDevice) {
watchosArm64("watch")
} else {
watchosX64("watch")
}
macosX64("macOS")
// macosX64("macOS")
android()
jvm()
cocoapods {
// Configure fields required by CocoaPods.
summary = "PeopleInSpace"
homepage = "https://github.com/joreilly/PeopleInSpace"
noPodspec()
}
// cocoapods {
// // Configure fields required by CocoaPods.
// summary = "PeopleInSpace"
// homepage = "https://github.com/joreilly/PeopleInSpace"
// noPodspec()
// }
js(IR) {
useCommonJs()
browser()
}
// js(IR) {
// useCommonJs()
// browser()
// }
sourceSets {
sourceSets["commonMain"].dependencies {
@ -78,6 +78,9 @@ kotlin {
isForce = true
}
api("aws.sdk.kotlin:dynamodb:0.9.4-beta")
with(Deps.Ktor) {
implementation(clientCore)
implementation(clientJson)
@ -109,6 +112,7 @@ kotlin {
sourceSets["androidMain"].dependencies {
implementation(Deps.Ktor.clientAndroid)
implementation(Deps.SqlDelight.androidDriver)
//implementation("aws.sdk.kotlin:dynamodb:0.9.4-beta")
}
sourceSets["androidTest"].dependencies {
// having issue with following after update to Kotlin 1.5.21
@ -124,26 +128,26 @@ kotlin {
implementation(Deps.Log.slf4j)
}
sourceSets["iOSMain"].dependencies {
implementation(Deps.Ktor.clientIos)
implementation(Deps.SqlDelight.nativeDriver)
}
sourceSets["iOSTest"].dependencies {
}
sourceSets["watchMain"].dependencies {
implementation(Deps.Ktor.clientIos)
implementation(Deps.SqlDelight.nativeDriver)
}
sourceSets["macOSMain"].dependencies {
implementation(Deps.Ktor.clientIos)
implementation(Deps.SqlDelight.nativeDriverMacos)
}
sourceSets["jsMain"].dependencies {
implementation(Deps.Ktor.clientJs)
}
// sourceSets["iOSMain"].dependencies {
// implementation(Deps.Ktor.clientIos)
// implementation(Deps.SqlDelight.nativeDriver)
// }
// sourceSets["iOSTest"].dependencies {
// }
//
// sourceSets["watchMain"].dependencies {
// implementation(Deps.Ktor.clientIos)
// implementation(Deps.SqlDelight.nativeDriver)
// }
//
// sourceSets["macOSMain"].dependencies {
// implementation(Deps.Ktor.clientIos)
// implementation(Deps.SqlDelight.nativeDriverMacos)
// }
//
// sourceSets["jsMain"].dependencies {
// implementation(Deps.Ktor.clientJs)
// }
}
}
@ -160,10 +164,10 @@ sqldelight {
}
}
multiplatformSwiftPackage {
packageName("PeopleInSpace")
swiftToolsVersion("5.3")
targetPlatforms {
iOS { v("13") }
}
}
//multiplatformSwiftPackage {
// packageName("PeopleInSpace")
// swiftToolsVersion("5.3")
// targetPlatforms {
// iOS { v("13") }
// }
//}

View file

@ -1,13 +1,109 @@
package com.surrus
import aws.sdk.kotlin.runtime.UnknownServiceErrorException
import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
import aws.sdk.kotlin.services.dynamodb.model.*
import com.surrus.common.di.initKoin
import com.surrus.common.remote.PeopleInSpaceApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
fun main() {
runBlocking {
fun main() = runBlocking {
val koin = initKoin(enableNetworkLogs = true).koin
val api = koin.get<PeopleInSpaceApi>()
println(api.fetchPeople())
val peopleInSpace = koin.get<PeopleInSpaceApi>()
val dynamoDbClient = DynamoDbClient { region = "eu-west-1" }
val table = "PeopleInSpace"
val key = "id"
try {
tutorialSetup(dynamoDbClient, table, key)
println("Writing to table...")
val people = peopleInSpace.fetchPeople().people
people.forEach { person ->
val itemValues = mutableMapOf<String, AttributeValue>()
itemValues["id"] = AttributeValue.S(person.name)
itemValues["craft"] = AttributeValue.S(person.craft)
itemValues["personImageUrl"] = AttributeValue.S(person.personImageUrl!!)
dynamoDbClient.putItem {
tableName = table
item = itemValues
}
}
println("Completed writing to table.")
println()
//cleanUp(dynamoDbClient, table)
} catch (e: DynamoDbException) {
println("ERROR (DynamoDbException): " + e.message)
} catch (e: UnknownServiceErrorException) {
println("ERROR (UnknownServiceErrorException): " + e.message)
} finally {
dynamoDbClient.close()
}
println("Exiting...")
}
private suspend fun tutorialSetup(dynamoDbClient: DynamoDbClient, newTable: String, key: String) {
val createTableRequest = CreateTableRequest {
tableName = newTable
attributeDefinitions = listOf(
AttributeDefinition {
attributeName = key
attributeType = ScalarAttributeType.S
}
)
keySchema = listOf(
KeySchemaElement {
attributeName = key
keyType = KeyType.Hash
}
)
provisionedThroughput {
readCapacityUnits = 10
writeCapacityUnits = 10
}
}
println("Creating table: $newTable...")
dynamoDbClient.createTable(createTableRequest)
println("Waiting for table to be active...")
var tableIsActive = dynamoDbClient.describeTable {
tableName = newTable
}.table?.tableStatus == TableStatus.Active
do {
if (!tableIsActive) {
delay(500)
tableIsActive = dynamoDbClient.describeTable {
tableName = newTable
}.table?.tableStatus == TableStatus.Active
}
} while(!tableIsActive)
println("$newTable is ready.")
println()
}
private suspend fun cleanUp(dynamoDbClient: DynamoDbClient, newTable: String) {
println("Cleaning up...")
println("Deleting table: $newTable...")
dynamoDbClient.deleteTable {
tableName = newTable
}
println("$newTable has been deleted.")
println()
println("Cleanup complete")
println()
}

View file

@ -9,7 +9,7 @@ pluginManagement {
rootProject.name = "PeopleInSpace"
include(":app", ":common", ":compose-desktop")
include(":web")
include(":compose-web")
//include(":web")
//include(":compose-web")
include(":backend")
include(":wearApp")