Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core-common #2305

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions core/common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.gradle.kotlin.dsl.implementation

/*
* Copyright 2024 Mifos Initiative
*
Expand All @@ -9,12 +11,10 @@
*/
plugins {
alias(libs.plugins.mifos.kmp.library)
id(libs.plugins.kotlin.parcelize.get().pluginId)
id("kotlinx-serialization")
alias(libs.plugins.kotlin.serialization)

//done by the plugin

alias(libs.plugins.mifos.android.hilt)
alias(libs.plugins.mifos.android.library.jacoco)
alias(libs.plugins.secrets)
}

android {
Expand All @@ -25,12 +25,56 @@ android {
}
}

secrets {
defaultPropertiesFileName = "secrets.defaults.properties"
kotlin {

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

sourceSets {
commonMain.dependencies {
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.serialization.json)
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)
api(libs.jb.kotlin.stdlib)
api(libs.kotlinx.datetime)
}

androidMain.dependencies {
implementation(libs.kotlinx.coroutines.android)
}
commonTest.dependencies {
implementation(libs.kotlinx.coroutines.test)
}
iosMain.dependencies {
api(libs.kermit.simple)
}
desktopMain.dependencies {
implementation(libs.kotlinx.coroutines.swing)
implementation(libs.kotlin.reflect)
}
jsMain.dependencies {
api(libs.jb.kotlin.stdlib.js)
api(libs.jb.kotlin.dom)
}
}
}

dependencies {
// implementation(projects.core.model)
implementation(projects.core.model)
implementation(project(":core:common"))
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.turbine)

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/android-client/blob/master/LICENSE.md
*/
package com.mifos.core.common.network.di


import com.mifos.core.common.network.MifosDispatchers
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

import org.koin.core.qualifier.named
import org.koin.dsl.module

actual val ioDispatcherModule: org.koin.core.module.Module
get() = module {
single<CoroutineDispatcher>(named(MifosDispatchers.IO.name)) { Dispatchers.IO }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mifos.core.common.utils

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

actual fun createPlatformFileUtils(): FileUtils = AndroidFileUtils()

class AndroidFileUtils : FileUtils {
override suspend fun writeInputStreamDataToFile(
inputStream: ByteArray,
filePath: String
): Boolean = withContext(Dispatchers.IO) {
try {
File(filePath).writeBytes(inputStream)
true
} catch (e: Exception) {
FileUtils.logger.e { "Android write error: ${e.message}" }
false
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/*
* 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/android-client/blob/master/LICENSE.md
*/
package com.mifos.core.common.utils

import android.content.Context
Expand All @@ -15,9 +6,9 @@ import android.preference.PreferenceManager
import com.mifos.core.common.R
import java.util.Locale

object LanguageHelper {
// https://gunhansancar.com/change-language-programmatically-in-android/
fun onAttach(context: Context): Context? {
actual object LanguageHelper {
actual fun onAttach(context: Any): Any? {
context as Context
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
return if (preferences.getBoolean(
context.getString(R.string.core_common_default_system_language),
Expand All @@ -37,13 +28,14 @@ object LanguageHelper {
}
}

@JvmStatic
fun onAttach(context: Context, defaultLanguage: String): Context? {
actual fun onAttach(context: Any, defaultLanguage: String): Any? {
context as Context
val lang = getPersistedData(context, defaultLanguage)
return lang?.let { setLocale(context, it) }
}

fun setLocale(context: Context?, language: String): Context? {
actual fun setLocale(context: Any?, language: String): Any? {
context as Context?
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
updateResources(context!!, language)
} else {
Expand Down Expand Up @@ -78,4 +70,4 @@ object LanguageHelper {
resources?.updateConfiguration(configuration, resources.displayMetrics)
return context
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mifos.core.common.utils

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities


actual object Network {

lateinit var appContext: Context

fun init(context: Context) {
appContext = context.applicationContext
}

fun isOnline(): Boolean {
val connectivityManager =
appContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val capabilities =
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
return capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) == true ||
capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) == true ||
capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) == true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mifos.core.common.utils

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import androidx.annotation.MainThread

actual class NetworkUtilsWrapper actual constructor() {
private var context: Context? = null

fun initialize(context: Context) {
this.context = context.applicationContext
}

@MainThread
fun isNetworkConnected(): Boolean {
val ctx = context ?: error("Call NetworkUtilsWrapper.initialize() first")
val connectivityManager = ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val network = connectivityManager.activeNetwork ?: return false
val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ package com.mifos.core.common.utils

import android.os.Parcel
import android.os.Parcelable
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parceler
import kotlinx.parcelize.Parcelize
import kotlinx.parcelize.TypeParceler

actual typealias Parcelize = Parcelize

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/


package com.mifos.core.common.network

import javax.inject.Qualifier
import kotlin.annotation.AnnotationRetention.RUNTIME
import org.koin.core.annotation.Qualifier

@Qualifier
@Retention(RUNTIME)
@Retention(AnnotationRetention.RUNTIME)
annotation class Dispatcher(val mifosDispatcher: MifosDispatchers)

enum class MifosDispatchers {
Default,
IO,
Unconfined,
}

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ApplicationScope
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mifos.core.common.network.com.mifos.core.common.di

import com.mifos.core.common.network.MifosDispatchers
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import org.koin.core.module.Module
import org.koin.core.qualifier.named
import org.koin.dsl.module


val DispatchersModule = module {
includes(ioDispatcherModule)
single<CoroutineDispatcher>(named(MifosDispatchers.Default.name)) { Dispatchers.Default }
single<CoroutineDispatcher>(named(MifosDispatchers.Unconfined.name)) { Dispatchers.Unconfined }
single<CoroutineScope>(named("ApplicationScope")) {
CoroutineScope(SupervisorJob() + Dispatchers.Default)
}
}

expect val ioDispatcherModule: Module
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
package com.mifos.core.common.utils

import com.mifos.core.common.BuildConfig
import utils.asServerConfig

object BaseUrl {

Expand Down
Loading
Loading