Skip to content

Commit

Permalink
Merge pull request #161 from Team-B1ND/feature/160-keystore-ios-logic
Browse files Browse the repository at this point in the history
[KeyStore] iOS Logic
  • Loading branch information
8954sood authored Aug 20, 2024
2 parents 793ec8f + 2c05eea commit dba521f
Show file tree
Hide file tree
Showing 29 changed files with 488 additions and 113 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ jobs:
- name: Run spotless
run: ./gradlew dodam-student:spotlessCheck

- name: Build with Gradle
run: ./gradlew dodam-student:clean build --parallel
- name: Android Build with Gradle
run: ./gradlew dodam-student:clean assembleRelease --parallel
15 changes: 15 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Keystore",
platforms: [.iOS(.v14)],
products: [
.library(
name: "Keystore",
targets: ["Keystore"]
)
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.kotlin.dsl.configure
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

fun Project.kotlin(block: KotlinMultiplatformExtension.() -> Unit) {
extensions.configure<KotlinMultiplatformExtension>(block)
Expand Down Expand Up @@ -48,7 +49,11 @@ fun Project.setupMultiplatform() {
}
}

fun KotlinMultiplatformExtension.setIOS(name: String, bundleId: String? = null) {
fun KotlinMultiplatformExtension.setIOS(
name: String,
bundleId: String? = null,
block: KotlinNativeTarget.() -> Unit = {}
) {
listOf(
iosX64(),
iosArm64(),
Expand All @@ -61,5 +66,6 @@ fun KotlinMultiplatformExtension.setIOS(name: String, bundleId: String? = null)
binaryOptions["bundleId"] = bundleId
}
}
iosTarget.run(block)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.b1nd.dodam.primitive

import com.b1nd.dodam.dsl.implementation
import com.b1nd.dodam.dsl.kotlin
import com.b1nd.dodam.dsl.library
import com.b1nd.dodam.dsl.libs
import com.b1nd.dodam.dsl.setupMultiplatform
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.dependencies

class MultiplatformComposePlugin: Plugin<Project> {
override fun apply(target: Project) {
Expand All @@ -17,6 +19,14 @@ class MultiplatformComposePlugin: Plugin<Project> {
apply("org.jetbrains.kotlin.multiplatform")
}
setupMultiplatform()

kotlin {
sourceSets.commonMain.dependencies {
implementation(libs.library("androidx-lifecycle-viewmodel-compose"))
implementation(libs.library("koin-compose-multiplatform"))
implementation(libs.library("koin-compose-viewmodel"))
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ package com.b1nd.dodam.common.utiles

import platform.Foundation.NSString
import platform.Foundation.stringWithFormat
import platform.darwin.NSObject

actual fun String.Companion.javaFormat(format: String, vararg args: Any?): String = NSString.stringWithFormat(format, args)
actual fun String.Companion.javaFormat(format: String, vararg args: Any?): String {
// val nsArray = arrayOf(*args.map { it as? NSObject ?: NSNull() }.toTypedArray())//args.map { it as? NSObject ?: NSNull() } //NSString.stringWithFormat(format, args)
return NSString.stringWithFormat(format, args.map { it as? NSObject })
}
4 changes: 3 additions & 1 deletion datastore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ plugins {
kotlin {
setIOS("datastore")
sourceSets {
androidMain.dependencies {
implementation(projects.keystore)
}
commonMain.dependencies {
implementation(projects.common)
implementation(projects.keystore)
implementation(libs.androidx.datastore.preferences.core)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import com.b1nd.dodam.datastore.createDataStore
import com.b1nd.dodam.datastore.repository.DatastoreRepository
import com.b1nd.dodam.datastore.repository.DataStoreRepository
import com.b1nd.dodam.datastore.repository.DataStoreRepositoryImpl
import org.koin.core.module.Module
import org.koin.dsl.module

Expand All @@ -14,7 +15,7 @@ actual val dataStoreModule: Module = module {
createDataStore(context)
}

single<DatastoreRepository> {
DatastoreRepository(get(), get())
single<DataStoreRepository> {
DataStoreRepositoryImpl(get(), get())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.b1nd.dodam.datastore.repository

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import com.b1nd.dodam.datastore.model.User
import com.b1nd.dodam.keystore.KeyStoreManager
import kotlinx.coroutines.flow.map

class DataStoreRepositoryImpl constructor(
private val dataStore: DataStore<Preferences>,
private val keyStoreManager: KeyStoreManager,
) : DataStoreRepository {
private val tokenKey = stringPreferencesKey("token")
private val idKey = stringPreferencesKey("id")
private val pwKey = stringPreferencesKey("pw")

override val user = dataStore.data.map {
User(
id = keyStoreManager.decrypt(it[idKey] ?: ""),
pw = keyStoreManager.decrypt(it[pwKey] ?: ""),
token = it[tokenKey] ?: "",
)
}

override val token = dataStore.data.map {
it[tokenKey] ?: ""
}

override suspend fun saveUser(id: String, pw: String, token: String) {
dataStore.edit {
it[idKey] = keyStoreManager.encrypt(id)
it[pwKey] = keyStoreManager.encrypt(pw)
it[tokenKey] = token
}
}

override suspend fun saveToken(token: String) {
dataStore.edit {
it[tokenKey] = token
}
}

override suspend fun deleteUser() {
dataStore.edit {
it[idKey] = ""
it[pwKey] = ""
it[tokenKey] = ""
}
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,17 @@
package com.b1nd.dodam.datastore.repository

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import com.b1nd.dodam.datastore.model.User
import com.b1nd.dodam.keystore.KeyStoreManager
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.Flow

class DatastoreRepository constructor(
private val dataStore: DataStore<Preferences>,
private val keyStoreManager: KeyStoreManager,
) {
private val tokenKey = stringPreferencesKey("token")
private val idKey = stringPreferencesKey("id")
private val pwKey = stringPreferencesKey("pw")
interface DataStoreRepository {

val user = dataStore.data.map {
User(
id = keyStoreManager.decrypt(it[idKey] ?: ""),
pw = keyStoreManager.decrypt(it[pwKey] ?: ""),
token = it[tokenKey] ?: "",
)
}
val user: Flow<User>

val token = dataStore.data.map {
it[tokenKey] ?: ""
}
val token: Flow<String>

suspend fun saveUser(id: String, pw: String, token: String) {
dataStore.edit {
it[idKey] = keyStoreManager.encrypt(id)
it[pwKey] = keyStoreManager.encrypt(pw)
it[tokenKey] = token
}
}
suspend fun saveUser(id: String, pw: String, token: String)

suspend fun saveToken(token: String) {
dataStore.edit {
it[tokenKey] = token
}
}
suspend fun saveToken(token: String)

suspend fun deleteUser() {
dataStore.edit {
it[idKey] = ""
it[pwKey] = ""
it[tokenKey] = ""
}
}
suspend fun deleteUser()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.b1nd.dodam.datastore.di
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import com.b1nd.dodam.datastore.createDataStore
import com.b1nd.dodam.datastore.repository.DatastoreRepository
import com.b1nd.dodam.datastore.repository.DataStoreRepository
import com.b1nd.dodam.datastore.repository.DataStoreRepositoryImpl
import org.koin.core.module.Module
import org.koin.dsl.module

Expand All @@ -12,7 +13,7 @@ actual val dataStoreModule: Module = module {
createDataStore()
}

single<DatastoreRepository> {
DatastoreRepository(get(), get())
single<DataStoreRepository> {
DataStoreRepositoryImpl()
}
}
Loading

0 comments on commit dba521f

Please sign in to comment.