forked from openMF/android-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'openMF:master' into master
- Loading branch information
Showing
45 changed files
with
1,876 additions
and
986 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
67 changes: 67 additions & 0 deletions
67
core/data/src/main/java/com/mifos/core/data/paging_source/ClientChargesPagingSource.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,67 @@ | ||
package com.mifos.core.data.paging_source | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.mifos.core.network.datamanager.DataManagerCharge | ||
import com.mifos.core.objects.client.Charges | ||
import com.mifos.core.objects.client.Page | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import rx.Subscriber | ||
import rx.android.schedulers.AndroidSchedulers | ||
import rx.schedulers.Schedulers | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
|
||
class ClientChargesPagingSource( | ||
private val clientId: Int, | ||
private val dataManagerCharge: DataManagerCharge | ||
) : | ||
PagingSource<Int, Charges>() { | ||
|
||
override fun getRefreshKey(state: PagingState<Int, Charges>): 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, Charges> { | ||
val position = params.key ?: 0 | ||
return try { | ||
val getClientCharges = getClientChargeList(clientId, position) | ||
val clientChargesList = getClientCharges.first | ||
val totalCharges = getClientCharges.second | ||
LoadResult.Page( | ||
data = clientChargesList, | ||
prevKey = if (position <= 0) null else position - 10, | ||
nextKey = if (position >= totalCharges) null else position + 10 | ||
) | ||
} catch (exception: Exception) { | ||
LoadResult.Error(exception) | ||
} | ||
} | ||
|
||
private suspend fun getClientChargeList( | ||
clientId: Int, | ||
position: Int | ||
): Pair<List<Charges>, Int> { | ||
return suspendCancellableCoroutine { continuation -> | ||
dataManagerCharge.getClientCharges(clientId = clientId, offset = position, 10) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribeOn(Schedulers.io()) | ||
.subscribe(object : Subscriber<Page<Charges>>() { | ||
override fun onCompleted() { | ||
} | ||
|
||
override fun onError(exception: Throwable) { | ||
continuation.resumeWithException(exception) | ||
} | ||
|
||
override fun onNext(page: Page<Charges>) { | ||
continuation.resume(Pair(page.pageItems, page.totalFilteredRecords)) | ||
} | ||
}) | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
core/data/src/main/java/com/mifos/core/data/repository/ClientChargeRepository.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,14 @@ | ||
package com.mifos.core.data.repository | ||
|
||
import androidx.paging.PagingData | ||
import com.mifos.core.objects.client.Charges | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Created by Aditya Gupta on 08/08/23. | ||
*/ | ||
interface ClientChargeRepository { | ||
|
||
fun getClientCharges(clientId: Int): Flow<PagingData<Charges>> | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...dentifiers/ClientIdentifiersRepository.kt → ...repository/ClientIdentifiersRepository.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
6 changes: 1 addition & 5 deletions
6
...atenewcenter/CreateNewCenterRepository.kt → ...a/repository/CreateNewCenterRepository.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 |
---|---|---|
@@ -1,16 +1,12 @@ | ||
package com.mifos.mifosxdroid.online.createnewcenter | ||
package com.mifos.core.data.repository | ||
|
||
import com.mifos.core.data.CenterPayload | ||
import com.mifos.core.objects.organisation.Office | ||
import com.mifos.core.objects.response.SaveResponse | ||
import rx.Observable | ||
|
||
/** | ||
* Created by Aditya Gupta on 10/08/23. | ||
*/ | ||
interface CreateNewCenterRepository { | ||
|
||
fun offices(): Observable<List<Office>> | ||
|
||
fun createCenter(centerPayload: CenterPayload): Observable<SaveResponse> | ||
} |
30 changes: 30 additions & 0 deletions
30
core/data/src/main/java/com/mifos/core/data/repository_imp/ClientChargeRepositoryImp.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,30 @@ | ||
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.ClientChargesPagingSource | ||
import com.mifos.core.data.repository.ClientChargeRepository | ||
import com.mifos.core.network.datamanager.DataManagerCharge | ||
import com.mifos.core.objects.client.Charges | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Created by Aditya Gupta on 08/08/23. | ||
*/ | ||
class ClientChargeRepositoryImp @Inject constructor(private val dataManagerCharge: DataManagerCharge) : | ||
ClientChargeRepository { | ||
|
||
override fun getClientCharges( | ||
clientId: Int | ||
): Flow<PagingData<Charges>> { | ||
return Pager( | ||
config = PagingConfig( | ||
pageSize = 10 | ||
), pagingSourceFactory = { | ||
ClientChargesPagingSource(clientId, dataManagerCharge) | ||
} | ||
).flow | ||
} | ||
} |
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
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
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
42 changes: 42 additions & 0 deletions
42
core/domain/src/main/java/com/mifos/core/domain/use_cases/CreateNewCenterUseCase.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,42 @@ | ||
package com.mifos.core.domain.use_cases | ||
|
||
import com.mifos.core.common.utils.Resource | ||
import com.mifos.core.data.CenterPayload | ||
import com.mifos.core.data.repository.CreateNewCenterRepository | ||
import com.mifos.core.objects.response.SaveResponse | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import rx.Subscriber | ||
import rx.android.schedulers.AndroidSchedulers | ||
import rx.schedulers.Schedulers | ||
import javax.inject.Inject | ||
|
||
class CreateNewCenterUseCase @Inject constructor(private val repository: CreateNewCenterRepository) { | ||
|
||
suspend operator fun invoke(centerPayload: CenterPayload): Flow<Resource<SaveResponse>> = | ||
callbackFlow { | ||
try { | ||
trySend(Resource.Loading()) | ||
repository.createCenter(centerPayload) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribeOn(Schedulers.io()) | ||
.subscribe(object : Subscriber<SaveResponse>() { | ||
override fun onCompleted() {} | ||
|
||
override fun onError(exception: Throwable) { | ||
trySend(Resource.Error(exception.message.toString())) | ||
} | ||
|
||
override fun onNext(saveResponse: SaveResponse) { | ||
trySend(Resource.Success(saveResponse)) | ||
} | ||
}) | ||
|
||
awaitClose { channel.close() } | ||
|
||
} catch (exception: Exception) { | ||
trySend(Resource.Error(exception.message.toString())) | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
core/domain/src/main/java/com/mifos/core/domain/use_cases/DeleteIdentifierUseCase.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,43 @@ | ||
package com.mifos.core.domain.use_cases | ||
|
||
import com.mifos.core.common.utils.Resource | ||
import com.mifos.core.data.repository.ClientIdentifiersRepository | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import org.apache.fineract.client.models.DeleteClientsClientIdIdentifiersIdentifierIdResponse | ||
import rx.Subscriber | ||
import rx.android.schedulers.AndroidSchedulers | ||
import rx.schedulers.Schedulers | ||
import javax.inject.Inject | ||
|
||
class DeleteIdentifierUseCase @Inject constructor(private val repository: ClientIdentifiersRepository) { | ||
|
||
suspend operator fun invoke( | ||
clientId: Int, | ||
identifierId: Int | ||
): Flow<Resource<DeleteClientsClientIdIdentifiersIdentifierIdResponse>> = callbackFlow { | ||
try { | ||
trySend(Resource.Loading()) | ||
repository.deleteClientIdentifier(clientId = clientId, identifierId = identifierId) | ||
.observeOn(AndroidSchedulers.mainThread()) | ||
.subscribeOn(Schedulers.io()) | ||
.subscribe(object : | ||
Subscriber<DeleteClientsClientIdIdentifiersIdentifierIdResponse>() { | ||
override fun onCompleted() {} | ||
|
||
override fun onError(exception: Throwable) { | ||
trySend(Resource.Error(exception.message.toString())) | ||
} | ||
|
||
override fun onNext(response: DeleteClientsClientIdIdentifiersIdentifierIdResponse?) { | ||
trySend(Resource.Success(response)) | ||
} | ||
}) | ||
|
||
awaitClose { channel.close() } | ||
} catch (exception: Exception) { | ||
trySend(Resource.Error(exception.message.toString())) | ||
} | ||
} | ||
} |
Oops, something went wrong.