Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Siplify NavigationAwareWidget interface
  • Loading branch information
grzesiek2010 committed Feb 27, 2026
commit 8dacc0dba2d0998ef8969f4253b0fb98940247bc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.odk.collect.androidshared.ui

import android.content.Context
import androidx.annotation.StringRes
import com.google.android.material.dialog.MaterialAlertDialogBuilder

object DialogUtils {
@JvmStatic
fun show(
context: Context,
@StringRes titleRes: Int,
@StringRes messageRes: Int,
) {
MaterialAlertDialogBuilder(context)
.setTitle(titleRes)
.setMessage(messageRes)
.setPositiveButton(org.odk.collect.strings.R.string.ok, null)
.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
import org.odk.collect.androidshared.system.ProcessRestoreDetector;
import org.odk.collect.androidshared.ui.DialogFragmentUtils;
import org.odk.collect.androidshared.ui.FragmentFactoryBuilder;
import org.odk.collect.androidshared.ui.DialogUtils;
import org.odk.collect.androidshared.ui.SnackbarUtils;
import org.odk.collect.androidshared.ui.ToastUtils;
import org.odk.collect.async.Scheduler;
Expand All @@ -199,6 +200,7 @@
import org.odk.collect.settings.SettingsProvider;
import org.odk.collect.settings.keys.ProjectKeys;
import org.odk.collect.strings.localization.LocalizedActivity;
import org.odk.collect.timedgrid.NavigationWarning;

import java.io.File;
import java.util.HashMap;
Expand Down Expand Up @@ -388,8 +390,9 @@ public void allowSwiping(boolean doSwipe) {
private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
if (odkView != null && odkView.isNavigationBlocked()) {
DialogFragmentUtils.showIfNotShowing(odkView.getFirstNavigationBlockedWarningDialog().get(), getSupportFragmentManager());
NavigationWarning navigationWarning = odkView != null ? odkView.isNavigationBlocked() : null;
if (navigationWarning != null) {
DialogUtils.show(FormFillingActivity.this, navigationWarning.getTitleRes(), navigationWarning.getMessageRes());
} else if (audioRecorder.isRecording() && !backgroundAudioViewModel.isBackgroundRecording()) {
// We want the user to stop recording before changing screens
DialogFragmentUtils.showIfNotShowing(RecordingWarningDialogFragment.class, getSupportFragmentManager());
Expand Down Expand Up @@ -500,8 +503,9 @@ public void save() {
}
},
() -> {
if (odkView != null && odkView.isNavigationBlocked()) {
DialogFragmentUtils.showIfNotShowing(odkView.getFirstNavigationBlockedWarningDialog().get(), getSupportFragmentManager());
NavigationWarning navigationWarning = odkView != null ? odkView.isNavigationBlocked() : null;
if (navigationWarning != null) {
DialogUtils.show(this, navigationWarning.getTitleRes(), navigationWarning.getMessageRes());
swipeHandler.setBeenSwiped(false);
return false;
}
Expand Down Expand Up @@ -1213,8 +1217,9 @@ private void moveScreen(Direction direction) {
return;
}

if (odkView != null && odkView.isNavigationBlocked()) {
DialogFragmentUtils.showIfNotShowing(odkView.getFirstNavigationBlockedWarningDialog().get(), getSupportFragmentManager());
NavigationWarning navigationWarning = odkView != null ? odkView.isNavigationBlocked() : null;
if (navigationWarning != null) {
DialogUtils.show(this, navigationWarning.getTitleRes(), navigationWarning.getMessageRes());
swipeHandler.setBeenSwiped(false);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.LifecycleOwner;

Expand Down Expand Up @@ -91,6 +90,7 @@
import org.odk.collect.permissions.PermissionsProvider;
import org.odk.collect.settings.SettingsProvider;
import org.odk.collect.timedgrid.NavigationAwareWidget;
import org.odk.collect.timedgrid.NavigationWarning;

import java.io.File;
import java.io.Serializable;
Expand All @@ -99,7 +99,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Objects;
import java.util.Set;

import javax.inject.Inject;
Expand Down Expand Up @@ -808,20 +808,14 @@ private void updateQuestions(FormEntryPrompt[] prompts) {
}
}

public boolean isNavigationBlocked() {
return getFirstNavigationBlockingWidget().isPresent();
}

public Optional<Class<? extends DialogFragment>> getFirstNavigationBlockedWarningDialog() {
return getFirstNavigationBlockingWidget().map(NavigationAwareWidget::getWarningDialog);
}

private Optional<NavigationAwareWidget> getFirstNavigationBlockingWidget() {
return widgets
.stream()
@Nullable
public NavigationWarning isNavigationBlocked() {
return widgets.stream()
.filter(widget -> widget instanceof NavigationAwareWidget)
.map(widget -> (NavigationAwareWidget) widget)
.filter(NavigationAwareWidget::shouldBlockNavigation)
.findFirst();
.map(NavigationAwareWidget::shouldBlockNavigation)
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.odk.collect.android.widgets

import android.annotation.SuppressLint
import android.content.Context
import androidx.fragment.app.DialogFragment
import org.javarosa.core.model.FormIndex
import org.javarosa.core.model.IFormElement
import org.javarosa.core.model.data.IAnswerData
Expand All @@ -14,7 +13,7 @@ import org.odk.collect.android.widgets.items.ItemsWidgetUtils
import org.odk.collect.timedgrid.FormAnswerRefresher
import org.odk.collect.timedgrid.FormControllerFacade
import org.odk.collect.timedgrid.NavigationAwareWidget
import org.odk.collect.timedgrid.OngoingAssessmentWarningDialogFragment
import org.odk.collect.timedgrid.NavigationWarning
import org.odk.collect.timedgrid.TimedGridWidgetDelegate

@SuppressLint("ViewConstructor")
Expand Down Expand Up @@ -79,7 +78,5 @@ class TimedGridWidget(
widgetDelegate.onDetachedFromWindow()
}

override fun shouldBlockNavigation(): Boolean = widgetDelegate.shouldBlockNavigation()

override fun getWarningDialog(): Class<out DialogFragment> = OngoingAssessmentWarningDialogFragment::class.java
override fun shouldBlockNavigation(): NavigationWarning? = widgetDelegate.shouldBlockNavigation()
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.odk.collect.timedgrid

import androidx.fragment.app.DialogFragment
import androidx.annotation.StringRes

interface NavigationAwareWidget {
/**
* Whenever the navigation should be stopped.
*/
fun shouldBlockNavigation(): Boolean

/**
* If navigation should be stopped (see #shouldBlockNavigation) this returns DialogFragment to present to user.
* Returns NavigationBlock if navigation should be blocked, or null if navigation is allowed.
*/
fun getWarningDialog(): Class<out DialogFragment>
fun shouldBlockNavigation(): NavigationWarning?
}

data class NavigationWarning(
@StringRes val titleRes: Int,
@StringRes val messageRes: Int
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,18 @@ class TimedGridWidgetDelegate(
)
}

fun shouldBlockNavigation(): Boolean =
state == TimedGridState.IN_PROGRESS ||
fun shouldBlockNavigation(): NavigationWarning? {
val shouldNavigationBeBlocked = state == TimedGridState.IN_PROGRESS ||
state == TimedGridState.PAUSED ||
state == TimedGridState.COMPLETED_NO_LAST_ITEM

return if (shouldNavigationBeBlocked) {
NavigationWarning(
titleRes = R.string.assessment,
messageRes = R.string.assessment_warning
)
} else {
null
}
}
}