Skip to content

Commit

Permalink
feat: Implemented Paging3
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-gupta99 committed Feb 18, 2024
1 parent cec92e8 commit 4822876
Show file tree
Hide file tree
Showing 22 changed files with 495 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mifos.core.common.utils

object Constants {

const val SERVICE_STATUS = "service_status"

}
14 changes: 14 additions & 0 deletions core/common/src/main/java/com/mifos/core/common/utils/Page.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* This project is licensed under the open source MPL V2.
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.core.common.utils

/**
* Created by ishankhanna on 09/02/14.
*/
data class Page<T>(
var totalFilteredRecords: Int = 0,

var pageItems: List<T> = ArrayList()
)
10 changes: 10 additions & 0 deletions core/datastore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ android {
dependencies {

implementation(project(":core:data"))
implementation(project(":core:common"))

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
Expand All @@ -46,6 +47,15 @@ dependencies {
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")

//rxjava dependencies
implementation("io.reactivex:rxandroid:1.1.0")
implementation("io.reactivex:rxjava:1.3.8")

//DBFlow dependencies
kapt("com.github.raizlabs.dbflow.dbflow:dbflow-processor:3.1.1")
implementation("com.github.raizlabs.dbflow.dbflow:dbflow:3.1.1")
kapt("com.github.raizlabs.dbflow:dbflow-processor:4.2.4")

// Hilt dependency
implementation("com.google.dagger:hilt-android:2.50")
kapt("com.google.dagger:hilt-android-compiler:2.50")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mifos.core.database

import com.mifos.core.common.utils.Page
import com.mifos.core.data.model.client.Client
import com.raizlabs.android.dbflow.sql.language.SQLite
import rx.Observable

class DatabaseHelperClient {


/**
* Reading All Clients from table of Client and return the ClientList
*
* @return List Of Client
*/
//TODO Implement Observable Transaction to load Client List
fun readAllClients(): Observable<Page<Client>> {
return Observable.create { subscriber ->
val clientPage = Page<Client>()
clientPage.pageItems = SQLite.select()
.from(Client::class.java)
.queryList()
subscriber.onNext(clientPage)
subscriber.onCompleted()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mifos.core.datastore
import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import com.mifos.core.common.utils.Constants
import com.mifos.core.data.model.user.User
import dagger.hilt.android.qualifiers.ApplicationContext
import org.apache.fineract.client.models.PostAuthenticationResponse
Expand Down Expand Up @@ -45,6 +46,12 @@ class PrefManager @Inject constructor(@ApplicationContext context: Context) :
return get(Key.Custom(permissionDeniedStatus), true)
}

var userStatus: Boolean
get() = get(Key.Custom(Constants.SERVICE_STATUS), false)
set(status) {
put(Key.Custom(Constants.SERVICE_STATUS), status)
}

var usernamePassword: Pair<String, String>
get() = Pair(get(Key.Custom(AUTH_USERNAME)), get(Key.Custom(AUTH_PASSWORD)))
set(value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.mifos.core.designsystem.component

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun MifosPagingAppendProgress() {
Box(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight(),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator(
modifier = Modifier
.width(40.dp)
.height(40.dp)
.padding(8.dp),
strokeWidth = 4.dp
)
}

}
7 changes: 7 additions & 0 deletions core/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ android {
dependencies {

implementation(project(":core:datastore"))
implementation(project(":core:common"))
implementation(project(":core:data"))

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
Expand All @@ -58,4 +60,9 @@ dependencies {

// sdk client
implementation("com.github.openMF:fineract-client:2.0.3")

//DBFlow dependencies
kapt("com.github.raizlabs.dbflow.dbflow:dbflow-processor:3.1.1")
implementation("com.github.raizlabs.dbflow.dbflow:dbflow:3.1.1")
kapt("com.github.raizlabs.dbflow:dbflow-processor:4.2.4")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.mifos.core.network.datamanger

import com.mifos.core.common.utils.Page
import com.mifos.core.data.model.client.Client
import com.mifos.core.database.DatabaseHelperClient
import com.mifos.core.datastore.PrefManager
import com.mifos.core.network.di.BaseApiManagerQualifier
import com.mifos.core.network.mappers.clients.GetClientResponseMapper
import org.mifos.core.apimanager.BaseApiManager
import rx.Observable
import javax.inject.Inject

class DataManagerClient @Inject constructor(
@BaseApiManagerQualifier private val baseApiManager: BaseApiManager,
private val prefManager: PrefManager,
private val databaseHelperClient: DatabaseHelperClient
) {

/**
* This Method sending the Request to REST API if UserStatus is 0 and
* get list of the clients. The response is pass to the DatabaseHelperClient
* that save the response in Database different thread and next pass the response to
* Presenter to show in the view
*
*
* If the offset is zero and UserStatus is 1 then fetch all clients list and show on the view.
* else if offset is not zero and UserStatus is 1 then return default empty response to
* presenter
*
* @param paged True Enable the Pagination of the client list REST API
* @param offset Value give from which position Fetch ClientList
* @param limit Maximum Number of clients will come in response
* @return Client List from offset to max Limit
*/

fun getAllClients(offset: Int, limit: Int): Observable<Page<Client>> {
return when (prefManager.userStatus) {
false -> baseApiManager.getClientsApi().retrieveAll21(
null, null, null,
null, null, null,
null, null, offset,
limit, null, null, null
).map(GetClientResponseMapper::mapFromEntity)

true -> {
/**
* Return All Clients List from DatabaseHelperClient only one time.
* If offset is zero this means this is first request and
* return all clients from DatabaseHelperClient
*/
if (offset == 0) databaseHelperClient.readAllClients() else Observable.just(Page())
}
}
}


/**
* This Method Request to the DatabaseHelperClient and DatabaseHelperClient Read the All
* clients from Client_Table and give the response Page of List of Client
*
* @return Page of Client List
*/
val allDatabaseClients: Observable<Page<Client>>
get() = databaseHelperClient.readAllClients()

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mifos.core.network.di

import com.mifos.core.database.DatabaseHelperClient
import com.mifos.core.datastore.PrefManager
import com.mifos.core.network.datamanger.DataManagerClient
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand Down Expand Up @@ -28,4 +30,7 @@ object NetworkModule {
return baseManager
}

@Provides
fun provideDataMangerClient() : DatabaseHelperClient = DatabaseHelperClient()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.mifos.core.network.mappers.clients

import com.mifos.core.data.model.client.Client
import com.mifos.core.data.model.client.Status
import org.apache.fineract.client.models.GetClientStatus
import org.apache.fineract.client.models.GetClientsPageItemsResponse
import org.mifos.core.data.AbstractMapper

object ClientMapper : AbstractMapper<GetClientsPageItemsResponse, Client>() {

override fun mapFromEntity(entity: GetClientsPageItemsResponse): Client {
return Client().apply {
id = entity.id!!
accountNo = entity.accountNo
fullname = entity.fullname
firstname = entity.displayName!!.split(" ")[0]
lastname =
if (entity.displayName!!.split(" ").size >= 2) entity.displayName!!.split(" ")[1] else ""
displayName = entity.displayName
officeId = entity.officeId!!
officeName = entity.officeName
active = entity.active!!
status = Status().apply {
id = entity.status?.id!!
code = entity.status?.code
value = entity.status?.description
}
}
}

override fun mapToEntity(domainModel: Client): GetClientsPageItemsResponse {
return GetClientsPageItemsResponse().apply {
id = domainModel.id
accountNo = domainModel.accountNo
fullname = domainModel.fullname
displayName = domainModel.displayName
officeId = domainModel.officeId
officeName = domainModel.officeName
active = domainModel.active
status = GetClientStatus().apply {
id = domainModel.status?.id
code = domainModel.status?.code
description = domainModel.status?.value
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mifos.core.network.mappers.clients

import com.mifos.core.common.utils.Page
import com.mifos.core.data.model.client.Client
import org.apache.fineract.client.models.GetClientsResponse
import org.mifos.core.data.AbstractMapper

object GetClientResponseMapper : AbstractMapper<GetClientsResponse, Page<Client>>() {

override fun mapFromEntity(entity: GetClientsResponse): Page<Client> {
return Page<Client>().apply {
totalFilteredRecords = entity.totalFilteredRecords!!
pageItems = ClientMapper.mapFromEntityList(entity.pageItems!!)
}
}

override fun mapToEntity(domainModel: Page<Client>): GetClientsResponse {
return GetClientsResponse().apply {
totalFilteredRecords = domainModel.totalFilteredRecords
pageItems = ClientMapper.mapToEntityList(domainModel.pageItems)
}
}
}
7 changes: 7 additions & 0 deletions feature/client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ dependencies {

implementation(project(":core:data"))
implementation(project(":core:designsystem"))
implementation(project(":core:datastore"))
implementation(project(":core:common"))
implementation(project(":core:network"))


implementation("androidx.core:core-ktx:1.12.0")
Expand Down Expand Up @@ -94,4 +97,8 @@ dependencies {

// swipe refresh
implementation("com.google.accompanist:accompanist-swiperefresh:0.25.1")

// paging 3
implementation("androidx.paging:paging-runtime-ktx:3.2.1")
implementation("androidx.paging:paging-compose:3.2.1")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mifos.feature.client.clientList.data.repositoryImp

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.mifos.core.common.utils.Page
import com.mifos.core.data.model.client.Client
import com.mifos.core.network.datamanger.DataManagerClient
import com.mifos.feature.client.clientList.domain.repository.ClientListRepository
import com.mifos.feature.client.clientList.paging.ClientListPagingSource
import kotlinx.coroutines.flow.Flow
import rx.Observable
import javax.inject.Inject

/**
* Created by Aditya Gupta on 08/08/23.
*/
class ClientListRepositoryImp @Inject constructor(private val dataManagerClient: DataManagerClient) :
ClientListRepository {

override fun getAllClients(): Flow<PagingData<Client>> {
return Pager(
config = PagingConfig(
pageSize = 10
), pagingSourceFactory = {
ClientListPagingSource(dataManagerClient)
}
).flow
}

override fun allDatabaseClients(): Observable<Page<Client>> {
return dataManagerClient.allDatabaseClients
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mifos.feature.client.clientList.di

import com.mifos.core.network.datamanger.DataManagerClient
import com.mifos.feature.client.clientList.data.repositoryImp.ClientListRepositoryImp
import com.mifos.feature.client.clientList.domain.repository.ClientListRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object RepositoryModule {

@Singleton
@Provides
fun provideClientListRepository(dataManagerClient: DataManagerClient): ClientListRepository =
ClientListRepositoryImp(dataManagerClient)

}
Loading

0 comments on commit 4822876

Please sign in to comment.