From e6e95a2acfb27120c5fce7ff2b751e6633b95c14 Mon Sep 17 00:00:00 2001 From: Amr Salaheddin Abdelhady <52531091+amrsalah3@users.noreply.github.com> Date: Thu, 21 Jul 2022 17:50:50 +0200 Subject: [PATCH] AC-1032: Migrate Dashboard screen from MVP to MVVM architecture (#950) --- .../activities/dashboard/DashboardActivity.kt | 9 +- .../activities/dashboard/DashboardContract.kt | 26 ----- .../activities/dashboard/DashboardFragment.kt | 108 +++++++----------- .../dashboard/DashboardPresenter.kt | 28 ----- .../dashboard/DashboardViewModel.kt | 8 ++ 5 files changed, 51 insertions(+), 128 deletions(-) delete mode 100644 openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardContract.kt delete mode 100644 openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardPresenter.kt create mode 100644 openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardViewModel.kt diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardActivity.kt b/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardActivity.kt index 11d7f89415..8e2234d2ba 100644 --- a/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardActivity.kt +++ b/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardActivity.kt @@ -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. @@ -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() { @@ -119,4 +118,4 @@ class DashboardActivity : ACBaseActivity() { super.onDestroy() handler?.removeCallbacks(runnable) } -} \ No newline at end of file +} diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardContract.kt b/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardContract.kt deleted file mode 100644 index 52659acfd4..0000000000 --- a/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardContract.kt +++ /dev/null @@ -1,26 +0,0 @@ -/* - * The contents of this file are subject to the OpenMRS Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://license.openmrs.org - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the - * License for the specific language governing rights and limitations - * under the License. - * - * Copyright (C) OpenMRS, LLC. All Rights Reserved. - */ - -package org.openmrs.mobile.activities.dashboard - -import org.openmrs.mobile.activities.BasePresenterContract -import org.openmrs.mobile.activities.BaseView - -interface DashboardContract { - interface View : BaseView { - fun bindDrawableResources() - } - - interface Presenter : BasePresenterContract -} \ No newline at end of file diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardFragment.kt b/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardFragment.kt index 03a1f60d2c..4a2991a306 100644 --- a/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardFragment.kt +++ b/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardFragment.kt @@ -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 @@ -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.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? = null + private var mBitmapCache: SparseArray? = null override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) @@ -108,37 +97,23 @@ class DashboardFragment : ACBaseFragment(), 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() { @@ -148,15 +123,16 @@ class DashboardFragment : ACBaseFragment(), 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() + } } } @@ -166,10 +142,10 @@ class DashboardFragment : ACBaseFragment(), 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]) } } @@ -193,14 +169,6 @@ class DashboardFragment : ACBaseFragment(), Dashboa } } - /** - * Starts new Activity depending on which ImageView triggered it - */ - private fun startNewActivity(clazz: Class) { - val intent = Intent(activity, clazz) - startActivity(intent) - } - override fun onClick(v: View) { val directionToRegister = DashboardFragmentDirections.actionDashboardFragmentToAddEditPatientActivity() val directionToFindPatent = DashboardFragmentDirections.actionDashboardFragmentToSyncedPatientsActivity() @@ -219,12 +187,14 @@ class DashboardFragment : ACBaseFragment(), 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 { @@ -237,4 +207,4 @@ class DashboardFragment : ACBaseFragment(), Dashboa super.onDestroyView() _binding = null } -} \ No newline at end of file +} diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardPresenter.kt b/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardPresenter.kt deleted file mode 100644 index 10323f7282..0000000000 --- a/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardPresenter.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * The contents of this file are subject to the OpenMRS Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://license.openmrs.org - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the - * License for the specific language governing rights and limitations - * under the License. - * - * Copyright (C) OpenMRS, LLC. All Rights Reserved. - */ - -package org.openmrs.mobile.activities.dashboard - -import org.openmrs.mobile.activities.BasePresenter - -class DashboardPresenter(private val mDashboardView: DashboardContract.View) : BasePresenter(), DashboardContract.Presenter { - - override fun subscribe() { - mDashboardView.bindDrawableResources() - } - - init { - mDashboardView.setPresenter(this) - } -} \ No newline at end of file diff --git a/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardViewModel.kt b/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardViewModel.kt new file mode 100644 index 0000000000..4a289e3017 --- /dev/null +++ b/openmrs-client/src/main/java/org/openmrs/mobile/activities/dashboard/DashboardViewModel.kt @@ -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()