Skip to content

Commit

Permalink
AC-1032: Migrate Dashboard screen from MVP to MVVM architecture (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrsalah3 authored Jul 21, 2022
1 parent 40461e4 commit e6e95a2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import android.os.Bundle
import android.os.Handler
import androidx.navigation.fragment.NavHostFragment
import com.openmrs.android_sdk.utilities.ToastUtil
import dagger.hilt.android.AndroidEntryPoint
import org.openmrs.mobile.R
import org.openmrs.mobile.activities.ACBaseActivity


@AndroidEntryPoint
class DashboardActivity : ACBaseActivity() {

/*TODO: Permission handling to be coded later, moving to SDK 22 for now.
Expand Down Expand Up @@ -99,9 +100,7 @@ class DashboardActivity : ACBaseActivity() {
super.onResume()
val navHostFragment = supportFragmentManager.findFragmentById(R.id.dashboard_nav_host_fragment) as NavHostFragment
val dashboardFragment: DashboardFragment? = navHostFragment.childFragmentManager.primaryNavigationFragment as DashboardFragment?
if (dashboardFragment != null) {
DashboardPresenter(dashboardFragment)
}
dashboardFragment?.bindDrawableResources()
}

override fun onBackPressed() {
Expand All @@ -119,4 +118,4 @@ class DashboardActivity : ACBaseActivity() {
super.onDestroy()
handler?.removeCallbacks(runnable)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package org.openmrs.mobile.activities.dashboard

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.util.SparseArray
Expand All @@ -22,35 +21,25 @@ import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.navigation.fragment.findNavController
import com.openmrs.android_sdk.utilities.ApplicationConstants
import com.openmrs.android_sdk.utilities.ImageUtils
import com.github.amlcurran.showcaseview.OnShowcaseEventListener
import com.github.amlcurran.showcaseview.ShowcaseView
import com.github.amlcurran.showcaseview.targets.Target
import com.github.amlcurran.showcaseview.targets.ViewTarget
import com.openmrs.android_sdk.utilities.ApplicationConstants
import com.openmrs.android_sdk.utilities.ImageUtils
import dagger.hilt.android.AndroidEntryPoint
import org.openmrs.mobile.R
import org.openmrs.mobile.activities.ACBaseActivity
import org.openmrs.mobile.activities.ACBaseFragment
import org.openmrs.mobile.activities.BaseFragment
import org.openmrs.mobile.databinding.FragmentDashboardBinding
import org.openmrs.mobile.utilities.ThemeUtils

class DashboardFragment : ACBaseFragment<DashboardContract.Presenter>(), DashboardContract.View, View.OnClickListener {
@AndroidEntryPoint
class DashboardFragment : BaseFragment(), View.OnClickListener {

private var _binding: FragmentDashboardBinding? = null
private val binding get() = _binding!!
private var mFindPatientButton: ImageView? = null
private var mRegistryPatientButton: ImageView? = null
private var mActiveVisitsButton: ImageView? = null
private var mCaptureVitalsButton: ImageView? = null
private var mProviderManagementButton: ImageView? = null
private var mFindPatientView: RelativeLayout? = null
private var mRegistryPatientView: RelativeLayout? = null
private var mActiveVisitsView: RelativeLayout? = null
private var mCaptureVitalsView: RelativeLayout? = null
private var mProviderManagementView: RelativeLayout? = null
private var mBitmapCache: SparseArray<Bitmap?>? = null
private var mBitmapCache: SparseArray<Bitmap>? = null

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
Expand Down Expand Up @@ -108,37 +97,23 @@ class DashboardFragment : ACBaseFragment<DashboardContract.Presenter>(), Dashboa
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentDashboardBinding.inflate(inflater, container, false)
val root = binding.root
if (root != null) {
initFragmentFields(binding)
setListeners()
}
return root
}

private fun initFragmentFields(binding: FragmentDashboardBinding) {
mFindPatientButton = binding.findPatientButton
mRegistryPatientButton = binding.registryPatientButton
mActiveVisitsButton = binding.activeVisitsButton
mCaptureVitalsButton = binding.captureVitalsButton
mProviderManagementButton = binding.dashboardProviderManagementButton
mFindPatientView = binding.findPatientView
mRegistryPatientView = binding.registryPatientView
mCaptureVitalsView = binding.captureVitalsView
mActiveVisitsView = binding.activeVisitsView
mProviderManagementView = binding.dashboardProviderManagementView
bindDrawableResources()
setListeners()

return binding.root
}

private fun setListeners() {
mActiveVisitsView!!.setOnClickListener(this)
mRegistryPatientView!!.setOnClickListener(this)
mFindPatientView!!.setOnClickListener(this)
mCaptureVitalsView!!.setOnClickListener(this)
mProviderManagementView!!.setOnClickListener(this)
with(binding) {
activeVisitsView.setOnClickListener(this@DashboardFragment)
registryPatientView.setOnClickListener(this@DashboardFragment)
findPatientView.setOnClickListener(this@DashboardFragment)
captureVitalsView.setOnClickListener(this@DashboardFragment)
dashboardProviderManagementView.setOnClickListener(this@DashboardFragment)
}
}

override fun onDestroy() {
Expand All @@ -148,15 +123,16 @@ class DashboardFragment : ACBaseFragment<DashboardContract.Presenter>(), Dashboa

/**
* Binds drawable resources to all dashboard buttons
* Initially called by this view's presenter
*/
override fun bindDrawableResources() {
bindDrawableResource(mFindPatientButton, R.drawable.ico_search)
bindDrawableResource(mRegistryPatientButton, R.drawable.ico_registry)
bindDrawableResource(mActiveVisitsButton, R.drawable.ico_visits)
bindDrawableResource(mCaptureVitalsButton, R.drawable.ico_vitals)
if (ThemeUtils.isDarkModeActivated()) {
changeColorOfDashboardIcons()
fun bindDrawableResources() {
with(binding) {
bindDrawableResource(findPatientButton, R.drawable.ico_search)
bindDrawableResource(registryPatientButton, R.drawable.ico_registry)
bindDrawableResource(activeVisitsButton, R.drawable.ico_visits)
bindDrawableResource(captureVitalsButton, R.drawable.ico_vitals)
if (ThemeUtils.isDarkModeActivated()) {
changeColorOfDashboardIcons()
}
}
}

Expand All @@ -166,10 +142,10 @@ class DashboardFragment : ACBaseFragment<DashboardContract.Presenter>(), Dashboa
* @param imageView ImageView to bind resource to
* @param drawableId id of drawable resource (for example R.id.somePicture);
*/
private fun bindDrawableResource(imageView: ImageView?, drawableId: Int) {
private fun bindDrawableResource(imageView: ImageView, drawableId: Int) {
mBitmapCache = SparseArray()
if (view != null) {
createImageBitmap(drawableId, imageView!!.layoutParams)
createImageBitmap(drawableId, imageView.layoutParams)
imageView.setImageBitmap(mBitmapCache!![drawableId])
}
}
Expand All @@ -193,14 +169,6 @@ class DashboardFragment : ACBaseFragment<DashboardContract.Presenter>(), Dashboa
}
}

/**
* Starts new Activity depending on which ImageView triggered it
*/
private fun startNewActivity(clazz: Class<out ACBaseActivity?>) {
val intent = Intent(activity, clazz)
startActivity(intent)
}

override fun onClick(v: View) {
val directionToRegister = DashboardFragmentDirections.actionDashboardFragmentToAddEditPatientActivity()
val directionToFindPatent = DashboardFragmentDirections.actionDashboardFragmentToSyncedPatientsActivity()
Expand All @@ -219,12 +187,14 @@ class DashboardFragment : ACBaseFragment<DashboardContract.Presenter>(), Dashboa
}

private fun changeColorOfDashboardIcons() {
val greenColorResId = R.color.green
ImageUtils.changeImageViewTint(context, mActiveVisitsButton, greenColorResId)
ImageUtils.changeImageViewTint(context, mCaptureVitalsButton, greenColorResId)
ImageUtils.changeImageViewTint(context, mFindPatientButton, greenColorResId)
ImageUtils.changeImageViewTint(context, mRegistryPatientButton, greenColorResId)
ImageUtils.changeImageViewTint(context, mProviderManagementButton, greenColorResId)
with(binding) {
val greenColorResId = R.color.green
ImageUtils.changeImageViewTint(context, activeVisitsButton, greenColorResId)
ImageUtils.changeImageViewTint(context, captureVitalsButton, greenColorResId)
ImageUtils.changeImageViewTint(context, findPatientButton, greenColorResId)
ImageUtils.changeImageViewTint(context, registryPatientButton, greenColorResId)
ImageUtils.changeImageViewTint(context, dashboardProviderManagementButton, greenColorResId)
}
}

companion object {
Expand All @@ -237,4 +207,4 @@ class DashboardFragment : ACBaseFragment<DashboardContract.Presenter>(), Dashboa
super.onDestroyView()
_binding = null
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.openmrs.mobile.activities.dashboard

import dagger.hilt.android.lifecycle.HiltViewModel
import org.openmrs.mobile.activities.BaseViewModel
import javax.inject.Inject

@HiltViewModel
class DashboardViewModel @Inject constructor() : BaseViewModel<Unit>()

0 comments on commit e6e95a2

Please sign in to comment.