-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [database/charge] dbflow to room (#2312)
* MIFOSAC-392 [database/charge] dbflow to room * backup * Fix exception * added singleton
- Loading branch information
Showing
15 changed files
with
392 additions
and
86 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
core/common/src/main/java/com/mifos/core/common/utils/DatabaseFetchException.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,15 @@ | ||
/* | ||
* Copyright 2025 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 | ||
|
||
class DatabaseFetchException( | ||
message: String, | ||
cause: Throwable? = null, | ||
) : Exception(message, cause) |
48 changes: 48 additions & 0 deletions
48
core/common/src/main/java/com/mifos/core/common/utils/FlowCallAdapterFactory.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,48 @@ | ||
/* | ||
* Copyright 2025 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 kotlinx.coroutines.flow.Flow | ||
import retrofit2.CallAdapter | ||
import retrofit2.Response | ||
import retrofit2.Retrofit | ||
import java.lang.reflect.ParameterizedType | ||
import java.lang.reflect.Type | ||
|
||
class FlowCallAdapterFactory private constructor() : CallAdapter.Factory() { | ||
override fun get( | ||
returnType: Type, | ||
annotations: Array<Annotation>, | ||
retrofit: Retrofit, | ||
): CallAdapter<*, *>? { | ||
if (getRawType(returnType) != Flow::class.java) { | ||
return null | ||
} | ||
check(returnType is ParameterizedType) { "Flow return type must be parameterized as Flow<Foo> or Flow<out Foo>" } | ||
val responseType = getParameterUpperBound(0, returnType) | ||
val rawFlowType = getRawType(responseType) | ||
return if (rawFlowType == Response::class.java) { | ||
check(responseType is ParameterizedType) { "Response must be parameterized as Response<Foo> or Response<out Foo>" } | ||
ResponseCallAdapter<Any>( | ||
getParameterUpperBound( | ||
0, | ||
responseType, | ||
), | ||
) | ||
} else { | ||
BodyCallAdapter<Any>(responseType) | ||
} | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun create() = FlowCallAdapterFactory() | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
core/common/src/main/java/com/mifos/core/common/utils/ResponseCallAdapter.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,72 @@ | ||
/* | ||
* Copyright 2025 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 // FlowCallAdapter.kt | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import retrofit2.Call | ||
import retrofit2.CallAdapter | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
import java.lang.reflect.Type | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
|
||
class ResponseCallAdapter<T>( | ||
private val responseType: Type, | ||
) : CallAdapter<T, Flow<Response<T>>> { | ||
override fun adapt(call: Call<T>): Flow<Response<T>> { | ||
return flow { | ||
emit( | ||
suspendCancellableCoroutine { continuation -> | ||
call.enqueue(object : Callback<T> { | ||
override fun onFailure(call: Call<T>, t: Throwable) { | ||
continuation.resumeWithException(t) | ||
} | ||
|
||
override fun onResponse(call: Call<T>, response: Response<T>) { | ||
continuation.resume(response) | ||
} | ||
}) | ||
continuation.invokeOnCancellation { call.cancel() } | ||
}, | ||
) | ||
} | ||
} | ||
|
||
override fun responseType() = responseType | ||
} | ||
|
||
class BodyCallAdapter<T>(private val responseType: Type) : CallAdapter<T, Flow<T>> { | ||
override fun adapt(call: Call<T>): Flow<T> { | ||
return flow { | ||
emit( | ||
suspendCancellableCoroutine { continuation -> | ||
call.enqueue(object : Callback<T> { | ||
override fun onFailure(call: Call<T>, t: Throwable) { | ||
continuation.resumeWithException(t) | ||
} | ||
|
||
override fun onResponse(call: Call<T>, response: Response<T>) { | ||
try { | ||
continuation.resume(response.body()!!) | ||
} catch (e: Exception) { | ||
continuation.resumeWithException(e) | ||
} | ||
} | ||
}) | ||
continuation.invokeOnCancellation { call.cancel() } | ||
}, | ||
) | ||
} | ||
} | ||
|
||
override fun responseType() = responseType | ||
} |
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
29 changes: 29 additions & 0 deletions
29
core/database/src/main/java/com/mifos/room/dao/ChargeDao.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,29 @@ | ||
/* | ||
* Copyright 2025 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.room.dao | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.Query | ||
import com.mifos.room.entities.client.Charges | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Created by Pronay Sarker on 14/02/2025 (3:32 PM) | ||
*/ | ||
@Dao | ||
interface ChargeDao { | ||
|
||
@Query("SELECT * FROM Charges where clientId = :clientId") | ||
fun getClientCharges(clientId: Int): Flow<List<Charges>> | ||
|
||
@Insert | ||
suspend fun insertAllCharges(vararg charges: List<Charges>) | ||
} |
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
Oops, something went wrong.