-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from Team-B1ND/feature/154-kmp-network-modules
Feature/KMP Network Modules
- Loading branch information
Showing
152 changed files
with
3,138 additions
and
378 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
60 changes: 60 additions & 0 deletions
60
build-logic/src/main/kotlin/com/b1nd/dodam/dsl/MultiplatformGradleDsl.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,60 @@ | ||
package com.b1nd.dodam.dsl | ||
|
||
import org.gradle.api.JavaVersion | ||
import org.gradle.api.Project | ||
import org.gradle.kotlin.dsl.configure | ||
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension | ||
|
||
fun Project.kotlin(block: KotlinMultiplatformExtension.() -> Unit) { | ||
extensions.configure<KotlinMultiplatformExtension>(block) | ||
} | ||
|
||
@OptIn(ExperimentalKotlinGradlePluginApi::class) | ||
fun Project.setupMultiplatform() { | ||
kotlin { | ||
androidTarget { | ||
compilations.all { | ||
kotlinOptions { | ||
jvmTarget = JavaVersion.VERSION_17.toString() | ||
} | ||
} | ||
} | ||
// remove compiler warring | ||
sourceSets.commonMain { | ||
compilerOptions { | ||
freeCompilerArgs.add("-Xexpect-actual-classes") | ||
} | ||
} | ||
} | ||
|
||
android { | ||
namespace?.let { | ||
this.namespace = it | ||
} | ||
compileSdkVersion(34) | ||
|
||
defaultConfig { | ||
minSdk = 28 | ||
targetSdk = 34 | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
} | ||
} | ||
|
||
fun KotlinMultiplatformExtension.setIOS(name: String) { | ||
listOf( | ||
iosX64(), | ||
iosArm64(), | ||
iosSimulatorArm64() | ||
).forEach { | ||
it.binaries.framework { | ||
baseName = name | ||
isStatic = true | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
build-logic/src/main/kotlin/com/b1nd/dodam/primitive/MultiplatformKoinPlugin.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,21 @@ | ||
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 | ||
|
||
class MultiplatformKoinPlugin: Plugin<Project> { | ||
override fun apply(target: Project) { | ||
with(target) { | ||
kotlin { | ||
sourceSets.commonMain.dependencies { | ||
implementation(libs.library("koin-core")) | ||
} | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...-logic/src/main/kotlin/com/b1nd/dodam/primitive/MultiplatformKotlinSerializationPlugin.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,26 @@ | ||
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 org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.kotlin.dsl.dependencies | ||
|
||
class MultiplatformKotlinSerializationPlugin : Plugin<Project> { | ||
override fun apply(target: Project) { | ||
with(target) { | ||
with(pluginManager) { | ||
apply("org.jetbrains.kotlin.plugin.serialization") | ||
} | ||
kotlin { | ||
sourceSets.commonMain { | ||
dependencies { | ||
implementation(libs.library("kotlinx-serialization-json")) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
build-logic/src/main/kotlin/com/b1nd/dodam/primitive/MultiplatformPlugin.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,20 @@ | ||
package com.b1nd.dodam.primitive | ||
|
||
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 | ||
|
||
class MultiplatformPlugin: Plugin<Project> { | ||
override fun apply(target: Project) { | ||
with(target) { | ||
with(pluginManager) { | ||
apply("com.android.library") | ||
apply("org.jetbrains.kotlin.multiplatform") | ||
} | ||
setupMultiplatform() | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
import com.b1nd.dodam.dsl.setIOS | ||
|
||
plugins { | ||
alias(libs.plugins.dodam.kotlin) | ||
alias(libs.plugins.dodam.multiplatform) | ||
} | ||
|
||
dependencies { | ||
implementation(libs.koin.core) | ||
kotlin { | ||
setIOS("common") | ||
|
||
sourceSets { | ||
commonMain.dependencies { | ||
implementation(libs.koin.core) | ||
implementation(libs.kotlinx.coroutines.core) | ||
} | ||
} | ||
} | ||
|
||
|
||
android { | ||
namespace = "com.b1nd.dodam.common" | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,19 +1,29 @@ | ||
import com.b1nd.dodam.dsl.kotlin | ||
import com.b1nd.dodam.dsl.setIOS | ||
|
||
plugins { | ||
alias(libs.plugins.dodam.android) | ||
alias(libs.plugins.dodam.android.kotlin) | ||
alias(libs.plugins.dodam.kotlin.serialization) | ||
alias(libs.plugins.dodam.koin) | ||
alias(libs.plugins.dodam.multiplatform) | ||
alias(libs.plugins.dodam.multiplatform.kotlin.serialization) | ||
alias(libs.plugins.dodam.multiplatform.koin) | ||
} | ||
|
||
kotlin { | ||
setIOS("datastore") | ||
sourceSets { | ||
commonMain.dependencies { | ||
implementation(projects.common) | ||
implementation(projects.keystore) | ||
implementation(libs.androidx.datastore.preferences.core) | ||
} | ||
} | ||
} | ||
|
||
|
||
android { | ||
namespace = "com.b1nd.dodam.datastore" | ||
defaultConfig { | ||
consumerProguardFiles("consumer-rules.pro") | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(projects.common) | ||
implementation(projects.keystore) | ||
implementation(libs.androidx.datastore.preference) | ||
} |
9 changes: 9 additions & 0 deletions
9
datastore/src/androidMain/kotlin/com/b1nd/dodam/datastore/DataStoreBuilder.android.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.b1nd.dodam.datastore | ||
|
||
import android.content.Context | ||
|
||
fun createDataStore(context: Context) = createCoreDataStore( | ||
producePath = { | ||
context.filesDir.resolve(DATA_STORE_FILE_NAME).absolutePath | ||
}, | ||
) |
20 changes: 20 additions & 0 deletions
20
datastore/src/androidMain/kotlin/com/b1nd/dodam/datastore/di/DataStoreModule.android.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,20 @@ | ||
package com.b1nd.dodam.datastore.di | ||
|
||
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 org.koin.core.module.Module | ||
import org.koin.dsl.module | ||
|
||
actual val dataStoreModule: Module = module { | ||
single<DataStore<Preferences>> { | ||
val context: Context = get() | ||
createDataStore(context) | ||
} | ||
|
||
single<DatastoreRepository> { | ||
DatastoreRepository(get(), get()) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
datastore/src/commonMain/kotlin/com/b1nd/dodam/datastore/DataStoreBuilder.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,14 @@ | ||
package com.b1nd.dodam.datastore | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory | ||
import androidx.datastore.preferences.core.Preferences | ||
import okio.Path.Companion.toPath | ||
|
||
fun createCoreDataStore(producePath: () -> String): DataStore<Preferences> = PreferenceDataStoreFactory.createWithPath( | ||
corruptionHandler = null, | ||
migrations = emptyList(), | ||
produceFile = { producePath().toPath() }, | ||
) | ||
|
||
internal const val DATA_STORE_FILE_NAME = "dodam_preferences.preferences_pb" |
5 changes: 5 additions & 0 deletions
5
datastore/src/commonMain/kotlin/com/b1nd/dodam/datastore/di/DataStoreModule.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,5 @@ | ||
package com.b1nd.dodam.datastore.di | ||
|
||
import org.koin.core.module.Module | ||
|
||
expect val dataStoreModule: Module |
File renamed without changes.
52 changes: 52 additions & 0 deletions
52
datastore/src/commonMain/kotlin/com/b1nd/dodam/datastore/repository/DatastoreRepository.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,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 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") | ||
|
||
val user = dataStore.data.map { | ||
User( | ||
id = keyStoreManager.decrypt(it[idKey] ?: ""), | ||
pw = keyStoreManager.decrypt(it[pwKey] ?: ""), | ||
token = it[tokenKey] ?: "", | ||
) | ||
} | ||
|
||
val token = dataStore.data.map { | ||
it[tokenKey] ?: "" | ||
} | ||
|
||
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 saveToken(token: String) { | ||
dataStore.edit { | ||
it[tokenKey] = token | ||
} | ||
} | ||
|
||
suspend fun deleteUser() { | ||
dataStore.edit { | ||
it[idKey] = "" | ||
it[pwKey] = "" | ||
it[tokenKey] = "" | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
datastore/src/iosMain/kotlin/com/b1nd/dodam/datastore/DataStoreBuilder.ios.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,23 @@ | ||
package com.b1nd.dodam.datastore | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import platform.Foundation.NSDocumentDirectory | ||
import platform.Foundation.NSFileManager | ||
import platform.Foundation.NSURL | ||
import platform.Foundation.NSUserDomainMask | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
fun createDataStore(): DataStore<Preferences> = createCoreDataStore( | ||
producePath = { | ||
val documentDirectory: NSURL? = NSFileManager.defaultManager.URLForDirectory( | ||
directory = NSDocumentDirectory, | ||
inDomain = NSUserDomainMask, | ||
appropriateForURL = null, | ||
create = false, | ||
error = null, | ||
) | ||
requireNotNull(documentDirectory).path + "/$DATA_STORE_FILE_NAME" | ||
}, | ||
) |
Oops, something went wrong.