· 6 years ago · Dec 26, 2019, 02:52 AM
1package co.id.ajsmsig.cs.simpel.ui.inputtopup
2
3import android.os.Build
4import android.util.Log
5import androidx.databinding.ObservableField
6import androidx.databinding.ObservableInt
7import androidx.databinding.ObservableLong
8import androidx.lifecycle.LiveData
9import androidx.lifecycle.MutableLiveData
10import co.id.ajsmsig.cs.simpel.R
11import co.id.ajsmsig.cs.simpel.SingleLiveData
12import co.id.ajsmsig.cs.simpel.base.BaseResponse
13import co.id.ajsmsig.cs.simpel.base.BaseViewModel
14import co.id.ajsmsig.cs.simpel.common.Result
15import co.id.ajsmsig.cs.simpel.db.entity.Fund
16import co.id.ajsmsig.cs.simpel.db.entity.Topup
17import co.id.ajsmsig.cs.simpel.db.entity.TopupInvesment
18import co.id.ajsmsig.cs.simpel.db.minimal.InvesmentAndFund
19import co.id.ajsmsig.cs.simpel.db.minimal.TopupSelectedMinimal
20import co.id.ajsmsig.cs.simpel.model.MessageInfo
21import co.id.ajsmsig.cs.simpel.model.inputtopup.*
22import co.id.ajsmsig.cs.simpel.util.DateUtils
23import co.id.ajsmsig.cs.simpel.util.extensions.addAutomaticThousandSeparator
24import co.id.ajsmsig.cs.simpel.util.extensions.addThousandSeparator
25import co.id.ajsmsig.cs.simpel.util.extensions.deleteAutomaticThousandSeparator
26import co.id.ajsmsig.cs.simpel.util.extensions.plusAssign
27import io.reactivex.Completable
28import io.reactivex.Observable
29import io.reactivex.android.schedulers.AndroidSchedulers
30import io.reactivex.schedulers.Schedulers
31import javax.inject.Inject
32
33class InputTopUpViewModel @Inject constructor(private val repository: InputTopUpRepository) :
34 BaseViewModel() {
35
36 val transferMoney = ObservableField<String>("0")
37 val totalPercentage = ObservableField(0)
38 val percentage = ObservableField<Int>()
39 val occuption = ObservableField<String>()
40 val occuptionId = ObservableInt(0)
41 val income = ObservableField<String>()
42 private val incomeId = ObservableInt(0)
43 val incomeSource = ObservableField<String>()
44 private val incomeSourceId = ObservableInt(0)
45 val topupModel = ObservableField<InfoDataProduct>()
46 val inputOtherUser = ObservableField<String>()
47 val otherOccuption = ObservableField<String>()
48 val errorOccuption = ObservableInt()
49 val policyNumber = ObservableField<String>()
50 val otherSource = ObservableField<String>()
51 val curencySymbol = ObservableField<String>()
52 val fundIdOld = ObservableField<String>()
53
54 private val _policyData = MutableLiveData<Result<InputTopupResponse>>()
55 val policyData: LiveData<Result<InputTopupResponse>>
56 get() = _policyData
57
58 private val listFundIdSelected = mutableListOf<String>()
59
60 val minTopup = ObservableField<Long>()
61 val maximumTopup = ObservableField<Long>()
62
63 private val selectedPercent = ObservableInt()
64 private val invesmentId = ObservableLong()
65 private val typeFundId = ObservableField<String>()
66 private val fundName = ObservableField<String>()
67
68 val topupId = ObservableLong(-1)
69
70 private val _failedInputTopup = SingleLiveData<TransferMessageModel>()
71 val failedInputTopup: LiveData<TransferMessageModel>
72 get() = _failedInputTopup
73
74 private val _funds = MutableLiveData<List<Fund>>()
75 val funds: LiveData<List<Fund>>
76 get() = _funds
77
78 private val _isUpdate = SingleLiveData<Boolean>()
79 val isUpdate: LiveData<Boolean>
80 get() = _isUpdate
81
82 private val _isMessage = SingleLiveData<Result<MessageInfo>>()
83 val isMessage: LiveData<Result<MessageInfo>>
84 get() = _isMessage
85
86 private val _isInputSelected = MutableLiveData<Result<TopupSelectedMinimal>>()
87 val isInputSelected: LiveData<Result<TopupSelectedMinimal>>
88 get() = _isInputSelected
89
90 private val _isSuccess = MutableLiveData<Result<List<InvesmentAndFund>>>()
91 val isSuccess: LiveData<Result<List<InvesmentAndFund>>>
92 get() = _isSuccess
93
94 private val _spinnerFunds = MutableLiveData<List<FundsDefault>>()
95 val fundsDefault: LiveData<List<FundsDefault>> get() = _spinnerFunds
96
97 private val _currentSpinnerPosition = MutableLiveData<Int>()
98 val currentSpinnerFundPosition: LiveData<Int> get() = _currentSpinnerPosition
99
100 private val _currentSpinnerPercentagePosition = MutableLiveData<Int>()
101 val currentSpinnerPercentagePosition: LiveData<Int> = _currentSpinnerPercentagePosition
102
103 private val _buttonType = MutableLiveData<Int>()
104 val buttonType: LiveData<Int> get() = _buttonType
105
106 private val _adapterPosition = MutableLiveData<Int>()
107 val adapterPosition: LiveData<Int> get() = _adapterPosition
108
109 private val _currentFundsList = MutableLiveData<MutableList<FundsDefault>>()
110 val currentFundsList: LiveData<MutableList<FundsDefault>> get() = _currentFundsList
111
112 val map: HashMap<String, FundsDefault> = HashMap()
113
114 val selectedFund = ObservableField<FundsDefault>()
115
116 fun getPolicyData() {
117 mCompositeDisposable += repository.getInfoPolicyToTopup()
118 .doOnSubscribe { setResultPolicyData(Result.Loading()) }
119 .subscribeOn(Schedulers.io())
120 .observeOn(AndroidSchedulers.mainThread())
121 .subscribeWith(object : BaseResponse<InputTopupResponse>() {
122 override fun onSuccess(response: InputTopupResponse) {
123 if (response.error) {
124 setResultPolicyData(Result.Error(R.string.unknown_error))
125 return
126 }
127
128 topupModel.set(response.data)
129 minTopup.set(response.data.minimumTopup)
130 maximumTopup.set(response.data.maxsimumTopup)
131 curencySymbol.set(response.data.currency)
132 setSpinnerFund(response.data.funds)
133
134 checkInvestmentAndFunds(response.data.fundsDefault)
135
136 setResultPolicyData(Result.HasData(response))
137 }
138
139 override fun onNoInternetConnection() {
140 setResultPolicyData(Result.NoInternetConnection())
141 }
142
143 override fun onTimeout() {
144 setResultPolicyData(Result.Error(R.string.timeout))
145 }
146
147 override fun onUnknownError(message: String) {
148 setResultPolicyData(Result.Error(R.string.unknown_error))
149 }
150 })
151 }
152
153 private fun insertTopupInvestment(funds: MutableList<FundsDefault>) {
154 val topupId = topupId.get().toString()
155 val topupInvestments = mutableListOf<TopupInvesment>()
156 for (i in funds) {
157 topupInvestments.add(TopupInvesment(0, topupId, i.id, i.mduPercent))
158 }
159 //Save fund to room
160 insertTopUpInvestments(topupInvestments)
161 }
162
163 private fun setDefaultFunds(funds: List<FundsDefault>) {
164 for (i in funds) {
165 val fund = FundsDefault(id = i.id, name = i.name, mduPercent = i.mduPercent)
166 map[i.name] = fund
167 }
168 updateFundToLiveData()
169 }
170
171 private fun setSpinnerFund(funds: List<Funds>) {
172 val listFund = mutableListOf<FundsDefault>()
173 for (i in funds) {
174 listFund.add(FundsDefault(i.id, i.name, 0))
175 }
176 _spinnerFunds.postValue(listFund)
177
178 insertFundsToLocal(listFund)
179 }
180
181 private fun insertFundsToLocal(funds: MutableList<FundsDefault>) {
182 val spinnerFunds = mutableListOf<Fund>()
183 for (i in funds) {
184 spinnerFunds.add(Fund(i.id, i.name))
185 }
186 saveSpinnerFunds(spinnerFunds)
187 }
188
189 private fun saveSpinnerFunds(fund: MutableList<Fund>) {
190 mCompositeDisposable += Completable.fromCallable { repository.saveFundInvesment(fund) }
191 .subscribeOn(Schedulers.io())
192 .observeOn(AndroidSchedulers.mainThread())
193 .subscribe({
194 }, {
195 Log.e("Error", "Error saving fund")
196 })
197 }
198
199 private fun insertTopUpInvestments(topupInvestments: List<TopupInvesment>) {
200 mCompositeDisposable += Observable.fromCallable { repository.insertInvestmentAndFunds(topupInvestments) }
201 .subscribeOn(Schedulers.io())
202 .observeOn(AndroidSchedulers.mainThread())
203 .subscribe({
204 updateDataTopupSucces()
205 },
206 {
207 Log.e("Message", it.message.toString())
208 })
209 }
210
211 private fun insertTopUpData(
212 funds: MutableList<FundsDefault>
213 ) {
214 val policyNumber = repository.policyNumber
215 val date = DateUtils.getCurrentTime()
216 val transfer = validateTransferMoney()
217 val data = Topup(
218 topupId.get().toInt(),
219 "",
220 policyNumber,
221 transfer,
222 occuption.get()!!,
223 income.get()!!,
224 incomeSource.get()!!,
225 false,
226 date,
227 curencySymbol.get()!!,
228 topupModel.get()!!.currencyId,
229 topupModel.get()!!.policyHolder,
230 0,
231 true
232 )
233 mCompositeDisposable += Completable.fromCallable { repository.insertTopupInLocalData(data) }
234 .subscribeOn(Schedulers.io())
235 .observeOn(AndroidSchedulers.mainThread())
236 .subscribe({
237 //If success insert topup and investment
238 insertTopupInvestment(funds)
239 }, {
240 Log.e("Error insert topup", "Error input ")
241 })
242 }
243
244 fun fetchSelectedFund(id: Long) {
245 mCompositeDisposable += Observable.fromCallable { repository.viewSelectedFund(id) }
246 .subscribeOn(Schedulers.io())
247 .observeOn(AndroidSchedulers.mainThread())
248 .subscribe { data ->
249 setListInvestmentFromLocal(data)
250 }
251 }
252
253 private fun setListInvestmentFromLocal(data: List<InvesmentAndFund>) {
254 for (i in data) {
255 val fund = FundsDefault(id = i.fundId, name = i.fund_name!!, mduPercent = i.percentage)
256 map[i.fund_name] = fund
257 }
258 updateFundToLiveData()
259 }
260
261 fun viewTopupFromTableTopup(id: Long) {
262 mCompositeDisposable += repository.findInputDataFromDraftSelected(id)
263 .subscribeOn(Schedulers.io())
264 .observeOn(AndroidSchedulers.mainThread())
265 .subscribe { data -> findSelectedInputTopupSucces(data) }
266 }
267
268 fun onDeleteTopupInvesmentFund(investId: Long) {
269 mCompositeDisposable += Completable.fromCallable {
270 repository.deleteInvestmentBecauseFundSame(
271 investId
272 )
273 }
274 .subscribeOn(Schedulers.io())
275 .observeOn(AndroidSchedulers.mainThread())
276 .subscribe { fetchSelectedFund(topupId.get()) }
277 }
278
279
280 /*
281 Logic to on backpressed
282 */
283 fun checkingTopupIdInLocal(
284 funds: MutableList<FundsDefault>,
285 id: Long
286 ) {
287 topupId.set(id)
288 mCompositeDisposable += repository.findDataTopup(id)
289 .subscribeOn(Schedulers.io())
290 .observeOn(AndroidSchedulers.mainThread())
291 .subscribe(
292 {
293 insertTopUpData(funds)
294 },
295 {
296 insertTopUpData(funds)
297 })
298 }
299 /*
300 separator between database and logic
301 */
302 fun validateSaveDataUserInput(
303 funds: MutableList<FundsDefault>
304 ) {
305
306 if (!transferMoneyIsValid()) {
307 return
308 }
309 if (!spinnerInputIsValid()) {
310 return
311 }
312 if (!precentageIsValid()) {
313 return
314 }
315 validateOtherSource(funds)
316 }
317
318 fun validateTransferMoney(): Long {
319 if (transferMoney.get().isNullOrEmpty()) {
320 return 0
321 }
322 return transferMoney.get()!!.deleteAutomaticThousandSeparator()
323 }
324
325 private fun validateOtherSource(funds: MutableList<FundsDefault>) {
326 var jobs = occuption.get()
327 var source = incomeSource.get()
328 if (!isOtherOccuptionValid()) {
329 return
330 }
331 if (!isOtherIncomeSourceValid()) {
332 return
333 }
334 if (!otherOccuption.get().isNullOrEmpty()) {
335 jobs = otherOccuption.get()
336 }
337 if (!otherSource.get().isNullOrEmpty()) {
338 source = otherSource.get()
339 }
340 occuption.set(jobs)
341 incomeSource.set(source)
342 checkingTopupIdInLocal(funds, topupId.get())
343 }
344
345 private fun isOtherOccuptionValid(): Boolean {
346 if (otherOccuption.get().equals("-")) {
347 setResultInvesment(
348 Result.Information(
349 MessageInfo(
350 true,
351 R.string.alert_other_occuption
352 )
353 )
354 )
355 return false
356 }
357 return true
358 }
359
360 private fun isOtherIncomeSourceValid(): Boolean {
361 if (otherSource.get().equals("-")) {
362 setResultInvesment(Result.Information(MessageInfo(true, R.string.alert_other_source)))
363 return false
364 }
365 return true
366 }
367
368 private fun precentageIsValid(): Boolean {
369 if (totalPercentage.get()!!.toInt() < 100) {
370 setResultInvesment(Result.Information(MessageInfo(true, R.string.alert_percent_minus)))
371 return false
372 }
373 return true
374 }
375
376 private fun spinnerInputIsValid(): Boolean {
377 if (occuptionId.get() == 0) {
378 setErrorMessage(
379 TransferMessageModel(
380 R.string.topup_information,
381 R.string.occuption_failed,
382 ""
383 )
384 )
385 return false
386 }
387 if (incomeId.get() == 0) {
388 setErrorMessage(
389 TransferMessageModel(
390 R.string.topup_information,
391 R.string.income_failed,
392 ""
393 )
394 )
395 return false
396 }
397 if (incomeSourceId.get() == 0) {
398 setErrorMessage(
399 TransferMessageModel(
400 R.string.topup_information,
401 R.string.income_source_failed,
402 ""
403 )
404 )
405 return false
406 }
407 return true
408 }
409
410 private fun transferMoneyIsValid(): Boolean {
411 if (transferMoney.get().isNullOrEmpty()) {
412 setErrorMessage(
413 TransferMessageModel(
414 R.string.topup_information,
415 R.string.input_topup_failed,
416 ""
417 )
418 )
419 return false
420 }
421 if (transferMoney.get()!!.deleteAutomaticThousandSeparator() < minTopup.get()!!.toInt()) {
422 setErrorMessage(
423 TransferMessageModel(
424 R.string.topup_information,
425 R.string.topup_minimal,
426 minTopup.get()!!.addThousandSeparator()
427 )
428 )
429 return false
430 }
431 if (transferMoney.get()!!.deleteAutomaticThousandSeparator() > maximumTopup.get()!!.toLong()) {
432 setErrorMessage(
433 TransferMessageModel(
434 R.string.topup_information,
435 R.string.topup_maximal,
436 maximumTopup.get()!!.addThousandSeparator()
437 )
438 )
439 return false
440 }
441 return true
442 }
443
444 private fun settingTotalPercentage(data: List<InvesmentAndFund>) {
445 var totalTopup = 0
446 for (i in data) {
447 totalTopup += i.percentage
448 }
449 totalPercentage.set(totalTopup)
450 }
451
452 private fun setListFundIdSelected(data: List<InvesmentAndFund>) {
453 listFundIdSelected.clear()
454 for (i in data) {
455 listFundIdSelected.add(i.fundId)
456 }
457 }
458
459 fun saveFundInvestment(id: String, name: String) {
460 typeFundId.set(id)
461 fundName.set(name)
462 }
463
464 private fun findSelectedInputTopupSucces(data: TopupSelectedMinimal) {
465 setResultInputTopupSelected(Result.HasData(data))
466 if (data.amount > 0) {
467 transferMoney.set(data.amount.addAutomaticThousandSeparator())
468 }
469 }
470
471 private fun updateDataTopupSucces() {
472 _isUpdate.sendAction(true)
473 }
474
475 fun setPercentage(percent: Int) {
476 percentage.set(percent)
477 }
478
479 fun setTopupId(id: Long) {
480 topupId.set(id)
481 }
482
483 fun getTopupId(): Long {
484 return topupId.get()
485 }
486
487 fun setOccuption(job: String, id: Int) {
488 occuption.set(job)
489 occuptionId.set(id)
490 otherOccuption.set(null)
491 }
492
493 fun setIncome(money: String, id: Int) {
494 income.set(money)
495 incomeId.set(id)
496 }
497
498 fun setIncomeSource(source: String, id: Int) {
499 incomeSource.set(source)
500 incomeSourceId.set(id)
501 otherSource.set(null)
502 }
503
504 fun setOtherOccuption() {
505 if (inputOtherUser.get().isNullOrEmpty()) {
506 otherOccuption.set("-")
507 return
508 }
509 occuptionId.set(9)
510 otherOccuption.set(inputOtherUser.get())
511 }
512
513 fun setOtherOccuptionId(id: Int) {
514 occuptionId.set(id)
515 }
516
517 fun setOtherSourceId(id: Int) {
518 incomeSourceId.set(id)
519 }
520
521 fun setOtherSource() {
522 if (inputOtherUser.get().isNullOrEmpty()) {
523 otherSource.set("-")
524 return
525 }
526 incomeSourceId.set(5)
527 otherSource.set(inputOtherUser.get())
528 }
529
530 fun setButtonChooseType(type: Int) {
531 _buttonType.postValue(type)
532 }
533
534 fun settingTopUpInvesementAndOldTopupInvestId(id: Long, fundId: String) {
535 invesmentId.set(id)
536 fundIdOld.set(fundId)
537 }
538
539 fun setPercentFromLocal(percent: Int) {
540 selectedPercent.set(percent)
541 }
542
543 fun getPolicyNumber() {
544 policyNumber.set(repository.policyNumber)
545 }
546
547 private fun setResultInvesment(result: Result<MessageInfo>) {
548 _isMessage.sendActionOnBackground(result)
549 }
550
551 private fun setResult(result: Result<List<InvesmentAndFund>>) {
552 _isSuccess.postValue(result)
553 }
554
555 private fun setResultInputTopupSelected(data: Result<TopupSelectedMinimal>) {
556 _isInputSelected.postValue(data)
557 }
558
559 private fun setErrorMessage(message: TransferMessageModel) {
560 _failedInputTopup.sendActionOnBackground(message)
561 }
562
563 private fun setResultPolicyData(result: Result<InputTopupResponse>) {
564 _policyData.postValue(result)
565 }
566
567 fun updatePercentage(
568 data: MutableList<FundsDefault>
569 ) {
570 var total = 0
571 for (i in data) {
572 total += i.mduPercent
573 }
574 totalPercentage.set(total)
575 }
576
577 fun validateTopUp(
578 fundName: String,
579 funds: MutableList<FundsDefault>
580 ) {
581 if (!totalPercentIsValid()) {
582 return
583 }
584 setTopupFund(fundName, funds)
585 }
586
587 private fun setTopupFund(
588 afterSelectedFund: String,
589 funds: MutableList<FundsDefault>
590 ) {
591 //If hashmap is empty add the first fund object
592 if (map.isEmpty()) {
593 val fund = createFund()
594 //Put fund into map
595 map[afterSelectedFund] = fund
596 setSelectedFund(fund)
597 } else {
598 /* If hashmap is not empty, check if there is the same key in the map, if true replace current map
599 and sum up the percentage (merge fund) */
600 if (map.containsKey(afterSelectedFund)) {
601 val currentEditedPercentage = map[afterSelectedFund]!!.mduPercent
602 val fund = createFund().copy(
603 mduPercent = percentage.get()!! + currentEditedPercentage,
604 percentSpinnerPosition = currentEditedPercentage / 10 + currentSpinnerPercentagePosition.value!!
605 )
606 map[afterSelectedFund] = fund
607 } else {
608 //If there is no duplicate key just add the fund to the map
609 val fund = createFund()
610 map[afterSelectedFund] = fund
611 setSelectedFund(fund)
612 }
613 }
614 updateFundToLiveData()
615 updatePercentage(funds)
616 }
617
618 private fun updateFundToLiveData() {
619 //Get current hashmap
620 for (i in map) {
621 //Update it to live data
622 _currentFundsList.postValue(ArrayList(map.values))
623 }
624 }
625
626 fun setSpinnerFundCurrentPosition(position: Int) {
627 _currentSpinnerPosition.postValue(position)
628 }
629
630 fun setSpinnerPercentagePosition(position: Int) {
631 _currentSpinnerPercentagePosition.postValue(position)
632 }
633
634 fun updateTopupFund(
635 funds: MutableList<FundsDefault>,
636 fundName: String
637 ) {
638 if (!totalPercentIsValid()) {
639 return
640 }
641 updateFund(funds, fundName)
642 }
643
644 private fun updateFund(
645 funds: MutableList<FundsDefault>,
646 afterSelectedFund: String
647 ) {
648 val beforeSelectedFund = selectedFund.get()!!.name
649 if (map.containsKey(afterSelectedFund) && beforeSelectedFund == afterSelectedFund) {
650 // If fund before selected is equals to fund after selected do normal update
651 val fund = createFund().copy(mduPercent = percentage.get()!!)
652 map[selectedFund.get()!!.name] = fund
653 selectedFund.set(fund)
654 } else if (map.containsKey(afterSelectedFund) && beforeSelectedFund != afterSelectedFund) {
655 // If fund before selected is not equals to fund after selected do merge update
656 val currentEditedPercentage = map[afterSelectedFund]?.mduPercent
657 val fund = createFund().copy(
658 mduPercent = percentage.get()!! + currentEditedPercentage!!,
659 percentSpinnerPosition = currentEditedPercentage / 10 + currentSpinnerPercentagePosition.value!!
660 )
661 map[afterSelectedFund] = fund
662 map.remove(selectedFund.get()!!.name)
663 } else {
664 //Just add fund
665 val fund = createFund().copy(name = afterSelectedFund, mduPercent = percentage.get()!!)
666 map.remove(selectedFund.get()!!.name)
667 map[afterSelectedFund] = fund
668 setSelectedFund(fund)
669 }
670 updateFundToLiveData()
671 //Re-set percentage
672 updatePercentage(funds)
673 }
674
675 private fun createFund(): FundsDefault {
676 return FundsDefault(
677 typeFundId.get()!!,
678 fundName.get()!!,
679 percentage.get()!!,
680 currentSpinnerFundPosition.value!!,
681 currentSpinnerPercentagePosition.value!!
682 )
683 }
684
685 private fun totalPercentIsValid(): Boolean {
686 if (buttonType.value == 0) {
687 //Get current total percentage
688 val currentPercentage = totalPercentage.get()!!
689 //Get current total percentage after updating data
690 val totalPercentage = currentPercentage.plus(percentage.get()!!)
691 //Check percentage
692 if (totalPercentage > 100) {
693 setResultInvesment(Result.Information(MessageInfo(true, R.string.alert_percent)))
694 return false
695 }
696 } else {
697 //Get percentage edited value
698 val currentEditedPercentage = selectedFund.get()!!.mduPercent
699 //Get current total percentage
700 val currentPercentage = totalPercentage.get()!!
701 //Get current total percentage after updating data
702 val totalPercentage = currentPercentage.plus(percentage.get()!!) - currentEditedPercentage
703 //Check percentage
704 if (totalPercentage > 100) {
705 setResultInvesment(Result.Information(MessageInfo(true, R.string.alert_percent)))
706 return false
707 }
708 }
709 return true
710 }
711
712 fun setAdapterPosition(position: Int) {
713 _adapterPosition.postValue(position)
714 }
715
716 fun removeMapFund(model: FundsDefault) {
717 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
718 map.remove(model.name, model)
719 }
720 }
721
722 fun getSelectedFund(): FundsDefault {
723 return selectedFund.get()!!
724 }
725
726 fun setSelectedFund(model: FundsDefault) {
727 selectedFund.set(model)
728 }
729
730 private fun checkInvestmentAndFunds(fundsDefault: List<FundsDefault>) {
731 val topupId = topupId.get()
732 mCompositeDisposable += repository.findDataTopup(topupId)
733 .subscribeOn(Schedulers.io())
734 .observeOn(AndroidSchedulers.mainThread())
735 .subscribe(
736 {
737 //if there is topupid in local, get data from local
738 fetchSelectedFund(topupId)
739 },
740 {
741 // otherwise get data from api
742 setDefaultFunds(fundsDefault)
743 })
744 }
745}