Skip to content

Commit

Permalink
Merge pull request #5455 from grzesiek2010/COLLECT-5454
Browse files Browse the repository at this point in the history
Support displaying snackbars above a particular view
  • Loading branch information
grzesiek2010 authored Feb 27, 2023
2 parents 3effb27 + ee84901 commit 28c77c6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2018 Shobhit
Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.odk.collect.androidshared.ui

import android.view.View
import android.widget.TextView
import com.google.android.material.R
import com.google.android.material.snackbar.Snackbar

/**
* Convenience wrapper around Android's [Snackbar] API.
*/
object SnackbarUtils {
@JvmStatic
@JvmOverloads
fun showShortSnackbar(parentView: View, message: String, anchorView: View? = null) {
showSnackbar(parentView, message, 3500, anchorView)
}

@JvmStatic
@JvmOverloads
fun showLongSnackbar(parentView: View, message: String, anchorView: View? = null) {
showSnackbar(parentView, message, 5500, anchorView)
}

/**
* Displays snackbar with {@param message} and multi-line message enabled.
*
* @param parentView The view to find a parent from.
* @param anchorView The view this snackbar should be anchored above.
* @param message The text to show. Can be formatted text.
*/
private fun showSnackbar(parentView: View, message: String, duration: Int, anchorView: View?) {
if (message.isBlank()) {
return
}

Snackbar.make(parentView, message.trim(), duration).apply {
val textView = this.view.findViewById<TextView>(R.id.snackbar_text)
textView.isSingleLine = false

if (anchorView?.visibility != View.GONE) {
this.anchorView = anchorView
}
}.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@
import org.odk.collect.android.utilities.InstancesRepositoryProvider;
import org.odk.collect.android.utilities.PlayServicesChecker;
import org.odk.collect.android.utilities.ScreenContext;
import org.odk.collect.android.utilities.SnackbarUtils;
import org.odk.collect.android.utilities.SoftKeyboardController;
import org.odk.collect.android.widgets.DateTimeWidget;
import org.odk.collect.android.widgets.QuestionWidget;
Expand All @@ -169,6 +168,7 @@
import org.odk.collect.androidshared.system.IntentLauncher;
import org.odk.collect.androidshared.ui.DialogFragmentUtils;
import org.odk.collect.androidshared.ui.FragmentFactoryBuilder;
import org.odk.collect.androidshared.ui.SnackbarUtils;
import org.odk.collect.androidshared.ui.ToastUtils;
import org.odk.collect.androidshared.ui.multiclicksafe.MultiClickGuard;
import org.odk.collect.async.Scheduler;
Expand Down Expand Up @@ -2487,7 +2487,7 @@ private void displayUIFor(@Nullable BackgroundLocationManager.BackgroundLocation
snackBarText = getString(backgroundLocationMessage.getMessageTextResourceId());
}

SnackbarUtils.showLongSnackbar(findViewById(R.id.llParent), snackBarText);
SnackbarUtils.showLongSnackbar(findViewById(R.id.llParent), snackBarText, findViewById(R.id.buttonholder));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import org.odk.collect.android.activities.FormMapActivity
import org.odk.collect.android.injection.DaggerUtils
import org.odk.collect.android.preferences.dialogs.ServerAuthDialogFragment
import org.odk.collect.android.utilities.ApplicationConstants
import org.odk.collect.android.utilities.SnackbarUtils
import org.odk.collect.androidshared.network.NetworkStateProvider
import org.odk.collect.androidshared.ui.DialogFragmentUtils
import org.odk.collect.androidshared.ui.SnackbarUtils
import org.odk.collect.permissions.PermissionListener
import org.odk.collect.permissions.PermissionsProvider
import org.odk.collect.strings.localization.LocalizedActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import org.odk.collect.android.R;
import org.odk.collect.android.formlists.sorting.FormListSortingOption;
import org.odk.collect.android.utilities.SnackbarUtils;
import org.odk.collect.androidshared.ui.SnackbarUtils;

import java.util.Arrays;

Expand Down

This file was deleted.

0 comments on commit 28c77c6

Please sign in to comment.