From f7b988bdc466942bb67a0699396dbcbb495cea7f Mon Sep 17 00:00:00 2001 From: FerdyRod Date: Mon, 12 Feb 2024 14:42:01 +0100 Subject: [PATCH 1/2] Adds a no connection listener to Sync Dialog to show snackbar to users --- .../dataSetTable/DataSetTableActivity.kt | 13 ++++++++ .../datasetDetail/DataSetDetailActivity.java | 11 +++++++ .../datasetList/DataSetListFragment.kt | 17 +++++++++- .../eventCapture/EventCaptureActivity.kt | 13 ++++++++ .../org/dhis2/usescases/main/MainActivity.kt | 14 ++++++++ .../usescases/main/program/ProgramFragment.kt | 17 +++++++++- .../ProgramEventDetailActivity.kt | 32 +++++++++++++++++-- .../searchTrackEntity/SearchTEActivity.java | 21 ++++++++++-- .../TeiDashboardMobileActivity.kt | 16 +++++++++- .../teidata/TEIDataFragment.kt | 16 +++++++++- .../utils/granularsync/SyncStatusDialog.kt | 17 ++++++++++ .../granularsync/SyncStatusDialogProvider.kt | 3 ++ app/src/main/res/values/strings.xml | 1 + .../commons/sync/OnNoConnectionListener.kt | 5 +++ .../commons/sync/SyncComponentProvider.kt | 1 + .../java/org/dhis2/commons/sync/SyncDialog.kt | 2 ++ 16 files changed, 191 insertions(+), 8 deletions(-) create mode 100644 commons/src/main/java/org/dhis2/commons/sync/OnNoConnectionListener.kt diff --git a/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/DataSetTableActivity.kt b/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/DataSetTableActivity.kt index f4ac9f4095..27a71a94af 100644 --- a/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/DataSetTableActivity.kt +++ b/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/DataSetTableActivity.kt @@ -38,6 +38,7 @@ import org.dhis2.commons.extensions.closeKeyboard import org.dhis2.commons.matomo.Labels.Companion.CLICK import org.dhis2.commons.popupmenu.AppMenuHelper import org.dhis2.commons.sync.OnDismissListener +import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.ActivityDatasetTableBinding import org.dhis2.usescases.datasets.dataSetTable.dataSetDetail.DataSetDetailFragment.Companion.create @@ -172,6 +173,18 @@ class DataSetTableActivity : ActivityGlobalAbstract(), DataSetTableContract.View if (hasChanged) presenter.updateData() } }) + .onNoConnectionListener( + object : OnNoConnectionListener { + override fun onNoConnection() { + val contextView = findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } + }, + ) .show(DATAVALUE_SYNC) } diff --git a/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/DataSetDetailActivity.java b/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/DataSetDetailActivity.java index 353e038802..e0529c293a 100644 --- a/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/DataSetDetailActivity.java +++ b/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/DataSetDetailActivity.java @@ -16,6 +16,8 @@ import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.ViewModelProvider; +import com.google.android.material.snackbar.Snackbar; + import org.dhis2.App; import org.dhis2.R; import org.dhis2.bindings.ExtensionsKt; @@ -26,6 +28,7 @@ import org.dhis2.commons.filters.FiltersAdapter; import org.dhis2.commons.orgunitselector.OUTreeFragment; import org.dhis2.commons.sync.ConflictType; +import org.dhis2.commons.sync.OnNoConnectionListener; import org.dhis2.databinding.ActivityDatasetDetailBinding; import org.dhis2.ui.ThemeManager; import org.dhis2.usescases.datasets.datasetDetail.datasetList.DataSetListFragment; @@ -240,10 +243,18 @@ public void setProgress(boolean active) { @Override public void showGranularSync() { presenter.trackDataSetGranularSync(); + View contextView = findViewById(R.id.navigationBar); new SyncStatusDialog.Builder() .withContext(this, null) .withSyncContext(new SyncContext.DataSet(dataSetUid)) .onDismissListener(hasChanged -> presenter.refreshList()) + .onNoConnectionListener(() -> + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT + ).show() + ) .show("DATASET_SYNC"); } } diff --git a/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListFragment.kt b/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListFragment.kt index 2e75a4831b..f983002ab4 100644 --- a/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListFragment.kt +++ b/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListFragment.kt @@ -5,9 +5,11 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.viewModels +import com.google.android.material.snackbar.Snackbar import org.dhis2.R import org.dhis2.commons.Constants import org.dhis2.commons.sync.OnDismissListener +import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.FragmentDataSetListBinding import org.dhis2.usescases.datasets.dataSetTable.DataSetTableActivity @@ -132,7 +134,20 @@ class DataSetListFragment : FragmentGlobalAbstract() { viewModel.updateData() } } - }).show(FRAGMENT_TAG) + }) + .onNoConnectionListener( + object : OnNoConnectionListener { + override fun onNoConnection() { + val contextView = activity.findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } + }, + ) + .show(FRAGMENT_TAG) } companion object { diff --git a/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventCapture/EventCaptureActivity.kt b/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventCapture/EventCaptureActivity.kt index f2cdb99aa2..3adb553017 100644 --- a/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventCapture/EventCaptureActivity.kt +++ b/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventCapture/EventCaptureActivity.kt @@ -22,6 +22,7 @@ import org.dhis2.commons.dialogs.AlertBottomDialog import org.dhis2.commons.dialogs.CustomDialog import org.dhis2.commons.dialogs.DialogClickListener import org.dhis2.commons.popupmenu.AppMenuHelper +import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.ActivityEventCaptureBinding import org.dhis2.ui.ErrorFieldList @@ -444,6 +445,18 @@ class EventCaptureActivity : SyncStatusDialog.Builder() .withContext(this) .withSyncContext(SyncContext.Event(eventUid!!)) + .onNoConnectionListener( + object : OnNoConnectionListener { + override fun onNoConnection() { + val contextView = findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } + }, + ) .show("EVENT_SYNC") } diff --git a/app/src/main/java/org/dhis2/usescases/main/MainActivity.kt b/app/src/main/java/org/dhis2/usescases/main/MainActivity.kt index a4f8ab5539..26f4a3c4dd 100644 --- a/app/src/main/java/org/dhis2/usescases/main/MainActivity.kt +++ b/app/src/main/java/org/dhis2/usescases/main/MainActivity.kt @@ -22,6 +22,7 @@ import androidx.core.view.ViewCompat import androidx.databinding.DataBindingUtil import androidx.drawerlayout.widget.DrawerLayout import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.google.android.material.snackbar.Snackbar import org.dhis2.BuildConfig import org.dhis2.R import org.dhis2.bindings.app @@ -30,6 +31,7 @@ import org.dhis2.commons.filters.FilterItem import org.dhis2.commons.filters.FilterManager import org.dhis2.commons.filters.FiltersAdapter import org.dhis2.commons.sync.OnDismissListener +import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.ActivityMainBinding import org.dhis2.ui.dialogs.alert.AlertDialog @@ -304,6 +306,18 @@ class MainActivity : } }, ) + .onNoConnectionListener( + object : OnNoConnectionListener { + override fun onNoConnection() { + val contextView = findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } + }, + ) .show("ALL_SYNC") } diff --git a/app/src/main/java/org/dhis2/usescases/main/program/ProgramFragment.kt b/app/src/main/java/org/dhis2/usescases/main/program/ProgramFragment.kt index 66430c381f..53294c5e57 100644 --- a/app/src/main/java/org/dhis2/usescases/main/program/ProgramFragment.kt +++ b/app/src/main/java/org/dhis2/usescases/main/program/ProgramFragment.kt @@ -16,6 +16,7 @@ import androidx.compose.runtime.livedata.observeAsState import androidx.compose.ui.platform.ViewCompositionStrategy import androidx.core.view.ViewCompat import androidx.databinding.DataBindingUtil +import com.google.android.material.snackbar.Snackbar import org.dhis2.App import org.dhis2.R import org.dhis2.android.rtsm.commons.Constants.INTENT_EXTRA_APP_CONFIG @@ -27,6 +28,7 @@ import org.dhis2.bindings.dp import org.dhis2.commons.filters.FilterManager import org.dhis2.commons.orgunitselector.OUTreeFragment import org.dhis2.commons.sync.OnDismissListener +import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.FragmentProgramBinding import org.dhis2.usescases.general.FragmentGlobalAbstract @@ -220,7 +222,20 @@ class ProgramFragment : FragmentGlobalAbstract(), ProgramView { } } }, - ).show(FRAGMENT_TAG) + ) + .onNoConnectionListener( + object : OnNoConnectionListener { + override fun onNoConnection() { + val contextView = activity?.findViewById(R.id.navigationBar) + Snackbar.make( + contextView!!, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } + }, + ) + .show(FRAGMENT_TAG) } fun sharedView() = binding.drawerLayout diff --git a/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailActivity.kt b/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailActivity.kt index baa4b748e6..dd81e4506f 100644 --- a/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailActivity.kt +++ b/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailActivity.kt @@ -12,6 +12,7 @@ import androidx.activity.viewModels import androidx.constraintlayout.widget.ConstraintSet import androidx.databinding.DataBindingUtil import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.google.android.material.snackbar.Snackbar import dhis2.org.analytics.charts.ui.GroupAnalyticsFragment import org.dhis2.R import org.dhis2.bindings.app @@ -27,6 +28,7 @@ import org.dhis2.commons.matomo.Actions.Companion.CREATE_EVENT import org.dhis2.commons.network.NetworkUtils import org.dhis2.commons.orgunitselector.OUTreeFragment import org.dhis2.commons.sync.OnDismissListener +import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.ActivityProgramEventDetailBinding import org.dhis2.ui.ThemeManager @@ -195,7 +197,20 @@ class ProgramEventDetailActivity : override fun onDismiss(hasChanged: Boolean) { if (hasChanged) FilterManager.getInstance().publishData() } - }).show("EVENT_SYNC") + }) + .onNoConnectionListener( + object : OnNoConnectionListener { + override fun onNoConnection() { + val contextView = findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } + }, + ) + .show("EVENT_SYNC") } public override fun onPause() { @@ -376,7 +391,20 @@ class ProgramEventDetailActivity : override fun onDismiss(hasChanged: Boolean) { if (hasChanged) FilterManager.getInstance().publishData() } - }).show(FRAGMENT_TAG) + }) + .onNoConnectionListener( + object : OnNoConnectionListener { + override fun onNoConnection() { + val contextView = findViewById(R.id.rootView) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } + }, + ) + .show(FRAGMENT_TAG) } private fun showList() { diff --git a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEActivity.java b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEActivity.java index 90b6d03ed3..713f3cdecd 100644 --- a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEActivity.java +++ b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEActivity.java @@ -332,6 +332,7 @@ protected void onSaveInstanceState(@NonNull Bundle outState) { } private void openSyncDialog() { + View contextView = findViewById(R.id.navigationBar); new SyncStatusDialog.Builder() .withContext(this, null) .withSyncContext( @@ -339,7 +340,15 @@ private void openSyncDialog() { ) .onDismissListener(hasChanged -> { if (hasChanged) viewModel.refreshData(); - }).show("PROGRAM_SYNC"); + }) + .onNoConnectionListener(() -> + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT + ).show() + ) + .show("PROGRAM_SYNC"); } @Override @@ -582,6 +591,7 @@ public void setPrograms(List programs) { @Override public void showSyncDialog(String enrollmentUid) { + View contextView = findViewById(R.id.navigationBar); new SyncStatusDialog.Builder() .withContext(this, null) .withSyncContext( @@ -589,7 +599,14 @@ public void showSyncDialog(String enrollmentUid) { ) .onDismissListener(hasChanged -> { if (hasChanged) viewModel.refreshData(); - }).show("TEI_SYNC"); + }) + .onNoConnectionListener(() -> + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT + ).show() + ).show("TEI_SYNC"); } private void setInitialProgram(List programs) { diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/TeiDashboardMobileActivity.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/TeiDashboardMobileActivity.kt index e3ba3bf62a..b64e048489 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/TeiDashboardMobileActivity.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/TeiDashboardMobileActivity.kt @@ -21,6 +21,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModelProvider import androidx.viewpager2.widget.ViewPager2 +import com.google.android.material.snackbar.Snackbar import org.dhis2.App import org.dhis2.R import org.dhis2.commons.Constants @@ -31,6 +32,7 @@ import org.dhis2.commons.network.NetworkUtils import org.dhis2.commons.popupmenu.AppMenuHelper import org.dhis2.commons.resources.ResourceManager import org.dhis2.commons.sync.OnDismissListener +import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.ActivityDashboardMobileBinding import org.dhis2.ui.ThemeManager @@ -302,7 +304,19 @@ class TeiDashboardMobileActivity : finish() } } - }).show(TEI_SYNC) + }) + .onNoConnectionListener( + object : OnNoConnectionListener { + override fun onNoConnection() { + val contextView = findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } + }, + ).show(TEI_SYNC) } } diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataFragment.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataFragment.kt index 145ef18362..87591930ac 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataFragment.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataFragment.kt @@ -18,6 +18,7 @@ import androidx.recyclerview.widget.DividerItemDecoration import com.bumptech.glide.Glide import com.bumptech.glide.load.resource.bitmap.CircleCrop import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions +import com.google.android.material.snackbar.Snackbar import io.reactivex.Flowable import io.reactivex.Single import io.reactivex.functions.Consumer @@ -40,6 +41,7 @@ import org.dhis2.commons.orgunitselector.OUTreeFragment import org.dhis2.commons.resources.ColorUtils import org.dhis2.commons.resources.ObjectStyleUtils.getIconResource import org.dhis2.commons.sync.OnDismissListener +import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext.EnrollmentEvent import org.dhis2.databinding.FragmentTeiDataBinding import org.dhis2.usescases.eventsWithoutRegistration.eventInitial.EventInitialActivity @@ -619,7 +621,19 @@ class TEIDataFragment : FragmentGlobalAbstract(), TEIDataContracts.View { override fun onDismiss(hasChanged: Boolean) { if (hasChanged) FilterManager.getInstance().publishData() } - }).show(enrollmentUid) + }) + .onNoConnectionListener( + object : OnNoConnectionListener { + override fun onNoConnection() { + val contextView = activity?.findViewById(R.id.navigationBar) + Snackbar.make( + contextView!!, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } + }, + ).show(enrollmentUid) } override fun displayCatComboOptionSelectorForEvents(data: List) { diff --git a/app/src/main/java/org/dhis2/utils/granularsync/SyncStatusDialog.kt b/app/src/main/java/org/dhis2/utils/granularsync/SyncStatusDialog.kt index 4f93b3944b..786e279a7b 100644 --- a/app/src/main/java/org/dhis2/utils/granularsync/SyncStatusDialog.kt +++ b/app/src/main/java/org/dhis2/utils/granularsync/SyncStatusDialog.kt @@ -32,6 +32,7 @@ import org.dhis2.bindings.showSMS import org.dhis2.commons.date.toDateSpan import org.dhis2.commons.network.NetworkUtils import org.dhis2.commons.sync.OnDismissListener +import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.OnSyncNavigationListener import org.dhis2.commons.sync.SyncContext import org.dhis2.commons.ui.icons.SyncStateIcon @@ -69,6 +70,8 @@ class SyncStatusDialog : BottomSheetDialogFragment(), GranularSyncContracts.View private val viewModel: GranularSyncPresenter by viewModels { viewModelFactory } + var onNoConnectionListener: OnNoConnectionListener? = null + override fun onAttach(context: Context) { super.onAttach(context) (context.applicationContext as App).serverComponent()!!.plus( @@ -172,9 +175,16 @@ class SyncStatusDialog : BottomSheetDialogFragment(), GranularSyncContracts.View networkUtils.isOnline() -> syncGranular() viewModel.canSendSMS() && viewModel.isSMSEnabled(context?.showSMS() == true) -> syncSms() + !networkUtils.isOnline() && + !viewModel.isSMSEnabled(context?.showSMS() == true) -> showSnackbar() } } + private fun showSnackbar() { + dismiss() + onNoConnectionListener?.onNoConnection() + } + private fun syncGranular() { syncing = true viewModel.initGranularSync().observe( @@ -335,6 +345,7 @@ class SyncStatusDialog : BottomSheetDialogFragment(), GranularSyncContracts.View private var navigator: SyncStatusDialogNavigator? = null private lateinit var syncContext: SyncContext private var dismissListener: OnDismissListener? = null + private var noConnectionListener: OnNoConnectionListener? = null fun withContext( context: FragmentActivity, @@ -372,12 +383,18 @@ class SyncStatusDialog : BottomSheetDialogFragment(), GranularSyncContracts.View return this } + fun onNoConnectionListener(noConnectionListener: OnNoConnectionListener): Builder { + this.noConnectionListener = noConnectionListener + return this + } + private fun build(): SyncStatusDialog { return SyncStatusDialog().apply { arguments = Bundle().apply { putParcelable(SYNC_CONTEXT, syncContext) } dismissListenerDialog = dismissListener + onNoConnectionListener = noConnectionListener syncStatusDialogNavigator = navigator } } diff --git a/app/src/main/java/org/dhis2/utils/granularsync/SyncStatusDialogProvider.kt b/app/src/main/java/org/dhis2/utils/granularsync/SyncStatusDialogProvider.kt index ff67e89a02..8cdd3e45eb 100644 --- a/app/src/main/java/org/dhis2/utils/granularsync/SyncStatusDialogProvider.kt +++ b/app/src/main/java/org/dhis2/utils/granularsync/SyncStatusDialogProvider.kt @@ -2,6 +2,7 @@ package org.dhis2.utils.granularsync import androidx.fragment.app.FragmentActivity import org.dhis2.commons.sync.OnDismissListener +import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.OnSyncNavigationListener import org.dhis2.commons.sync.SyncComponentProvider import org.dhis2.commons.sync.SyncContext @@ -13,6 +14,7 @@ class SyncStatusDialogProvider : SyncComponentProvider { syncContext: SyncContext, dismissListener: OnDismissListener?, onSyncNavigationListener: OnSyncNavigationListener?, + onNoConnectionListener: OnNoConnectionListener?, ) { val syncBuilder = SyncStatusDialog.Builder() .withContext(activity, onSyncNavigationListener) @@ -20,6 +22,7 @@ class SyncStatusDialogProvider : SyncComponentProvider { with(syncBuilder) { dismissListener?.let { onDismissListener(it) } + onNoConnectionListener?.let { onNoConnectionListener(it) } } syncBuilder .show(syncContext.conflictType().name) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5523c9fb57..de0ede2810 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -963,6 +963,7 @@ Remove Coordinates You don’t have access to data.\nContact your administrator. + Looks like you\'re offline. Please check you connection Delete this %s? This %s and all its data across all programs will be deleted. This action cannot be undone. Remove from %s? diff --git a/commons/src/main/java/org/dhis2/commons/sync/OnNoConnectionListener.kt b/commons/src/main/java/org/dhis2/commons/sync/OnNoConnectionListener.kt new file mode 100644 index 0000000000..5817f18355 --- /dev/null +++ b/commons/src/main/java/org/dhis2/commons/sync/OnNoConnectionListener.kt @@ -0,0 +1,5 @@ +package org.dhis2.commons.sync + +interface OnNoConnectionListener { + fun onNoConnection() +} diff --git a/commons/src/main/java/org/dhis2/commons/sync/SyncComponentProvider.kt b/commons/src/main/java/org/dhis2/commons/sync/SyncComponentProvider.kt index dd7065caa7..4c820108bf 100644 --- a/commons/src/main/java/org/dhis2/commons/sync/SyncComponentProvider.kt +++ b/commons/src/main/java/org/dhis2/commons/sync/SyncComponentProvider.kt @@ -8,5 +8,6 @@ interface SyncComponentProvider { syncContext: SyncContext, dismissListener: OnDismissListener? = null, onSyncNavigationListener: OnSyncNavigationListener? = null, + onNoConnectionListener: OnNoConnectionListener? = null, ) } diff --git a/commons/src/main/java/org/dhis2/commons/sync/SyncDialog.kt b/commons/src/main/java/org/dhis2/commons/sync/SyncDialog.kt index 6a8ae720f5..021572a310 100644 --- a/commons/src/main/java/org/dhis2/commons/sync/SyncDialog.kt +++ b/commons/src/main/java/org/dhis2/commons/sync/SyncDialog.kt @@ -9,6 +9,7 @@ class SyncDialog( val syncContext: SyncContext, private val dismissListener: OnDismissListener? = null, private val onSyncNavigationListener: OnSyncNavigationListener? = null, + private val onNoConnectionListener: OnNoConnectionListener? = null, ) { fun show() { (activity.applicationContext as? ComponentProvider) @@ -18,6 +19,7 @@ class SyncDialog( syncContext, dismissListener, onSyncNavigationListener, + onNoConnectionListener, ) } } From 5dca594ba19661b0a909a282a6b22e4b854ec39e Mon Sep 17 00:00:00 2001 From: FerdyRod Date: Tue, 13 Feb 2024 16:42:54 +0100 Subject: [PATCH 2/2] resolve code smell. now dialog uses SAM interface for the NoConnectionListener --- .../dataSetTable/DataSetTableActivity.kt | 21 ++++------ .../datasetList/DataSetListFragment.kt | 21 ++++------ .../eventCapture/EventCaptureActivity.kt | 21 ++++------ .../org/dhis2/usescases/main/MainActivity.kt | 21 ++++------ .../usescases/main/program/ProgramFragment.kt | 21 ++++------ .../ProgramEventDetailActivity.kt | 41 ++++++++----------- .../TeiDashboardMobileActivity.kt | 21 ++++------ .../teidata/TEIDataFragment.kt | 21 ++++------ .../commons/sync/OnNoConnectionListener.kt | 2 +- 9 files changed, 73 insertions(+), 117 deletions(-) diff --git a/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/DataSetTableActivity.kt b/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/DataSetTableActivity.kt index 27a71a94af..c0f162192b 100644 --- a/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/DataSetTableActivity.kt +++ b/app/src/main/java/org/dhis2/usescases/datasets/dataSetTable/DataSetTableActivity.kt @@ -38,7 +38,6 @@ import org.dhis2.commons.extensions.closeKeyboard import org.dhis2.commons.matomo.Labels.Companion.CLICK import org.dhis2.commons.popupmenu.AppMenuHelper import org.dhis2.commons.sync.OnDismissListener -import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.ActivityDatasetTableBinding import org.dhis2.usescases.datasets.dataSetTable.dataSetDetail.DataSetDetailFragment.Companion.create @@ -173,18 +172,14 @@ class DataSetTableActivity : ActivityGlobalAbstract(), DataSetTableContract.View if (hasChanged) presenter.updateData() } }) - .onNoConnectionListener( - object : OnNoConnectionListener { - override fun onNoConnection() { - val contextView = findViewById(R.id.navigationBar) - Snackbar.make( - contextView, - R.string.sync_offline_check_connection, - Snackbar.LENGTH_SHORT, - ).show() - } - }, - ) + .onNoConnectionListener { + val contextView = findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } .show(DATAVALUE_SYNC) } diff --git a/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListFragment.kt b/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListFragment.kt index f983002ab4..476e90bae2 100644 --- a/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListFragment.kt +++ b/app/src/main/java/org/dhis2/usescases/datasets/datasetDetail/datasetList/DataSetListFragment.kt @@ -9,7 +9,6 @@ import com.google.android.material.snackbar.Snackbar import org.dhis2.R import org.dhis2.commons.Constants import org.dhis2.commons.sync.OnDismissListener -import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.FragmentDataSetListBinding import org.dhis2.usescases.datasets.dataSetTable.DataSetTableActivity @@ -135,18 +134,14 @@ class DataSetListFragment : FragmentGlobalAbstract() { } } }) - .onNoConnectionListener( - object : OnNoConnectionListener { - override fun onNoConnection() { - val contextView = activity.findViewById(R.id.navigationBar) - Snackbar.make( - contextView, - R.string.sync_offline_check_connection, - Snackbar.LENGTH_SHORT, - ).show() - } - }, - ) + .onNoConnectionListener { + val contextView = activity.findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } .show(FRAGMENT_TAG) } diff --git a/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventCapture/EventCaptureActivity.kt b/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventCapture/EventCaptureActivity.kt index 3adb553017..ec0907f55d 100644 --- a/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventCapture/EventCaptureActivity.kt +++ b/app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventCapture/EventCaptureActivity.kt @@ -22,7 +22,6 @@ import org.dhis2.commons.dialogs.AlertBottomDialog import org.dhis2.commons.dialogs.CustomDialog import org.dhis2.commons.dialogs.DialogClickListener import org.dhis2.commons.popupmenu.AppMenuHelper -import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.ActivityEventCaptureBinding import org.dhis2.ui.ErrorFieldList @@ -445,18 +444,14 @@ class EventCaptureActivity : SyncStatusDialog.Builder() .withContext(this) .withSyncContext(SyncContext.Event(eventUid!!)) - .onNoConnectionListener( - object : OnNoConnectionListener { - override fun onNoConnection() { - val contextView = findViewById(R.id.navigationBar) - Snackbar.make( - contextView, - R.string.sync_offline_check_connection, - Snackbar.LENGTH_SHORT, - ).show() - } - }, - ) + .onNoConnectionListener { + val contextView = findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } .show("EVENT_SYNC") } diff --git a/app/src/main/java/org/dhis2/usescases/main/MainActivity.kt b/app/src/main/java/org/dhis2/usescases/main/MainActivity.kt index 26f4a3c4dd..a66a3bf8b2 100644 --- a/app/src/main/java/org/dhis2/usescases/main/MainActivity.kt +++ b/app/src/main/java/org/dhis2/usescases/main/MainActivity.kt @@ -31,7 +31,6 @@ import org.dhis2.commons.filters.FilterItem import org.dhis2.commons.filters.FilterManager import org.dhis2.commons.filters.FiltersAdapter import org.dhis2.commons.sync.OnDismissListener -import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.ActivityMainBinding import org.dhis2.ui.dialogs.alert.AlertDialog @@ -306,18 +305,14 @@ class MainActivity : } }, ) - .onNoConnectionListener( - object : OnNoConnectionListener { - override fun onNoConnection() { - val contextView = findViewById(R.id.navigationBar) - Snackbar.make( - contextView, - R.string.sync_offline_check_connection, - Snackbar.LENGTH_SHORT, - ).show() - } - }, - ) + .onNoConnectionListener { + val contextView = findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } .show("ALL_SYNC") } diff --git a/app/src/main/java/org/dhis2/usescases/main/program/ProgramFragment.kt b/app/src/main/java/org/dhis2/usescases/main/program/ProgramFragment.kt index 53294c5e57..4e0d1653d3 100644 --- a/app/src/main/java/org/dhis2/usescases/main/program/ProgramFragment.kt +++ b/app/src/main/java/org/dhis2/usescases/main/program/ProgramFragment.kt @@ -28,7 +28,6 @@ import org.dhis2.bindings.dp import org.dhis2.commons.filters.FilterManager import org.dhis2.commons.orgunitselector.OUTreeFragment import org.dhis2.commons.sync.OnDismissListener -import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.FragmentProgramBinding import org.dhis2.usescases.general.FragmentGlobalAbstract @@ -223,18 +222,14 @@ class ProgramFragment : FragmentGlobalAbstract(), ProgramView { } }, ) - .onNoConnectionListener( - object : OnNoConnectionListener { - override fun onNoConnection() { - val contextView = activity?.findViewById(R.id.navigationBar) - Snackbar.make( - contextView!!, - R.string.sync_offline_check_connection, - Snackbar.LENGTH_SHORT, - ).show() - } - }, - ) + .onNoConnectionListener { + val contextView = activity?.findViewById(R.id.navigationBar) + Snackbar.make( + contextView!!, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } .show(FRAGMENT_TAG) } diff --git a/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailActivity.kt b/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailActivity.kt index dd81e4506f..d7a7887e1c 100644 --- a/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailActivity.kt +++ b/app/src/main/java/org/dhis2/usescases/programEventDetail/ProgramEventDetailActivity.kt @@ -28,7 +28,6 @@ import org.dhis2.commons.matomo.Actions.Companion.CREATE_EVENT import org.dhis2.commons.network.NetworkUtils import org.dhis2.commons.orgunitselector.OUTreeFragment import org.dhis2.commons.sync.OnDismissListener -import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.ActivityProgramEventDetailBinding import org.dhis2.ui.ThemeManager @@ -198,18 +197,14 @@ class ProgramEventDetailActivity : if (hasChanged) FilterManager.getInstance().publishData() } }) - .onNoConnectionListener( - object : OnNoConnectionListener { - override fun onNoConnection() { - val contextView = findViewById(R.id.navigationBar) - Snackbar.make( - contextView, - R.string.sync_offline_check_connection, - Snackbar.LENGTH_SHORT, - ).show() - } - }, - ) + .onNoConnectionListener { + val contextView = findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } .show("EVENT_SYNC") } @@ -392,18 +387,14 @@ class ProgramEventDetailActivity : if (hasChanged) FilterManager.getInstance().publishData() } }) - .onNoConnectionListener( - object : OnNoConnectionListener { - override fun onNoConnection() { - val contextView = findViewById(R.id.rootView) - Snackbar.make( - contextView, - R.string.sync_offline_check_connection, - Snackbar.LENGTH_SHORT, - ).show() - } - }, - ) + .onNoConnectionListener { + val contextView = findViewById(R.id.rootView) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + } .show(FRAGMENT_TAG) } diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/TeiDashboardMobileActivity.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/TeiDashboardMobileActivity.kt index b64e048489..dde3387b99 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/TeiDashboardMobileActivity.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/TeiDashboardMobileActivity.kt @@ -32,7 +32,6 @@ import org.dhis2.commons.network.NetworkUtils import org.dhis2.commons.popupmenu.AppMenuHelper import org.dhis2.commons.resources.ResourceManager import org.dhis2.commons.sync.OnDismissListener -import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext import org.dhis2.databinding.ActivityDashboardMobileBinding import org.dhis2.ui.ThemeManager @@ -305,18 +304,14 @@ class TeiDashboardMobileActivity : } } }) - .onNoConnectionListener( - object : OnNoConnectionListener { - override fun onNoConnection() { - val contextView = findViewById(R.id.navigationBar) - Snackbar.make( - contextView, - R.string.sync_offline_check_connection, - Snackbar.LENGTH_SHORT, - ).show() - } - }, - ).show(TEI_SYNC) + .onNoConnectionListener { + val contextView = findViewById(R.id.navigationBar) + Snackbar.make( + contextView, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + }.show(TEI_SYNC) } } diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataFragment.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataFragment.kt index 87591930ac..985d3fcb08 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataFragment.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/dashboardfragments/teidata/TEIDataFragment.kt @@ -41,7 +41,6 @@ import org.dhis2.commons.orgunitselector.OUTreeFragment import org.dhis2.commons.resources.ColorUtils import org.dhis2.commons.resources.ObjectStyleUtils.getIconResource import org.dhis2.commons.sync.OnDismissListener -import org.dhis2.commons.sync.OnNoConnectionListener import org.dhis2.commons.sync.SyncContext.EnrollmentEvent import org.dhis2.databinding.FragmentTeiDataBinding import org.dhis2.usescases.eventsWithoutRegistration.eventInitial.EventInitialActivity @@ -622,18 +621,14 @@ class TEIDataFragment : FragmentGlobalAbstract(), TEIDataContracts.View { if (hasChanged) FilterManager.getInstance().publishData() } }) - .onNoConnectionListener( - object : OnNoConnectionListener { - override fun onNoConnection() { - val contextView = activity?.findViewById(R.id.navigationBar) - Snackbar.make( - contextView!!, - R.string.sync_offline_check_connection, - Snackbar.LENGTH_SHORT, - ).show() - } - }, - ).show(enrollmentUid) + .onNoConnectionListener { + val contextView = activity?.findViewById(R.id.navigationBar) + Snackbar.make( + contextView!!, + R.string.sync_offline_check_connection, + Snackbar.LENGTH_SHORT, + ).show() + }.show(enrollmentUid) } override fun displayCatComboOptionSelectorForEvents(data: List) { diff --git a/commons/src/main/java/org/dhis2/commons/sync/OnNoConnectionListener.kt b/commons/src/main/java/org/dhis2/commons/sync/OnNoConnectionListener.kt index 5817f18355..5d75bdbd3f 100644 --- a/commons/src/main/java/org/dhis2/commons/sync/OnNoConnectionListener.kt +++ b/commons/src/main/java/org/dhis2/commons/sync/OnNoConnectionListener.kt @@ -1,5 +1,5 @@ package org.dhis2.commons.sync -interface OnNoConnectionListener { +fun interface OnNoConnectionListener { fun onNoConnection() }