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

fix: [ANDROAPP-5658] Shows snackbar when user does not have connection when trying to sync #3496

Merged
merged 2 commits into from
Feb 14, 2024
Merged
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 @@ -172,6 +172,14 @@ class DataSetTableActivity : ActivityGlobalAbstract(), DataSetTableContract.View
if (hasChanged) presenter.updateData()
}
})
.onNoConnectionListener {
val contextView = findViewById<View>(R.id.navigationBar)
Snackbar.make(
contextView,
R.string.sync_offline_check_connection,
Snackbar.LENGTH_SHORT,
).show()
}
.show(DATAVALUE_SYNC)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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
Expand Down Expand Up @@ -132,7 +133,16 @@ class DataSetListFragment : FragmentGlobalAbstract() {
viewModel.updateData()
}
}
}).show(FRAGMENT_TAG)
})
.onNoConnectionListener {
val contextView = activity.findViewById<View>(R.id.navigationBar)
Snackbar.make(
contextView,
R.string.sync_offline_check_connection,
Snackbar.LENGTH_SHORT,
).show()
}
.show(FRAGMENT_TAG)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,14 @@ class EventCaptureActivity :
SyncStatusDialog.Builder()
.withContext(this)
.withSyncContext(SyncContext.Event(eventUid!!))
.onNoConnectionListener {
val contextView = findViewById<View>(R.id.navigationBar)
Snackbar.make(
contextView,
R.string.sync_offline_check_connection,
Snackbar.LENGTH_SHORT,
).show()
}
.show("EVENT_SYNC")
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/org/dhis2/usescases/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -304,6 +305,14 @@ class MainActivity :
}
},
)
.onNoConnectionListener {
val contextView = findViewById<View>(R.id.navigationBar)
Snackbar.make(
contextView,
R.string.sync_offline_check_connection,
Snackbar.LENGTH_SHORT,
).show()
}
.show("ALL_SYNC")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -220,7 +221,16 @@ class ProgramFragment : FragmentGlobalAbstract(), ProgramView {
}
}
},
).show(FRAGMENT_TAG)
)
.onNoConnectionListener {
val contextView = activity?.findViewById<View>(R.id.navigationBar)
Snackbar.make(
contextView!!,
R.string.sync_offline_check_connection,
Snackbar.LENGTH_SHORT,
).show()
}
.show(FRAGMENT_TAG)
}

fun sharedView() = binding.drawerLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -195,7 +196,16 @@ class ProgramEventDetailActivity :
override fun onDismiss(hasChanged: Boolean) {
if (hasChanged) FilterManager.getInstance().publishData()
}
}).show("EVENT_SYNC")
})
.onNoConnectionListener {
val contextView = findViewById<View>(R.id.navigationBar)
Snackbar.make(
contextView,
R.string.sync_offline_check_connection,
Snackbar.LENGTH_SHORT,
).show()
}
.show("EVENT_SYNC")
}

public override fun onPause() {
Expand Down Expand Up @@ -376,7 +386,16 @@ class ProgramEventDetailActivity :
override fun onDismiss(hasChanged: Boolean) {
if (hasChanged) FilterManager.getInstance().publishData()
}
}).show(FRAGMENT_TAG)
})
.onNoConnectionListener {
val contextView = findViewById<View>(R.id.rootView)
Snackbar.make(
contextView,
R.string.sync_offline_check_connection,
Snackbar.LENGTH_SHORT,
).show()
}
.show(FRAGMENT_TAG)
}

private fun showList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,23 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
}

private void openSyncDialog() {
View contextView = findViewById(R.id.navigationBar);
new SyncStatusDialog.Builder()
.withContext(this, null)
.withSyncContext(
new SyncContext.TrackerProgram(initialProgram)
)
.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
Expand Down Expand Up @@ -582,14 +591,22 @@ public void setPrograms(List<ProgramSpinnerModel> programs) {

@Override
public void showSyncDialog(String enrollmentUid) {
View contextView = findViewById(R.id.navigationBar);
new SyncStatusDialog.Builder()
.withContext(this, null)
.withSyncContext(
new SyncContext.TrackerProgramTei(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<ProgramSpinnerModel> programs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -302,7 +303,15 @@ class TeiDashboardMobileActivity :
finish()
}
}
}).show(TEI_SYNC)
})
.onNoConnectionListener {
val contextView = findViewById<View>(R.id.navigationBar)
Snackbar.make(
contextView,
R.string.sync_offline_check_connection,
Snackbar.LENGTH_SHORT,
).show()
}.show(TEI_SYNC)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -619,7 +620,15 @@ class TEIDataFragment : FragmentGlobalAbstract(), TEIDataContracts.View {
override fun onDismiss(hasChanged: Boolean) {
if (hasChanged) FilterManager.getInstance().publishData()
}
}).show(enrollmentUid)
})
.onNoConnectionListener {
val contextView = activity?.findViewById<View>(R.id.navigationBar)
Snackbar.make(
contextView!!,
R.string.sync_offline_check_connection,
Snackbar.LENGTH_SHORT,
).show()
}.show(enrollmentUid)
}

override fun displayCatComboOptionSelectorForEvents(data: List<EventViewModel>) {
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/org/dhis2/utils/granularsync/SyncStatusDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,13 +14,15 @@ class SyncStatusDialogProvider : SyncComponentProvider {
syncContext: SyncContext,
dismissListener: OnDismissListener?,
onSyncNavigationListener: OnSyncNavigationListener?,
onNoConnectionListener: OnNoConnectionListener?,
) {
val syncBuilder = SyncStatusDialog.Builder()
.withContext(activity, onSyncNavigationListener)
.withSyncContext(syncContext)

with(syncBuilder) {
dismissListener?.let { onDismissListener(it) }
onNoConnectionListener?.let { onNoConnectionListener(it) }
}
syncBuilder
.show(syncContext.conflictType().name)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@
<string name="remove">Remove</string>
<string name="coordinates">Coordinates</string>
<string name="no_data_access">You don’t have access to data.\nContact your administrator.</string>
<string name="sync_offline_check_connection">Looks like you\'re offline. Please check you connection</string>
<string name="delete_tei_dialog_title">Delete this %s?</string>
<string name="delete_tei_dialog_message">This %s and all its data across all programs will be deleted. This action cannot be undone.</string>
<string name="remove_enrollment_dialog_title">Remove from %s?</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.dhis2.commons.sync

fun interface OnNoConnectionListener {
fun onNoConnection()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ interface SyncComponentProvider {
syncContext: SyncContext,
dismissListener: OnDismissListener? = null,
onSyncNavigationListener: OnSyncNavigationListener? = null,
onNoConnectionListener: OnNoConnectionListener? = null,
)
}
Loading
Loading