Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: home old fragment migrated to mvvm #2202

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.repositories.HomeRepository
import org.mifos.mobile.repositories.HomeRepositoryImp
import org.mifos.mobile.repositories.LoanRepository
import org.mifos.mobile.repositories.LoanRepositoryImp
import org.mifos.mobile.repositories.NotificationRepository
import org.mifos.mobile.api.local.PreferencesHelper
import org.mifos.mobile.repositories.*

Expand All @@ -17,6 +22,11 @@ class RepositoryModule {
return UserAuthRepositoryImp(dataManager)
}

@Provides
fun providesHomeRepository(dataManager: DataManager) : HomeRepository {
return HomeRepositoryImp(dataManager)
}

@Provides
fun providesLoanRepository(dataManager: DataManager): LoanRepository {
return LoanRepositoryImp(dataManager)
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/org/mifos/mobile/repositories/HomeRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import okhttp3.ResponseBody
import org.mifos.mobile.models.client.Client
import org.mifos.mobile.models.client.ClientAccounts

interface HomeRepository {

fun clientAccounts(): Observable<ClientAccounts?>?
fun currentClient(): Observable<Client?>?
fun clientImage(): Observable<ResponseBody?>?
fun unreadNotificationsCount(): Observable<Int>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import okhttp3.ResponseBody
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.client.Client
import org.mifos.mobile.models.client.ClientAccounts
import javax.inject.Inject

class HomeRepositoryImp @Inject constructor(private val dataManager: DataManager) : HomeRepository {

override fun clientAccounts(): Observable<ClientAccounts?>? {
return dataManager.clientAccounts
}

override fun currentClient(): Observable<Client?>? {
return dataManager.currentClient
}

override fun clientImage(): Observable<ResponseBody?>? {
return dataManager.clientImage
}

override fun unreadNotificationsCount(): Observable<Int> {
return dataManager.unreadNotificationsCount
}

}
130 changes: 77 additions & 53 deletions app/src/main/java/org/mifos/mobile/ui/fragments/HomeOldFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
Expand All @@ -25,7 +26,6 @@ import org.mifos.mobile.R
import org.mifos.mobile.api.local.PreferencesHelper
import org.mifos.mobile.databinding.FragmentHomeOldBinding
import org.mifos.mobile.models.client.Client
import org.mifos.mobile.presenters.HomeOldPresenter
import org.mifos.mobile.ui.activities.HomeActivity
import org.mifos.mobile.ui.activities.LoanApplicationActivity
import org.mifos.mobile.ui.activities.NotificationActivity
Expand All @@ -35,25 +35,19 @@ import org.mifos.mobile.ui.enums.AccountType
import org.mifos.mobile.ui.enums.ChargeType
import org.mifos.mobile.ui.fragments.base.BaseFragment
import org.mifos.mobile.ui.getThemeAttributeColor
import org.mifos.mobile.ui.views.HomeOldView
import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.CurrencyUtil
import org.mifos.mobile.utils.MaterialDialog
import org.mifos.mobile.utils.TextDrawable
import org.mifos.mobile.utils.Toaster
import org.mifos.mobile.utils.*
import org.mifos.mobile.viewModels.HomeViewModel
import javax.inject.Inject

/**
* Created by michaelsosnick on 1/1/17.
*/
@AndroidEntryPoint
class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
class HomeOldFragment : BaseFragment(), OnRefreshListener {
private var _binding: FragmentHomeOldBinding? = null
private val binding get() = _binding!!

@JvmField
@Inject
var presenter: HomeOldPresenter? = null
lateinit var viewModel: HomeViewModel

@JvmField
@Inject
Expand All @@ -73,9 +67,9 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
savedInstanceState: Bundle?,
): View {
_binding = FragmentHomeOldBinding.inflate(inflater, container, false)
viewModel = ViewModelProvider(this)[HomeViewModel::class.java]
val rootView = binding.root
clientId = preferencesHelper?.clientId
presenter?.attachView(this)
setHasOptionsMenu(true)
binding.swipeHomeContainer.setColorSchemeResources(
R.color.blue_light,
Expand Down Expand Up @@ -107,7 +101,7 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
val menuItem = menu.findItem(R.id.menu_notifications)
val count = menuItem.actionView
tvNotificationCount = count?.findViewById(R.id.tv_notification_indicator)
presenter?.unreadNotificationsCount
viewModel.unreadNotificationsCount
count?.setOnClickListener {
startActivity(
Intent(
Expand Down Expand Up @@ -153,7 +147,7 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
super.onActivityCreated(savedInstanceState)
if (savedInstanceState != null) {
showUserDetails(savedInstanceState.getParcelable<Parcelable>(Constants.USER_DETAILS) as? Client)
presenter?.setUserProfile(preferencesHelper?.userProfileImage)
viewModel.setUserProfile(preferencesHelper?.userProfileImage)
showLoanAccountDetails(savedInstanceState.getDouble(Constants.TOTAL_LOAN))
showSavingAccountDetails(savedInstanceState.getDouble(Constants.TOTAL_SAVINGS))
}
Expand All @@ -164,12 +158,12 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
}

private fun loadClientData() {
presenter?.loadClientAccountDetails()
presenter?.userDetails
presenter?.userImage
viewModel.loadClientAccountDetails()
viewModel.userDetails
viewModel.userImage
}

override fun showUserInterface() {
fun showUserInterface() {
toolbarView = (activity as HomeActivity?)?.toolbar?.rootView
}

Expand All @@ -191,7 +185,7 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
*
* @param totalLoanAmount Total Loan amount
*/
override fun showLoanAccountDetails(totalLoanAmount: Double) {
private fun showLoanAccountDetails(totalLoanAmount: Double) {
this.totalLoanAmount = totalLoanAmount
binding.tvLoanTotalAmount.text = CurrencyUtil.formatCurrency(context, totalLoanAmount)
}
Expand All @@ -209,7 +203,7 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
*
* @param totalSavingAmount Total Saving amount
*/
override fun showSavingAccountDetails(totalSavingAmount: Double) {
private fun showSavingAccountDetails(totalSavingAmount: Double) {
this.totalSavingAmount = totalSavingAmount
binding.tvSavingTotalAmount.text = CurrencyUtil.formatCurrency(context, totalSavingAmount)
}
Expand All @@ -227,7 +221,7 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
*
* @param client Details about client
*/
override fun showUserDetails(client: Client?) {
fun showUserDetails(client: Client?) {
this.client = client
binding.tvUserName.text = getString(R.string.hello_client, client?.displayName)
}
Expand All @@ -237,7 +231,7 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
*
* @param bitmap Client Image
*/
override fun showUserImage(bitmap: Bitmap?) {
fun showUserImage(bitmap: Bitmap?) {
activity?.runOnUiThread {
if (bitmap != null) {
binding.ivCircularUserImage.visibility = View.VISIBLE
Expand Down Expand Up @@ -266,6 +260,34 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.homeUiState.observe(viewLifecycleOwner) {
when (it) {
is HomeUiState.Loading -> showProgress()
is HomeUiState.UserImage -> {
hideProgress()
showUserImage(it.image)
}
is HomeUiState.ClientAccountDetails -> {
hideProgress()
showLoanAccountDetails(it.loanAccounts)
showSavingAccountDetails(it.savingsAccounts)
}
is HomeUiState.Error -> {
hideProgress()
showError(getString(it.errorMessage))
}
is HomeUiState.UserDetails -> {
hideProgress()
showUserDetails(it.client)
}
is HomeUiState.UnreadNotificationsCount -> {
hideProgress()
showNotificationCount(it.count)
}
}
}

toggleVisibilityButton(
binding.btnSavingTotalAmountVisibility,
binding.tvSavingTotalAmount,
Expand All @@ -277,41 +299,44 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
binding.tvLoanTotalAmountHidden,
)

binding.llTotalLoan.setOnClickListener {
onClickLoan()
}
with(binding) {
llTotalLoan.setOnClickListener {
onClickLoan()
}

binding.llTotalSavings.setOnClickListener {
onClickSavings()
}
llTotalSavings.setOnClickListener {
onClickSavings()
}

binding.ivCircularUserImage.setOnClickListener {
userImageClicked()
}
ivCircularUserImage.setOnClickListener {
userImageClicked()
}

binding.llAccounts.setOnClickListener {
accountsClicked()
}
llAccounts.setOnClickListener {
accountsClicked()
}

binding.llTransfer.setOnClickListener {
transferClicked()
}
llTransfer.setOnClickListener {
transferClicked()
}

binding.llCharges.setOnClickListener {
chargesClicked()
}
llCharges.setOnClickListener {
chargesClicked()
}

binding.llApplyForLoan.setOnClickListener {
applyForLoan()
}
llApplyForLoan.setOnClickListener {
applyForLoan()
}

binding.llBeneficiaries.setOnClickListener {
beneficiaries()
}
llBeneficiaries.setOnClickListener {
beneficiaries()
}

binding.llSurveys.setOnClickListener {
surveys()
llSurveys.setOnClickListener {
surveys()
}
}

}

private fun toggleVisibilityButton(
Expand All @@ -332,7 +357,7 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
}
}

override fun showNotificationCount(count: Int) {
fun showNotificationCount(count: Int) {
if (count > 0) {
tvNotificationCount?.visibility = View.VISIBLE
tvNotificationCount?.text = count.toString()
Expand Down Expand Up @@ -424,7 +449,7 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
*
* @param errorMessage Error message that tells the user about the problem.
*/
override fun showError(errorMessage: String?) {
fun showError(errorMessage: String?) {
val checkedItem = (activity as HomeActivity?)?.checkedItem
if (checkedItem == R.id.item_about_us || checkedItem == R.id.item_help || checkedItem == R.id.item_settings) {
return
Expand All @@ -435,14 +460,14 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
/**
* Shows [SwipeRefreshLayout]
*/
override fun showProgress() {
fun showProgress() {
binding.swipeHomeContainer.isRefreshing = true
}

/**
* Hides [SwipeRefreshLayout]
*/
override fun hideProgress() {
fun hideProgress() {
binding.swipeHomeContainer.isRefreshing = false
}

Expand All @@ -452,7 +477,6 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
binding.swipeHomeContainer.isRefreshing = false
binding.swipeHomeContainer.removeAllViews()
}
presenter?.detachView()
_binding = null
}

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/org/mifos/mobile/utils/HomeUiState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.mifos.mobile.utils

import android.graphics.Bitmap
import org.mifos.mobile.models.client.Client

sealed class HomeUiState {
object Loading : HomeUiState()
data class Error(val errorMessage: Int) : HomeUiState()
data class ClientAccountDetails(val loanAccounts: Double, val savingsAccounts: Double) : HomeUiState()
data class UserDetails(val client: Client) : HomeUiState()
data class UserImage(val image: Bitmap?) : HomeUiState()
data class UnreadNotificationsCount(val count: Int) : HomeUiState()
}
Loading
Loading