-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor Center list fragment to compose (#2109)
* refactor: refactor Individual collection sheet to compose * refactor: refactor client list fragment to compose * refactor: refactor center list fragment to compose * refactor: refactor center list to compose
- Loading branch information
1 parent
9b64f34
commit 88b33f8
Showing
24 changed files
with
1,056 additions
and
371 deletions.
There are no files selected for viewing
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
111 changes: 111 additions & 0 deletions
111
core/data/src/main/java/com/mifos/core/data/paging_source/CenterListPagingSource.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,111 @@ | ||
package com.mifos.core.data.paging_source | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.mifos.core.network.datamanager.DataManagerCenter | ||
import com.mifos.core.objects.client.Page | ||
import com.mifos.core.objects.group.Center | ||
import rx.Subscriber | ||
import rx.android.schedulers.AndroidSchedulers | ||
import rx.schedulers.Schedulers | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
class CenterListPagingSource(private val dataManagerCenter: DataManagerCenter) : | ||
PagingSource<Int, Center>() { | ||
|
||
override fun getRefreshKey(state: PagingState<Int, Center>): Int? { | ||
return state.anchorPosition?.let { position -> | ||
state.closestPageToPosition(position)?.prevKey?.plus(10) ?: state.closestPageToPosition( | ||
position | ||
)?.nextKey?.minus(10) | ||
} | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Center> { | ||
val position = params.key ?: 0 | ||
return try { | ||
val getCenters = getCenterList(position) | ||
val centerList = getCenters.first | ||
val totalCenters = getCenters.second | ||
val centerDbList = getCenterDbList() | ||
val centerListWithSync = getCenterListWithSync(centerList, centerDbList) | ||
LoadResult.Page( | ||
data = centerListWithSync, | ||
prevKey = if (position <= 0) null else position - 10, | ||
nextKey = if (position >= totalCenters) null else position + 10 | ||
) | ||
} catch (e: Exception) { | ||
LoadResult.Error(e) | ||
} | ||
} | ||
|
||
private suspend fun getCenterList(position: Int): Pair<List<Center>, Int> = | ||
suspendCoroutine { continuation -> | ||
try { | ||
dataManagerCenter.getCenters(true, position, 10) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribeOn(Schedulers.io()) | ||
.subscribe(object : Subscriber<Page<Center>>() { | ||
override fun onCompleted() { | ||
} | ||
|
||
override fun onError(exception: Throwable) { | ||
continuation.resumeWithException(exception) | ||
} | ||
|
||
override fun onNext(center: Page<Center>) { | ||
continuation.resume( | ||
Pair( | ||
center.pageItems, | ||
center.totalFilteredRecords | ||
) | ||
) | ||
} | ||
}) | ||
} catch (exception: Exception) { | ||
continuation.resumeWithException(exception) | ||
} | ||
} | ||
|
||
private suspend fun getCenterDbList(): List<Center> = suspendCoroutine { continuation -> | ||
try { | ||
|
||
dataManagerCenter.allDatabaseCenters | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribeOn(Schedulers.io()) | ||
.subscribe(object : Subscriber<Page<Center>>() { | ||
override fun onCompleted() { | ||
} | ||
|
||
override fun onError(error: Throwable) { | ||
continuation.resumeWithException(error) | ||
} | ||
|
||
override fun onNext(centers: Page<Center>) { | ||
continuation.resume(centers.pageItems) | ||
} | ||
}) | ||
} catch (exception: Exception) { | ||
continuation.resumeWithException(exception) | ||
} | ||
} | ||
|
||
|
||
private fun getCenterListWithSync( | ||
centerList: List<Center>, | ||
centerDbList: List<Center> | ||
): List<Center> { | ||
if (centerDbList.isNotEmpty()) { | ||
centerList.forEach { center -> | ||
centerDbList.forEach { centerDb -> | ||
if (center.id == centerDb.id) { | ||
center.sync = true | ||
} | ||
} | ||
} | ||
} | ||
return centerList | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
core/data/src/main/java/com/mifos/core/data/repository/CenterListRepository.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,20 @@ | ||
package com.mifos.core.data.repository | ||
|
||
import androidx.paging.PagingData | ||
import com.mifos.core.objects.client.Page | ||
import com.mifos.core.objects.group.Center | ||
import com.mifos.core.objects.group.CenterWithAssociations | ||
import kotlinx.coroutines.flow.Flow | ||
import rx.Observable | ||
|
||
/** | ||
* Created by Aditya Gupta on 06/08/23. | ||
*/ | ||
interface CenterListRepository { | ||
|
||
fun getAllCenters(): Flow<PagingData<Center>> | ||
|
||
fun getCentersGroupAndMeeting(id: Int): Observable<CenterWithAssociations> | ||
|
||
fun allDatabaseCenters(): Observable<Page<Center>> | ||
} |
39 changes: 39 additions & 0 deletions
39
core/data/src/main/java/com/mifos/core/data/repository_imp/CenterListRepositoryImp.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,39 @@ | ||
package com.mifos.core.data.repository_imp | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import com.mifos.core.data.paging_source.CenterListPagingSource | ||
import com.mifos.core.data.repository.CenterListRepository | ||
import com.mifos.core.network.datamanager.DataManagerCenter | ||
import com.mifos.core.objects.client.Page | ||
import com.mifos.core.objects.group.Center | ||
import com.mifos.core.objects.group.CenterWithAssociations | ||
import kotlinx.coroutines.flow.Flow | ||
import rx.Observable | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Created by Aditya Gupta on 06/08/23. | ||
*/ | ||
class CenterListRepositoryImp @Inject constructor(private val dataManagerCenter: DataManagerCenter) : | ||
CenterListRepository { | ||
|
||
override fun getAllCenters(): Flow<PagingData<Center>> { | ||
return Pager( | ||
config = PagingConfig( | ||
pageSize = 10 | ||
), pagingSourceFactory = { | ||
CenterListPagingSource(dataManagerCenter) | ||
} | ||
).flow | ||
} | ||
|
||
override fun getCentersGroupAndMeeting(id: Int): Observable<CenterWithAssociations> { | ||
return dataManagerCenter.getCentersGroupAndMeeting(id) | ||
} | ||
|
||
override fun allDatabaseCenters(): Observable<Page<Center>> { | ||
return dataManagerCenter.allDatabaseCenters | ||
} | ||
} |
10 changes: 6 additions & 4 deletions
10
core/designsystem/src/main/java/com/mifos/core/designsystem/component/MifosScaffold.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
8 changes: 8 additions & 0 deletions
8
core/designsystem/src/main/java/com/mifos/core/designsystem/icon/MifosIcon.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,8 @@ | ||
package com.mifos.core.designsystem.icon | ||
|
||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.rounded.Add | ||
|
||
object MifosIcons { | ||
val Add = Icons.Rounded.Add | ||
} |
47 changes: 47 additions & 0 deletions
47
core/domain/src/main/java/com/mifos/core/domain/use_cases/GetCenterListDbUseCase.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,47 @@ | ||
package com.mifos.core.domain.use_cases | ||
|
||
import com.mifos.core.common.utils.Resource | ||
import com.mifos.core.data.repository.CenterListRepository | ||
import com.mifos.core.objects.client.Page | ||
import com.mifos.core.objects.group.Center | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import rx.Subscriber | ||
import rx.android.schedulers.AndroidSchedulers | ||
import rx.schedulers.Schedulers | ||
import javax.inject.Inject | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
class GetCenterListDbUseCase @Inject constructor(private val repository: CenterListRepository) { | ||
|
||
suspend operator fun invoke(): Flow<Resource<List<Center>>> = flow { | ||
try { | ||
emit(Resource.Loading()) | ||
emit(Resource.Success(getCenterList())) | ||
} catch (exception: Exception) { | ||
emit(Resource.Error(exception.toString())) | ||
} | ||
} | ||
|
||
private suspend fun getCenterList(): List<Center> = suspendCoroutine { continuation -> | ||
try { | ||
repository.allDatabaseCenters() | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribeOn(Schedulers.io()) | ||
.subscribe(object : Subscriber<Page<Center>>() { | ||
override fun onCompleted() {} | ||
override fun onError(exception: Throwable) { | ||
continuation.resumeWithException(exception) | ||
} | ||
|
||
override fun onNext(centerPage: Page<Center>) { | ||
continuation.resume(centerPage.pageItems) | ||
} | ||
}) | ||
} catch (exception: Exception) { | ||
continuation.resumeWithException(exception) | ||
} | ||
} | ||
} |
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 @@ | ||
/build |
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,33 @@ | ||
plugins { | ||
alias(libs.plugins.mifos.android.feature) | ||
alias(libs.plugins.mifos.android.library.compose) | ||
alias(libs.plugins.mifos.android.library.jacoco) | ||
} | ||
|
||
android { | ||
namespace = "com.mifos.feature.center" | ||
} | ||
|
||
dependencies { | ||
|
||
implementation(projects.core.datastore) | ||
implementation(projects.core.network) | ||
implementation(projects.core.domain) | ||
|
||
implementation(libs.androidx.material) | ||
|
||
//DBFlow dependencies | ||
kapt(libs.dbflow.processor) | ||
implementation(libs.dbflow) | ||
kapt(libs.github.dbflow.processor) | ||
testImplementation(libs.hilt.android.testing) | ||
testImplementation(projects.core.testing) | ||
|
||
androidTestImplementation(projects.core.testing) | ||
|
||
//paging compose | ||
implementation(libs.androidx.paging.compose) | ||
|
||
//coil | ||
implementation(libs.coil.kt.compose) | ||
} |
Empty file.
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
24 changes: 24 additions & 0 deletions
24
feature/center/src/androidTest/java/com/mifos/feature/center/ExampleInstrumentedTest.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,24 @@ | ||
package com.mifos.feature.center | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.mifos.feature.center.test", appContext.packageName) | ||
} | ||
} |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
</manifest> |
Oops, something went wrong.