Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
Closes #3921 - Show Snackbar after page load after toggling block toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
ekager authored and boek committed Jan 10, 2019
1 parent 5915d58 commit ecb43e4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
33 changes: 33 additions & 0 deletions app/src/main/java/org/mozilla/focus/fragment/BrowserFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.preference.PreferenceManager
import android.support.annotation.RequiresApi
import android.support.design.widget.AppBarLayout
import android.support.design.widget.CoordinatorLayout
import android.support.design.widget.Snackbar
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
Expand Down Expand Up @@ -150,6 +151,8 @@ class BrowserFragment : WebFragment(), LifecycleObserver, View.OnClickListener,
private var findInPagePrevious: ImageButton? = null
private var closeFindInPage: ImageButton? = null

private var showContentBlockingSnackbar = false

private var fullscreenCallback: IWebView.FullscreenCallback? = null

private var manager: DownloadManager? = null
Expand Down Expand Up @@ -1237,6 +1240,11 @@ class BrowserFragment : WebFragment(), LifecycleObserver, View.OnClickListener,

fun reload() = getWebView()?.reload()

fun reloadWithNewBlockingState() {
reload()
showContentBlockingSnackbar = true
}

fun setBlockingUI(enabled: Boolean) {
val webView = getWebView()
webView?.setBlockingEnabled(enabled)
Expand Down Expand Up @@ -1430,6 +1438,8 @@ class BrowserFragment : WebFragment(), LifecycleObserver, View.OnClickListener,
swipeRefresh!!.isRefreshing = false

updateSecurityIcon(session)

showContentBlockingSnackbarIfChanged()
}

updateBlockingBadging(loading || session.trackerBlockingEnabled)
Expand Down Expand Up @@ -1485,6 +1495,29 @@ class BrowserFragment : WebFragment(), LifecycleObserver, View.OnClickListener,
showCrashReporter(crash)
}

private fun showContentBlockingSnackbarIfChanged() {
if (showContentBlockingSnackbar) {
// Show Snackbar to inform users and allow them to undo their choice
val snackbar = Snackbar.make(
activity!!.findViewById(android.R.id.content),
if (!session.trackerBlockingEnabled)
R.string.content_blocking_disabled_snackbar_message else
R.string.content_blocking_enabled_snackbar_message,
Snackbar.LENGTH_SHORT
)
snackbar.setAction(
if (!session.trackerBlockingEnabled)
R.string.content_blocking_snackbar_enable_action else
R.string.content_blocking_snackbar_disable_action
) {
val menu = menuWeakReference!!.get()
menu?.updateBlocking(!session.trackerBlockingEnabled)
}
snackbar.show()
showContentBlockingSnackbar = false
}
}

companion object {
const val FRAGMENT_TAG = "browser"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ internal class BlockingItemViewHolder(itemView: View, private val fragment: Brow
ThreadUtils.postToMainThread(Runnable { view.setText(R.string.content_blocking_disabled) })
}

override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
fragment.setBlockingUI(isChecked)
fun addOrRemoveURL(isBlockingEnabled: Boolean) {
fragment.setBlockingUI(isBlockingEnabled)

val url = fragment.url
val host = try {
Expand All @@ -67,23 +67,26 @@ internal class BlockingItemViewHolder(itemView: View, private val fragment: Brow
url
} ?: url

if (!isChecked) {
if (!isBlockingEnabled) {
addUrlToExceptionsList(host = host)
} else {
removeUrlFromExceptionsList(host = host)
}

TelemetryWrapper.blockingSwitchEvent(isChecked)
TelemetryWrapper.blockingSwitchEvent(isBlockingEnabled)

// Delay closing the menu and reloading the website a bit so that the user can actually see
// the switch change its state.
ThreadUtils.postToMainThreadDelayed(Runnable {
menu.dismiss()

fragment.reload()
fragment.reloadWithNewBlockingState()
}, Switch_THUMB_ANIMATION_DURATION)
}

override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
addOrRemoveURL(isChecked)
}

private fun addUrlToExceptionsList(host: String) {
fragment.launch(IO) {
val duplicateURL = ExceptionDomains.load(fragment.requireContext()).contains(host)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public void updateLoading(boolean loading) {
adapter.updateLoading(loading);
}

public void updateBlocking(boolean isBlockingEnabled) {
adapter.updateBlocking(isBlockingEnabled);
}

public void show(View anchor) {
final int xOffset = ViewUtils.INSTANCE.isRTL(anchor) ? -anchor.getWidth() : 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ class BrowserMenuAdapter(
navigationItemViewHolder.updateTrackers(trackers)
}

fun updateBlocking(isBlockingEnabled: Boolean) {
val navigationItemViewHolder = blockingItemViewHolderReference.get() ?: return
navigationItemViewHolder.addOrRemoveURL(isBlockingEnabled)
}

fun updateLoading(loading: Boolean) {
val navigationItemViewHolder = navigationItemViewHolderReference.get() ?: return
navigationItemViewHolder.updateLoading(loading)
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,14 @@ be temporary, and you can try again later.</li>
<!-- Label for the checkbox to send the tab crash report to Mozilla -->
<string name="crash_report_send_crash_label">Send crash report to Mozilla</string>

<!-- Pop-up message to inform users they have enabled content blocking -->
<string name="content_blocking_enabled_snackbar_message">Content Blocking is enabled</string>
<!-- Pop-up message to inform users they have disabled content blocking -->
<string name="content_blocking_disabled_snackbar_message">Content Blocking is disabled</string>
<!-- Snackbar action button that users can press to enable content blocking -->
<string name="content_blocking_snackbar_enable_action">Enable</string>
<!-- Snackbar action button that users can press to disable content blocking -->
<string name="content_blocking_snackbar_disable_action">Disable</string>

<!-- Deprecated strings -->
<string name="shortcut_erase_and_open_long_label">Erase and open %1$s</string>
Expand Down

0 comments on commit ecb43e4

Please sign in to comment.