-
Notifications
You must be signed in to change notification settings - Fork 611
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
refactor: [database/charge] dbflow to room #2312
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@itsPronay I think it would be a good idea to add @singleton to the providesClientDao() method. Since Room is designed to work with a single shared database connection, using @singleton ensures that all components use the same ChargeDao instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HekmatullahAmin thanks for the suggestion brother.
but we already used
@InstallIn(SingletonComponent::class)
on the DaoModule, it already ensures that all the provided components in this module are scoped as singletonsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @itsPronay !
Just to clarify the difference between @Installin(SingletonComponent::class) and @singleton in Hilt:
@Installin(SingletonComponent::class) means that the dependency will live as long as the application is alive, but it doesn't guarantee a single instance by itself.
@singleton tells Hilt to reuse the same instance of the dependency within the same scope.
Example:
In this case, every class that requests ApiService gets a new instance, because @singleton is missing.
Singleton Example:
By adding @singleton, only one instance of SomeService is created and shared across the entire app (within the SingletonComponent scope).
Without @singleton, multiple instances are created even if SingletonComponent is used.
If you’d like more details, you can check out the official documentation here:
Hilt - Android Developer Guide
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HekmatullahAmin Thanks for explaining.
I have a doubt, since MifosDatabase is already a singleton, wouldn't the DAO instances be shared across the app by default? meaning singleton