Skip to content

Commit

Permalink
refactor: refactor Center list fragment to compose (#2109)
Browse files Browse the repository at this point in the history
* 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
Aditya-gupta99 authored Jun 13, 2024
1 parent 9b64f34 commit 88b33f8
Show file tree
Hide file tree
Showing 24 changed files with 1,056 additions and 371 deletions.
5 changes: 5 additions & 0 deletions core/data/src/main/java/com/mifos/core/data/di/DataModule.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.mifos.core.data.di

import com.mifos.core.data.repository.CenterListRepository
import com.mifos.core.data.repository.CheckerInboxTasksRepository
import com.mifos.core.data.repository.GroupsListRepository
import com.mifos.core.data.repository.NewIndividualCollectionSheetRepository
import com.mifos.core.data.repository_imp.CenterListRepositoryImp
import com.mifos.core.data.repository_imp.CheckerInboxTasksRepositoryImp
import com.mifos.core.data.repository_imp.GroupsListRepositoryImpl
import com.mifos.core.data.repository_imp.NewIndividualCollectionSheetRepositoryImp
Expand All @@ -25,4 +27,7 @@ abstract class DataModule {
internal abstract fun provideGroupListRepository(
groupsListRepositoryImpl: GroupsListRepositoryImpl
): GroupsListRepository

@Binds
internal abstract fun bindCenterListRepository(impl: CenterListRepositoryImp): CenterListRepository
}
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
}
}
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>>
}
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
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
@file:OptIn(ExperimentalMaterial3Api::class)

package com.mifos.core.designsystem.component

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.mifos.core.designsystem.theme.White

@Composable
fun MifosScaffold(
modifier: Modifier = Modifier,
topBar: @Composable () -> Unit,
snackbarHostState: SnackbarHostState?,
bottomBar: @Composable () -> Unit = {},
floatingActionButton: @Composable () -> Unit = {},
content: @Composable (PaddingValues) -> Unit
) {

Scaffold(
modifier = modifier,
topBar = topBar,
snackbarHost = { snackbarHostState?.let { SnackbarHost(it) } },
containerColor = White,
bottomBar = bottomBar
bottomBar = bottomBar,
floatingActionButton = floatingActionButton
) { padding ->
content(padding)
}
Expand Down
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
}
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)
}
}
}
1 change: 1 addition & 0 deletions feature/center/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
33 changes: 33 additions & 0 deletions feature/center/build.gradle.kts
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.
21 changes: 21 additions & 0 deletions feature/center/proguard-rules.pro
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
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)
}
}
4 changes: 4 additions & 0 deletions feature/center/src/main/AndroidManifest.xml
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>
Loading

0 comments on commit 88b33f8

Please sign in to comment.