Skip to content

Commit

Permalink
refactor: refactor group details to compose with multi module
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-gupta99 committed Jun 11, 2024
1 parent 41bc7ab commit f9e15a5
Show file tree
Hide file tree
Showing 14 changed files with 1,092 additions and 327 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,9 +1,11 @@
package com.mifos.core.data.di

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

@Binds
internal abstract fun provideGroupDetailsRepository(impl : GroupDetailsRepositoryImp) : GroupDetailsRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mifos.core.data.repository

import com.mifos.core.objects.accounts.GroupAccounts
import com.mifos.core.objects.group.Group
import com.mifos.core.objects.group.GroupWithAssociations
import rx.Observable

/**
* Created by Aditya Gupta on 06/08/23.
*/
interface GroupDetailsRepository {

fun getGroup(groupId: Int): Observable<Group>

fun getGroupAccounts(groupId: Int): Observable<GroupAccounts>

fun getGroupWithAssociations(groupId: Int): Observable<GroupWithAssociations>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mifos.core.data.repository_imp

import com.mifos.core.data.repository.GroupDetailsRepository
import com.mifos.core.network.datamanager.DataManagerGroups
import com.mifos.core.objects.accounts.GroupAccounts
import com.mifos.core.objects.group.Group
import com.mifos.core.objects.group.GroupWithAssociations
import rx.Observable
import javax.inject.Inject

/**
* Created by Aditya Gupta on 06/08/23.
*/
class GroupDetailsRepositoryImp @Inject constructor(private val dataManagerGroups: DataManagerGroups) :
GroupDetailsRepository {

override fun getGroup(groupId: Int): Observable<Group> {
return dataManagerGroups.getGroup(groupId)
}

override fun getGroupAccounts(groupId: Int): Observable<GroupAccounts> {
return dataManagerGroups.getGroupAccounts(groupId)
}

override fun getGroupWithAssociations(groupId: Int): Observable<GroupWithAssociations> {
return dataManagerGroups.getGroupWithAssociations(groupId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.mifos.core.domain.use_cases

import com.mifos.core.common.utils.Resource
import com.mifos.core.data.repository.GroupDetailsRepository
import com.mifos.core.objects.client.Client
import com.mifos.core.objects.group.GroupWithAssociations
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 GetGroupAssociateClientsUseCase @Inject constructor(private val groupDetailsRepository: GroupDetailsRepository) {

suspend operator fun invoke(groupId: Int): Flow<Resource<List<Client>>> = callbackFlow {
try {
trySend(Resource.Loading())

groupDetailsRepository.getGroupWithAssociations(groupId)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(object : Subscriber<GroupWithAssociations>() {
override fun onCompleted() {}

override fun onError(exception: Throwable) {
trySend(Resource.Error(exception.message.toString()))
}

override fun onNext(groupWithAssociations: GroupWithAssociations) {
trySend(Resource.Success(groupWithAssociations.clientMembers))
}
})

awaitClose { channel.close() }
} catch (exception: Exception) {
trySend(Resource.Error(exception.message.toString()))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.mifos.core.domain.use_cases

import com.mifos.core.common.utils.Resource
import com.mifos.core.data.repository.GroupDetailsRepository
import com.mifos.core.objects.zipmodels.GroupAndGroupAccounts
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import rx.Observable
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import javax.inject.Inject

class GetGroupDetailsUseCase @Inject constructor(private val repository: GroupDetailsRepository) {

suspend operator fun invoke(groupId: Int): Flow<Resource<GroupAndGroupAccounts>> =
callbackFlow {
try {
trySend(Resource.Loading())
Observable.combineLatest(
repository.getGroup(groupId),
repository.getGroupAccounts(groupId)
) { group, groupAccounts -> GroupAndGroupAccounts(group, groupAccounts) }
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(object : Subscriber<GroupAndGroupAccounts>() {
override fun onCompleted() {}

override fun onError(e: Throwable) {
trySend(Resource.Error(e.message.toString()))
}

override fun onNext(groupAndGroupAccounts: GroupAndGroupAccounts) {
trySend(Resource.Success(groupAndGroupAccounts))
}
})

awaitClose { channel.close() }
} catch (exception: Exception) {
trySend(Resource.Error(exception.message.toString()))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import com.mifos.core.designsystem.theme.BlueSecondary
import com.mifos.core.domain.use_cases.GroupsListPagingDataSource
import com.mifos.core.objects.group.Group
import com.mifos.core.testing.repository.TestGroupsListRepository
import com.mifos.core.testing.repository.getPagedData
import com.mifos.core.testing.repository.sampleGroups
import com.mifos.feature.groups.R
import com.mifos.feature.groupsList.group_list.GroupsListScreen
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.runBlocking
Expand Down
Loading

0 comments on commit f9e15a5

Please sign in to comment.