Skip to content

Commit

Permalink
Feat: [:core:common] KMP Migration (#1768)
Browse files Browse the repository at this point in the history
* Feat: [:core:common] KMP Migration

* Updated Usage Declaration
  • Loading branch information
niyajali authored Sep 26, 2024
1 parent a3b4fd3 commit d6623f8
Show file tree
Hide file tree
Showing 48 changed files with 255 additions and 179 deletions.
17 changes: 17 additions & 0 deletions core/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,36 @@ android {
}

kotlin {

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries.framework {
isStatic = false
export(libs.kermit.simple)
}
}

sourceSets {
commonMain.dependencies {
implementation(libs.kotlinx.coroutines.core)
api(libs.coil.kt)
api(libs.coil.core)
api(libs.coil.svg)
api(libs.coil.network.ktor)
api(libs.kermit.logging)
api(libs.squareup.okio)
}
androidMain.dependencies {
implementation(libs.kotlinx.coroutines.android)
}
commonTest.dependencies {
implementation(libs.kotlinx.coroutines.test)
}
iosMain.dependencies {
api(libs.kermit.simple)
}
}
}

This file was deleted.

This file was deleted.

34 changes: 0 additions & 34 deletions core/common/src/androidMain/kotlin/org/mifospay/common/Utils.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mifospay.core.common


// JVM and Android implementation
actual fun createPlatformFileUtils(): FileUtils = CommonFileUtils()
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mifospay.core.common

import java.text.NumberFormat
import java.util.Currency

actual class CurrencyFormatter {
actual fun format(
balance: Double?,
currencyCode: String?,
maximumFractionDigits: Int?,
): String {
val balanceFormatter = NumberFormat.getCurrencyInstance()
balanceFormatter.maximumFractionDigits = maximumFractionDigits ?: 0
balanceFormatter.currency = Currency.getInstance(currencyCode)
return balanceFormatter.format(balance)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
package org.mifospay.common
package org.mifospay.core.common

/**
* Created by naman on 17/6/17.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
package org.mifospay.common
package org.mifospay.core.common

object CreditCardUtils {
fun validateCreditCardNumber(str: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
package org.mifospay.core.common

import co.touchlab.kermit.Logger

object DebugUtil {

private val logger = Logger.withTag("QXZ")

fun log(vararg objects: Any): Array<out Any> {
val stringToPrint = objects.joinToString(", ")
logger.d { stringToPrint }
return objects
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.mifospay.core.common

import co.touchlab.kermit.Logger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okio.FileSystem
import okio.Path.Companion.toPath
import okio.SYSTEM


interface FileUtils {
suspend fun writeInputStreamDataToFile(inputStream: ByteArray, filePath: String): Boolean

companion object {
val logger = Logger.withTag("FileUtils")
}
}

expect fun createPlatformFileUtils(): FileUtils

class CommonFileUtils : FileUtils {
override suspend fun writeInputStreamDataToFile(
inputStream: ByteArray,
filePath: String,
): Boolean =
withContext(Dispatchers.Default) {
try {
val path = filePath.toPath()
FileSystem.SYSTEM.write(path) {
write(inputStream)
}

true
} catch (e: Exception) {
FileUtils.logger.e { "Error writing file: ${e.message}" }
false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
package org.mifospay.core.network
package org.mifospay.core.common

import org.koin.core.annotation.Qualifier

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
package org.mifospay.common
package org.mifospay.core.common

const val PAYEE_EXTERNAL_ID_ARG = "payeeExternalId"
const val TRANSFER_AMOUNT_ARG = "transferAmount"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.mifospay.core.common

expect class CurrencyFormatter {
fun format(balance: Double?, currencyCode: String?, maximumFractionDigits: Int?): String
}

fun <T> List<T>.toArrayList(): ArrayList<T> {
return ArrayList(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
package org.mifospay.core.network.di
package org.mifospay.core.common.di

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.SupervisorJob
import org.koin.core.qualifier.named
import org.koin.dsl.module
import org.mifospay.core.network.MifosDispatchers
import org.mifospay.core.common.MifosDispatchers

val DispatchersModule = module {
single<CoroutineDispatcher>(named(MifosDispatchers.IO.name)) { Dispatchers.IO }
single<CoroutineDispatcher>(named(MifosDispatchers.Default.name)) { Dispatchers.Default }
single<CoroutineScope>(named("ApplicationScope")) {
CoroutineScope(SupervisorJob() + Dispatchers.Default)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mifospay.core.common

// JVM and Android implementation
actual fun createPlatformFileUtils(): FileUtils = CommonFileUtils()

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mifospay.core.common

import java.text.NumberFormat
import java.util.Currency

actual class CurrencyFormatter {
actual fun format(
balance: Double?,
currencyCode: String?,
maximumFractionDigits: Int?,
): String {
val numberFormat = NumberFormat.getCurrencyInstance()
numberFormat.maximumFractionDigits = maximumFractionDigits ?: 0
numberFormat.currency = Currency.getInstance(currencyCode)
return numberFormat.format(balance)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.mifospay.core.common

import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.refTo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

// iOS implementation
@OptIn(ExperimentalForeignApi::class)
actual fun createPlatformFileUtils(): FileUtils = object : FileUtils {
override suspend fun writeInputStreamDataToFile(inputStream: ByteArray, filePath: String): Boolean =
withContext(Dispatchers.Default) {
try {
val nsData = inputStream.toNSData()
nsData.writeToFile(filePath, true)
true
} catch (e: Exception) {
FileUtils.logger.e { "Error writing file: ${e.message}" }
false
}
}

private fun ByteArray.toNSData(): NSData = NSData.create(bytes = this.refTo(0), length = this.size.toULong())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mifospay.core.common

import platform.Foundation.NSNumberFormatter

actual class CurrencyFormatter {
actual fun format(
balance: Double?,
currencyCode: String?,
maximumFractionDigits: Int?,
): String {
val numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle
numberFormatter.currencyCode = currencyCode
numberFormatter.maximumFractionDigits = maximumFractionDigits ?: 0
return numberFormatter.stringFromNumber(balance ?: 0.0) ?: ""
}
}
Loading

0 comments on commit d6623f8

Please sign in to comment.