Skip to content

Commit 6d97423

Browse files
PratyushSingh07therajanmaurya
authored andcommitted
refactor #2415: beneficiary,tpt to stateflow
1 parent 4be93df commit 6d97423

17 files changed

+242
-171
lines changed

app/src/main/java/org/mifos/mobile/api/DataManager.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import kotlinx.coroutines.flow.Flow
55
import okhttp3.ResponseBody
66
import org.mifos.mobile.api.local.DatabaseHelper
77
import org.mifos.mobile.api.local.PreferencesHelper
8-
import org.mifos.mobile.models.*
8+
import org.mifos.mobile.models.Charge
9+
import org.mifos.mobile.models.Page
10+
import org.mifos.mobile.models.Transaction
11+
import org.mifos.mobile.models.UpdatePasswordPayload
12+
import org.mifos.mobile.models.User
913
import org.mifos.mobile.models.accounts.loan.LoanAccount
1014
import org.mifos.mobile.models.accounts.loan.LoanWithAssociations
1115
import org.mifos.mobile.models.accounts.loan.LoanWithdraw
@@ -166,28 +170,28 @@ class DataManager @Inject constructor(
166170
return baseApiManager.loanAccountsListApi.withdrawLoanAccount(loanId, loanWithdraw)
167171
}
168172

169-
suspend fun beneficiaryList(): Response<List<Beneficiary?>?>? =
173+
suspend fun beneficiaryList(): List<Beneficiary> =
170174
baseApiManager.beneficiaryApi.beneficiaryList()
171175

172-
suspend fun beneficiaryTemplate(): Response<BeneficiaryTemplate?>? =
176+
suspend fun beneficiaryTemplate(): BeneficiaryTemplate =
173177
baseApiManager.beneficiaryApi.beneficiaryTemplate()
174178

175-
suspend fun createBeneficiary(beneficiaryPayload: BeneficiaryPayload?): Response<ResponseBody?>? {
179+
suspend fun createBeneficiary(beneficiaryPayload: BeneficiaryPayload?): ResponseBody {
176180
return baseApiManager.beneficiaryApi.createBeneficiary(beneficiaryPayload)
177181
}
178182

179183
suspend fun updateBeneficiary(
180184
beneficiaryId: Long?,
181185
payload: BeneficiaryUpdatePayload?,
182-
): Response<ResponseBody?>? {
186+
): ResponseBody {
183187
return baseApiManager.beneficiaryApi.updateBeneficiary(beneficiaryId, payload)
184188
}
185189

186-
suspend fun deleteBeneficiary(beneficiaryId: Long?): Response<ResponseBody?>? {
190+
suspend fun deleteBeneficiary(beneficiaryId: Long?): ResponseBody {
187191
return baseApiManager.beneficiaryApi.deleteBeneficiary(beneficiaryId)
188192
}
189193

190-
suspend fun thirdPartyTransferTemplate(): Response<AccountOptionsTemplate?>? =
194+
suspend fun thirdPartyTransferTemplate(): AccountOptionsTemplate =
191195
baseApiManager.thirdPartyTransferApi.accountTransferTemplate()
192196

193197
suspend fun makeThirdPartyTransfer(transferPayload: TransferPayload?): Response<ResponseBody?>? {

app/src/main/java/org/mifos/mobile/api/services/BeneficiaryService.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,27 @@ import org.mifos.mobile.models.beneficiary.Beneficiary
66
import org.mifos.mobile.models.beneficiary.BeneficiaryPayload
77
import org.mifos.mobile.models.beneficiary.BeneficiaryUpdatePayload
88
import org.mifos.mobile.models.templates.beneficiary.BeneficiaryTemplate
9-
import retrofit2.Response
109
import retrofit2.http.*
1110

1211
/**
1312
* Created by dilpreet on 14/6/17.
1413
*/
1514
interface BeneficiaryService {
1615
@GET(ApiEndPoints.BENEFICIARIES + "/tpt")
17-
suspend fun beneficiaryList(): Response<List<Beneficiary?>?>?
16+
suspend fun beneficiaryList(): List<Beneficiary>
1817

1918
@GET(ApiEndPoints.BENEFICIARIES + "/tpt/template")
20-
suspend fun beneficiaryTemplate(): Response<BeneficiaryTemplate?>?
19+
suspend fun beneficiaryTemplate(): BeneficiaryTemplate
2120

2221
@POST(ApiEndPoints.BENEFICIARIES + "/tpt")
23-
suspend fun createBeneficiary(@Body beneficiaryPayload: BeneficiaryPayload?): Response<ResponseBody?>?
22+
suspend fun createBeneficiary(@Body beneficiaryPayload: BeneficiaryPayload?): ResponseBody
2423

2524
@PUT(ApiEndPoints.BENEFICIARIES + "/tpt/{beneficiaryId}")
2625
suspend fun updateBeneficiary(
2726
@Path("beneficiaryId") beneficiaryId: Long?,
2827
@Body payload: BeneficiaryUpdatePayload?,
29-
): Response<ResponseBody?>?
28+
): ResponseBody
3029

3130
@DELETE(ApiEndPoints.BENEFICIARIES + "/tpt/{beneficiaryId}")
32-
suspend fun deleteBeneficiary(@Path("beneficiaryId") beneficiaryId: Long?): Response<ResponseBody?>?
31+
suspend fun deleteBeneficiary(@Path("beneficiaryId") beneficiaryId: Long?): ResponseBody
3332
}

app/src/main/java/org/mifos/mobile/api/services/ThirdPartyTransferService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import retrofit2.http.POST
1515
*/
1616
interface ThirdPartyTransferService {
1717
@GET(ApiEndPoints.ACCOUNT_TRANSFER + "/template?type=tpt")
18-
suspend fun accountTransferTemplate(): Response<AccountOptionsTemplate?>?
18+
suspend fun accountTransferTemplate(): AccountOptionsTemplate
1919

2020
@POST(ApiEndPoints.ACCOUNT_TRANSFER + "?type=tpt")
2121
suspend fun makeTransfer(@Body transferPayload: TransferPayload?): Response<ResponseBody?>?
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package org.mifos.mobile.repositories
22

3+
import kotlinx.coroutines.flow.Flow
34
import okhttp3.ResponseBody
45
import org.mifos.mobile.models.beneficiary.Beneficiary
56
import org.mifos.mobile.models.beneficiary.BeneficiaryPayload
67
import org.mifos.mobile.models.beneficiary.BeneficiaryUpdatePayload
78
import org.mifos.mobile.models.templates.beneficiary.BeneficiaryTemplate
8-
import retrofit2.Response
99

1010
interface BeneficiaryRepository {
1111

12-
suspend fun beneficiaryTemplate(): Response<BeneficiaryTemplate?>?
12+
suspend fun beneficiaryTemplate(): Flow<BeneficiaryTemplate>
1313

14-
suspend fun createBeneficiary(beneficiaryPayload: BeneficiaryPayload?): Response<ResponseBody?>?
14+
suspend fun createBeneficiary(beneficiaryPayload: BeneficiaryPayload?): Flow<ResponseBody>
1515

1616
suspend fun updateBeneficiary(
1717
beneficiaryId: Long?,
1818
payload: BeneficiaryUpdatePayload?,
19-
): Response<ResponseBody?>?
19+
): Flow<ResponseBody>
2020

21-
suspend fun deleteBeneficiary(beneficiaryId: Long?): Response<ResponseBody?>?
21+
suspend fun deleteBeneficiary(beneficiaryId: Long?): Flow<ResponseBody>
2222

23-
suspend fun beneficiaryList(): Response<List<Beneficiary?>?>?
23+
suspend fun beneficiaryList(): Flow<List<Beneficiary>>
2424

2525
}
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
11
package org.mifos.mobile.repositories
22

3+
import kotlinx.coroutines.flow.Flow
4+
import kotlinx.coroutines.flow.flow
35
import okhttp3.ResponseBody
46
import org.mifos.mobile.api.DataManager
57
import org.mifos.mobile.models.beneficiary.Beneficiary
68
import org.mifos.mobile.models.beneficiary.BeneficiaryPayload
79
import org.mifos.mobile.models.beneficiary.BeneficiaryUpdatePayload
810
import org.mifos.mobile.models.templates.beneficiary.BeneficiaryTemplate
9-
import retrofit2.Response
1011
import javax.inject.Inject
1112

1213
class BeneficiaryRepositoryImp @Inject constructor(private val dataManager: DataManager) :
1314
BeneficiaryRepository {
1415

15-
override suspend fun beneficiaryTemplate(): Response<BeneficiaryTemplate?>? {
16-
return dataManager.beneficiaryTemplate()
16+
override suspend fun beneficiaryTemplate(): Flow<BeneficiaryTemplate> {
17+
return flow {
18+
emit(dataManager.beneficiaryTemplate())
19+
}
1720
}
1821

19-
override suspend fun createBeneficiary(beneficiaryPayload: BeneficiaryPayload?): Response<ResponseBody?>? {
20-
return dataManager.createBeneficiary(beneficiaryPayload)
22+
override suspend fun createBeneficiary(beneficiaryPayload: BeneficiaryPayload?): Flow<ResponseBody> {
23+
return flow {
24+
emit(dataManager.createBeneficiary(beneficiaryPayload))
25+
}
2126
}
2227

2328
override suspend fun updateBeneficiary(
2429
beneficiaryId: Long?,
2530
payload: BeneficiaryUpdatePayload?
26-
): Response<ResponseBody?>? {
27-
return dataManager.updateBeneficiary(beneficiaryId, payload)
31+
): Flow<ResponseBody> {
32+
return flow {
33+
emit(dataManager.updateBeneficiary(beneficiaryId, payload))
34+
}
2835
}
2936

30-
override suspend fun deleteBeneficiary(beneficiaryId: Long?): Response<ResponseBody?>? {
31-
return dataManager.deleteBeneficiary(beneficiaryId)
37+
override suspend fun deleteBeneficiary(beneficiaryId: Long?): Flow<ResponseBody> {
38+
return flow {
39+
emit(dataManager.deleteBeneficiary(beneficiaryId))
40+
}
3241
}
3342

34-
override suspend fun beneficiaryList(): Response<List<Beneficiary?>?>? {
35-
return dataManager.beneficiaryList()
43+
override suspend fun beneficiaryList(): Flow<List<Beneficiary>> {
44+
return flow {
45+
emit(dataManager.beneficiaryList())
46+
}
3647
}
3748

3849
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.mifos.mobile.repositories
22

3+
import kotlinx.coroutines.flow.Flow
34
import org.mifos.mobile.models.templates.account.AccountOptionsTemplate
4-
import retrofit2.Response
55

66
interface ThirdPartyTransferRepository {
77

8-
suspend fun thirdPartyTransferTemplate(): Response<AccountOptionsTemplate?>?
8+
suspend fun thirdPartyTransferTemplate(): Flow<AccountOptionsTemplate>
99

1010
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.mifos.mobile.repositories
22

3+
import kotlinx.coroutines.flow.Flow
4+
import kotlinx.coroutines.flow.flow
35
import org.mifos.mobile.api.DataManager
46
import org.mifos.mobile.models.templates.account.AccountOptionsTemplate
57
import retrofit2.Response
@@ -8,8 +10,10 @@ import javax.inject.Inject
810
class ThirdPartyTransferRepositoryImp @Inject constructor(private val dataManager: DataManager) :
911
ThirdPartyTransferRepository {
1012

11-
override suspend fun thirdPartyTransferTemplate(): Response<AccountOptionsTemplate?>? {
12-
return dataManager.thirdPartyTransferTemplate()
13+
override suspend fun thirdPartyTransferTemplate(): Flow<AccountOptionsTemplate> {
14+
return flow {
15+
emit(dataManager.thirdPartyTransferTemplate())
16+
}
1317
}
1418

1519
}

app/src/main/java/org/mifos/mobile/ui/fragments/BeneficiaryApplicationFragment.kt

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import android.os.Parcelable
55
import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
8-
import androidx.lifecycle.ViewModel
9-
import androidx.lifecycle.ViewModelProvider
8+
import androidx.fragment.app.viewModels
9+
import androidx.lifecycle.Lifecycle
10+
import androidx.lifecycle.lifecycleScope
11+
import androidx.lifecycle.repeatOnLifecycle
1012
import com.github.therajanmaurya.sweeterror.SweetUIErrorHandler
1113
import dagger.hilt.android.AndroidEntryPoint
14+
import kotlinx.coroutines.launch
1215
import org.mifos.mobile.R
1316
import org.mifos.mobile.databinding.FragmentBeneficiaryApplicationBinding
1417
import org.mifos.mobile.models.beneficiary.Beneficiary
@@ -32,7 +35,7 @@ class BeneficiaryApplicationFragment : BaseFragment() {
3235
private var _binding: FragmentBeneficiaryApplicationBinding? = null
3336
private val binding get() = _binding!!
3437

35-
private lateinit var viewModel: BeneficiaryApplicationViewModel
38+
private val viewModel: BeneficiaryApplicationViewModel by viewModels()
3639

3740
private val listAccountType: MutableList<String?> = ArrayList()
3841
private var beneficiaryState: BeneficiaryState? = null
@@ -70,7 +73,6 @@ class BeneficiaryApplicationFragment : BaseFragment() {
7073
savedInstanceState: Bundle?,
7174
): View {
7275
_binding = FragmentBeneficiaryApplicationBinding.inflate(inflater, container, false)
73-
viewModel = ViewModelProvider(this)[BeneficiaryApplicationViewModel::class.java]
7476
sweetUIErrorHandler = SweetUIErrorHandler(activity, binding.root)
7577
showUserInterface()
7678
if (savedInstanceState == null) {
@@ -82,30 +84,41 @@ class BeneficiaryApplicationFragment : BaseFragment() {
8284
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
8385
super.onViewCreated(view, savedInstanceState)
8486

85-
viewModel.beneficiaryUiState.observe(viewLifecycleOwner) {
86-
when (it) {
87-
is BeneficiaryUiState.Loading -> showProgress()
88-
is BeneficiaryUiState.ShowError -> {
89-
hideProgress()
90-
showError(getString(it.message))
87+
viewLifecycleOwner.lifecycleScope.launch {
88+
repeatOnLifecycle(Lifecycle.State.STARTED) {
89+
viewModel.beneficiaryUiState.collect {
90+
when (it) {
91+
is BeneficiaryUiState.Loading -> showProgress()
92+
is BeneficiaryUiState.ShowError -> {
93+
hideProgress()
94+
showError(getString(it.message))
95+
}
96+
97+
is BeneficiaryUiState.SetVisibility -> {
98+
hideProgress()
99+
setVisibility(it.visibility)
100+
}
101+
102+
is BeneficiaryUiState.ShowBeneficiaryTemplate -> {
103+
hideProgress()
104+
showBeneficiaryTemplate(it.beneficiaryTemplate)
105+
}
106+
107+
is BeneficiaryUiState.CreatedSuccessfully -> {
108+
hideProgress()
109+
showBeneficiaryCreatedSuccessfully()
110+
}
111+
112+
is BeneficiaryUiState.UpdatedSuccessfully -> {
113+
hideProgress()
114+
showBeneficiaryUpdatedSuccessfully()
115+
}
116+
117+
is BeneficiaryUiState.Initial -> {}
118+
119+
else -> throw IllegalStateException("Undesired $it")
120+
}
91121
}
92-
is BeneficiaryUiState.SetVisibility -> {
93-
hideProgress()
94-
setVisibility(it.visibility)
95-
}
96-
is BeneficiaryUiState.ShowBeneficiaryTemplate -> {
97-
hideProgress()
98-
showBeneficiaryTemplate(it.beneficiaryTemplate)
99-
}
100-
is BeneficiaryUiState.CreatedSuccessfully -> {
101-
hideProgress()
102-
showBeneficiaryCreatedSuccessfully()
103-
}
104-
is BeneficiaryUiState.UpdatedSuccessfully -> {
105-
hideProgress()
106-
showBeneficiaryUpdatedSuccessfully()
107-
}
108-
else -> throw IllegalStateException("Undesired $it")
109122
}
110123
}
111124

@@ -151,7 +164,7 @@ class BeneficiaryApplicationFragment : BaseFragment() {
151164
*
152165
* @param beneficiaryTemplate [BeneficiaryTemplate] fetched from server
153166
*/
154-
fun showBeneficiaryTemplate(beneficiaryTemplate: BeneficiaryTemplate?) {
167+
private fun showBeneficiaryTemplate(beneficiaryTemplate: BeneficiaryTemplate?) {
155168
this.beneficiaryTemplate = beneficiaryTemplate
156169
for ((_, _, value) in beneficiaryTemplate?.accountTypeOptions!!) {
157170
listAccountType.add(value)
@@ -280,7 +293,7 @@ class BeneficiaryApplicationFragment : BaseFragment() {
280293
* Displays a {@link Snackbar} on successfully creation of
281294
* Beneficiary and pops fragments in order to go back to [BeneficiaryListFragment]
282295
*/
283-
fun showBeneficiaryCreatedSuccessfully() {
296+
private fun showBeneficiaryCreatedSuccessfully() {
284297
Toaster.show(binding.tilTransferLimit, getString(R.string.beneficiary_created_successfully))
285298
activity?.finish()
286299
}
@@ -289,7 +302,7 @@ class BeneficiaryApplicationFragment : BaseFragment() {
289302
* Displays a {@link Snackbar} on successfully updation of
290303
* Beneficiary and pops fragments in order to go back to [BeneficiaryListFragment]
291304
*/
292-
fun showBeneficiaryUpdatedSuccessfully() {
305+
private fun showBeneficiaryUpdatedSuccessfully() {
293306
Toaster.show(binding.root, getString(R.string.beneficiary_updated_successfully))
294307
activity?.supportFragmentManager?.popBackStack()
295308
activity?.supportFragmentManager?.popBackStack()
@@ -316,7 +329,7 @@ class BeneficiaryApplicationFragment : BaseFragment() {
316329
}
317330
}
318331

319-
fun setVisibility(state: Int) {
332+
private fun setVisibility(state: Int) {
320333
binding.llApplicationBeneficiary.visibility = state
321334
}
322335

0 commit comments

Comments
 (0)