Skip to content

Commit

Permalink
Updated PrefManger
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-gupta99 authored and therajanmaurya committed Aug 24, 2023
1 parent c50f263 commit beece85
Show file tree
Hide file tree
Showing 38 changed files with 159 additions and 379 deletions.
4 changes: 2 additions & 2 deletions mifosng-android/src/main/java/com/mifos/api/BaseApiManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.mifos.api.services.SearchService
import com.mifos.api.services.StaffService
import com.mifos.api.services.SurveyService
import com.mifos.utils.JsonDateSerializer
import com.mifos.utils.PrefManager.instanceUrl
import com.mifos.utils.PrefManager
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
Expand Down Expand Up @@ -155,7 +155,7 @@ class BaseApiManager {
val gson = GsonBuilder()
.registerTypeAdapter(Date::class.java, JsonDateSerializer()).create()
mRetrofit = Retrofit.Builder()
.baseUrl(instanceUrl)
.baseUrl(PrefManager.getInstanceUrl())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
*/
package com.mifos.api

import com.mifos.utils.PrefManager.isAuthenticated
import com.mifos.utils.PrefManager.tenant
import com.mifos.utils.PrefManager.token
import com.mifos.utils.PrefManager
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException
Expand All @@ -19,8 +17,8 @@ class MifosInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val chianrequest = chain.request()
val builder = chianrequest.newBuilder()
.header(HEADER_TENANT, tenant)
if (isAuthenticated) builder.header(HEADER_AUTH, token)
.header(HEADER_TENANT, PrefManager.getTenant())
if (PrefManager.isAuthenticated()) builder.header(HEADER_AUTH, PrefManager.getToken())
val request = builder.build()
return chain.proceed(request)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class DataManagerAuth @Inject constructor(private val baseApiManager: BaseApiMan
* @return Basic OAuth
*/
fun login(username: String, password: String): Observable<PostAuthenticationResponse> {
val body = PostAuthenticationRequest()
body.username = username
body.password = password
val body = PostAuthenticationRequest().apply {
this.username = username
this.password = password
}
return baseApiManager.getAuthApi().authenticate(body, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,15 @@ class DataManagerCenter @Inject constructor(
*/
fun getCenters(paged: Boolean, offset: Int, limit: Int): Observable<Page<Center>> {
return when (userStatus) {
0 -> mBaseApiManager.centerApi.getCenters(paged, offset, limit)
1 -> {
false -> mBaseApiManager.centerApi.getCenters(paged, offset, limit)
true -> {
/**
* Return All Centers List from DatabaseHelperCenter only one time.
* If offset is zero this means this is first request and
* return all centers from DatabaseHelperCenter
*/
if (offset == 0) mDatabaseHelperCenter.readAllCenters() else Observable.just(Page())
}

else -> Observable.just(Page())
}
}

Expand Down Expand Up @@ -102,14 +100,12 @@ class DataManagerCenter @Inject constructor(

fun createCenter(centerPayload: CenterPayload): Observable<SaveResponse> {
return when (userStatus) {
0 -> mBaseApiManager.centerApi.createCenter(centerPayload)
1 ->
false -> mBaseApiManager.centerApi.createCenter(centerPayload)
true ->
/**
* Save CenterPayload in Database table.
*/
mDatabaseHelperCenter.saveCenterPayload(centerPayload)

else -> Observable.just(SaveResponse())
}
}

Expand All @@ -120,14 +116,12 @@ class DataManagerCenter @Inject constructor(
*/
fun getCenterWithAssociations(centerId: Int): Observable<CenterWithAssociations> {
return when (userStatus) {
0 -> mBaseApiManager.centerApi.getAllGroupsForCenter(centerId)
1 ->
false -> mBaseApiManager.centerApi.getAllGroupsForCenter(centerId)
true ->
/**
* Return Groups from DatabaseHelperGroups.
*/
mDatabaseHelperCenter.getCenterAssociateGroups(centerId)

else -> Observable.just(CenterWithAssociations())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,20 @@ class DataManagerCharge @Inject constructor(
*/
fun getClientCharges(clientId: Int, offset: Int, limit: Int): Observable<Page<Charges>> {
return when (userStatus) {
0 -> mBaseApiManager.chargeApi.getListOfCharges(clientId, offset, limit)
false -> mBaseApiManager.chargeApi.getListOfCharges(clientId, offset, limit)
.concatMap { chargesPage ->
mDatabaseHelperCharge.saveClientCharges(chargesPage, clientId)
Observable.just(chargesPage)
}

1 -> {
true -> {
/**
* Return Client Charges from DatabaseHelperClient only one time.
*/
if (offset == 0) mDatabaseHelperCharge.readClientCharges(clientId) else Observable.just(
Page()
)
}

else -> Observable.just(Page())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.mifos.utils.PrefManager.userStatus
import okhttp3.MultipartBody
import okhttp3.ResponseBody
import rx.Observable
import rx.functions.Func1
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -31,7 +30,8 @@ import javax.inject.Singleton
@Singleton
class DataManagerClient @Inject constructor(
val mBaseApiManager: BaseApiManager,
val mDatabaseHelperClient: DatabaseHelperClient
val mDatabaseHelperClient: DatabaseHelperClient,
private val baseApiManager: org.mifos.core.apimanager.BaseApiManager
) {
/**
* This Method sending the Request to REST API if UserStatus is 0 and
Expand All @@ -51,19 +51,17 @@ class DataManagerClient @Inject constructor(
*/
fun getAllClients(paged: Boolean, offset: Int, limit: Int): Observable<Page<Client>> {
return when (userStatus) {
0 -> mBaseApiManager.clientsApi.getAllClients(paged, offset, limit)
false -> mBaseApiManager.clientsApi.getAllClients(paged, offset, limit)
.concatMap { clientPage -> Observable.just(clientPage) }

1 -> {
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) mDatabaseHelperClient.readAllClients() else Observable.just(Page())
}

else -> Observable.just(Page())
}
}

Expand All @@ -84,16 +82,14 @@ class DataManagerClient @Inject constructor(
*/
fun getClient(clientId: Int): Observable<Client> {
return when (userStatus) {
0 -> mBaseApiManager.clientsApi.getClient(clientId)
false -> mBaseApiManager.clientsApi.getClient(clientId)
.concatMap { client -> Observable.just(client) }

1 ->
true ->
/**
* Return Clients from DatabaseHelperClient only one time.
*/
mDatabaseHelperClient.getClient(clientId)

else -> Observable.just(Client())
}
}

Expand All @@ -114,14 +110,12 @@ class DataManagerClient @Inject constructor(
*/
fun getClientAccounts(clientId: Int): Observable<ClientAccounts> {
return when (userStatus) {
0 -> mBaseApiManager.clientsApi.getClientAccounts(clientId)
1 ->
false -> mBaseApiManager.clientsApi.getClientAccounts(clientId)
true ->
/**
* Return Clients from DatabaseHelperClient only one time.
*/
mDatabaseHelperClient.realClientAccounts(clientId)

else -> Observable.just(ClientAccounts())
}
}

Expand Down Expand Up @@ -181,23 +175,21 @@ class DataManagerClient @Inject constructor(
*/
val clientTemplate: Observable<ClientsTemplate>
get() = when (userStatus) {
0 -> mBaseApiManager.clientsApi.clientTemplate
false -> mBaseApiManager.clientsApi.clientTemplate
.concatMap { clientsTemplate ->
mDatabaseHelperClient.saveClientTemplate(
clientsTemplate
)
}

1 ->
true ->
/**
* Return Clients from DatabaseHelperClient only one time.
*/
/**
* Return Clients from DatabaseHelperClient only one time.
*/
mDatabaseHelperClient.readClientTemplate()

else -> Observable.just(ClientsTemplate())
}

/**
Expand All @@ -210,17 +202,15 @@ class DataManagerClient @Inject constructor(
*/
fun createClient(clientPayload: ClientPayload): Observable<Client> {
return when (userStatus) {
0 -> mBaseApiManager.clientsApi.createClient(clientPayload)
false -> mBaseApiManager.clientsApi.createClient(clientPayload)
.concatMap { client -> Observable.just(client) }

1 ->
true ->
/**
* If user is in offline mode and he is making client. client payload will be saved
* in Database for future synchronization to sever.
*/
mDatabaseHelperClient.saveClientPayloadToDB(clientPayload)

else -> Observable.just(Client())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class DataManagerGroups @Inject constructor(
*/
fun getGroups(paged: Boolean, offset: Int, limit: Int): Observable<Page<Group>> {
return when (userStatus) {
0 -> mBaseApiManager.groupApi.getGroups(paged, offset, limit)
1 -> {
false -> mBaseApiManager.groupApi.getGroups(paged, offset, limit)
true -> {
/**
* offset : is the value from which position we want to fetch the list, It means
* if offset is 0 and User is in the Offline Mode So fetch all groups
Expand All @@ -59,8 +59,6 @@ class DataManagerGroups @Inject constructor(
Page()
)
}

else -> Observable.just(Page())
}
}

Expand All @@ -82,14 +80,12 @@ class DataManagerGroups @Inject constructor(
*/
fun getGroup(groupId: Int): Observable<Group> {
return when (userStatus) {
0 -> mBaseApiManager.groupApi.getGroup(groupId)
1 ->
false -> mBaseApiManager.groupApi.getGroup(groupId)
true ->
/**
* Return Groups from DatabaseHelperGroups.
*/
mDatabaseHelperGroups.getGroup(groupId)

else -> Observable.just(Group())
}
}

Expand All @@ -110,14 +106,12 @@ class DataManagerGroups @Inject constructor(
*/
fun getGroupWithAssociations(groupId: Int): Observable<GroupWithAssociations> {
return when (userStatus) {
0 -> mBaseApiManager.groupApi.getGroupWithAssociations(groupId)
1 ->
false -> mBaseApiManager.groupApi.getGroupWithAssociations(groupId)
true ->
/**
* Return Groups from DatabaseHelperGroups.
*/
mDatabaseHelperClient.getGroupAssociateClients(groupId)

else -> Observable.just(GroupWithAssociations())
}
}

Expand All @@ -130,14 +124,12 @@ class DataManagerGroups @Inject constructor(
*/
fun getGroupAccounts(groupId: Int): Observable<GroupAccounts> {
return when (userStatus) {
0 -> mBaseApiManager.groupApi.getGroupAccounts(groupId)
1 ->
false -> mBaseApiManager.groupApi.getGroupAccounts(groupId)
true ->
/**
* Return Groups from DatabaseHelperGroups.
*/
mDatabaseHelperGroups.readGroupAccounts(groupId)

else -> Observable.just(GroupAccounts())
}
}

Expand Down Expand Up @@ -167,14 +159,12 @@ class DataManagerGroups @Inject constructor(
*/
fun createGroup(groupPayload: GroupPayload): Observable<SaveResponse> {
return when (userStatus) {
0 -> mBaseApiManager.groupApi.createGroup(groupPayload)
1 ->
false -> mBaseApiManager.groupApi.createGroup(groupPayload)
true ->
/**
* Save GroupPayload in Database table.
*/
mDatabaseHelperGroups.saveGroupPayload(groupPayload)

else -> Observable.just(SaveResponse())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ class DataManagerLoan @Inject constructor(
*/
fun getLoanById(loanId: Int): Observable<LoanWithAssociations> {
return when (userStatus) {
0 -> mBaseApiManager.loanApi.getLoanByIdWithAllAssociations(loanId)
1 ->
false -> mBaseApiManager.loanApi.getLoanByIdWithAllAssociations(loanId)
true ->
/**
* Return LoanWithAssociation from DatabaseHelperLoan.
*/
mDatabaseHelperLoan.getLoanById(loanId)

else -> Observable.just(LoanWithAssociations())
}
}

Expand Down Expand Up @@ -105,14 +103,12 @@ class DataManagerLoan @Inject constructor(
*/
fun getLoanRepayTemplate(loanId: Int): Observable<LoanRepaymentTemplate> {
return when (userStatus) {
0 -> mBaseApiManager.loanApi.getLoanRepaymentTemplate(loanId)
1 ->
false -> mBaseApiManager.loanApi.getLoanRepaymentTemplate(loanId)
true ->
/**
* Return LoanRepaymentTemplate from DatabaseHelperLoan.
*/
mDatabaseHelperLoan.getLoanRepayTemplate(loanId)

else -> Observable.just(LoanRepaymentTemplate())
}
}

Expand Down Expand Up @@ -158,16 +154,14 @@ class DataManagerLoan @Inject constructor(
request: LoanRepaymentRequest
): Observable<LoanRepaymentResponse> {
return when (userStatus) {
0 -> mBaseApiManager.loanApi.submitPayment(loanId, request)
false -> mBaseApiManager.loanApi.submitPayment(loanId, request)
.concatMap { loanRepaymentResponse -> Observable.just(loanRepaymentResponse) }

1 ->
true ->
/**
* Return LoanRepaymentResponse from DatabaseHelperLoan.
*/
mDatabaseHelperLoan.saveLoanRepaymentTransaction(loanId, request)

else -> Observable.just(LoanRepaymentResponse())
}
}

Expand Down
Loading

0 comments on commit beece85

Please sign in to comment.