Skip to content

Commit

Permalink
Merge pull request #155 from Team-B1ND/feature/154-kmp-network-modules
Browse files Browse the repository at this point in the history
Feature/KMP Network Modules
  • Loading branch information
8954sood authored Aug 3, 2024
2 parents cdbf830 + de4b1ec commit 753cd86
Show file tree
Hide file tree
Showing 152 changed files with 3,138 additions and 378 deletions.
12 changes: 12 additions & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ gradlePlugin {
id = "b1nd.dodam.primitive.kotlin.serialization"
implementationClass = "com.b1nd.dodam.primitive.KotlinSerializationPlugin"
}
register("multiplatform") {
id = "b1nd.dodam.primitive.multiplatform"
implementationClass = "com.b1nd.dodam.primitive.MultiplatformPlugin"
}
register("multiplatformKotlinSerialization") {
id = "b1nd.dodam.primitive.multiplatform.kotlin.serialization"
implementationClass = "com.b1nd.dodam.primitive.MultiplatformKotlinSerializationPlugin"
}
register("multiplatformKoinPlugin") {
id = "b1nd.dodam.primitive.multiplatform.koin"
implementationClass = "com.b1nd.dodam.primitive.MultiplatformKoinPlugin"
}
// convention
register("androidFeature") {
id = "b1nd.dodam.convention.android.feature"
Expand Down
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
}
}
}
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"))
}
}
}
}
}
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"))
}
}
}
}
}
}
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()
}
}
}
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ plugins {
alias(libs.plugins.composeinvestigator).apply(false)
alias(libs.plugins.google.services).apply(false)
alias(libs.plugins.firbase.crashlytics).apply(false)
alias(libs.plugins.kotlin.multiplatform).apply(false)
alias(libs.plugins.android.library).apply(false)
}

subprojects {
Expand Down
20 changes: 17 additions & 3 deletions common/build.gradle.kts
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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.b1nd.dodam.common.network.di
import com.b1nd.dodam.common.DispatcherType
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import org.koin.core.qualifier.named
import org.koin.dsl.module

Expand Down
21 changes: 0 additions & 21 deletions common/src/main/java/com/b1nd/dodam/common/DodamHash.kt

This file was deleted.

26 changes: 18 additions & 8 deletions datastore/build.gradle.kts
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)
}
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
},
)
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())
}
}
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"
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
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] = ""
}
}
}
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"
},
)
Loading

0 comments on commit 753cd86

Please sign in to comment.