Skip to content

Commit

Permalink
For mozilla-mobile#10398: Add AddonInstallationDialogFragment support
Browse files Browse the repository at this point in the history
  • Loading branch information
psymoon committed May 12, 2020
1 parent 9a30d57 commit 9d8dd53
Showing 1 changed file with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import mozilla.components.feature.addons.AddonManagerException
import mozilla.components.feature.addons.ui.AddonsManagerAdapter
import mozilla.components.feature.addons.ui.AddonsManagerAdapterDelegate
import mozilla.components.feature.addons.ui.PermissionsDialogFragment
import mozilla.components.feature.addons.ui.AddonInstallationDialogFragment
import mozilla.components.feature.addons.ui.translatedName
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
Expand Down Expand Up @@ -167,6 +168,11 @@ class AddonsManagementFragment : Fragment(R.layout.fragment_add_ons_management),
return findPreviousDialogFragment() != null
}

private fun hasExistingAddonInstallationDialogFragment(): Boolean {
return parentFragmentManager.findFragmentByTag(INSTALLATION_DIALOG_FRAGMENT_TAG)
as? AddonInstallationDialogFragment != null
}

private fun showPermissionDialog(addon: Addon) {
if (!isInstallationInProgress && !hasExistingPermissionDialogFragment()) {
val dialog = PermissionsDialogFragment.newInstance(
Expand All @@ -190,6 +196,45 @@ class AddonsManagementFragment : Fragment(R.layout.fragment_add_ons_management),
}
}

private fun showInstallationDialog(addon: Addon) {
if (!isInstallationInProgress && !hasExistingAddonInstallationDialogFragment()) {
val addonCollectionProvider = requireContext().components.addonCollectionProvider

val dialog = AddonInstallationDialogFragment.newInstance(
addon = addon,
addonCollectionProvider = addonCollectionProvider,
promptsStyling = AddonInstallationDialogFragment.PromptsStyling(
gravity = Gravity.BOTTOM,
shouldWidthMatchParent = true,
confirmButtonBackgroundColor = ThemeManager.resolveAttribute(
R.attr.accent,
requireContext()
),
confirmButtonTextColor = ThemeManager.resolveAttribute(
R.attr.contrastText,
requireContext()
),
confirmButtonRadius = (resources.getDimensionPixelSize(R.dimen.tab_corner_radius)).toFloat()
),
onConfirmButtonClicked = { _, allowInPrivateBrowsing ->
if (allowInPrivateBrowsing) {
requireContext().components.addonManager.setAddonAllowedInPrivateBrowsing(
addon,
allowInPrivateBrowsing,
onSuccess = {
runIfFragmentIsAttached {
adapter?.updateAddon(it)
}
}
)
}
}
)

dialog.show(parentFragmentManager, INSTALLATION_DIALOG_FRAGMENT_TAG)
}
}

private val onPositiveButtonClicked: ((Addon) -> Unit) = { addon ->
addonProgressOverlay?.visibility = View.VISIBLE

Expand All @@ -202,18 +247,11 @@ class AddonsManagementFragment : Fragment(R.layout.fragment_add_ons_management),
requireContext().components.addonManager.installAddon(
addon,
onSuccess = {
this@AddonsManagementFragment.view?.let { view ->
val rootView = activity?.getRootView() ?: view
showSnackBar(
rootView,
getString(
R.string.mozac_feature_addons_successfully_installed,
it.translatedName
)
)
runIfFragmentIsAttached {
isInstallationInProgress = false
adapter?.updateAddon(it)
addonProgressOverlay?.visibility = View.GONE
isInstallationInProgress = false
showInstallationDialog(it)
}
},
onError = { _, _ ->
Expand Down Expand Up @@ -245,5 +283,6 @@ class AddonsManagementFragment : Fragment(R.layout.fragment_add_ons_management),

companion object {
private const val PERMISSIONS_DIALOG_FRAGMENT_TAG = "ADDONS_PERMISSIONS_DIALOG_FRAGMENT"
private const val INSTALLATION_DIALOG_FRAGMENT_TAG = "ADDONS_INSTALLATION_DIALOG_FRAGMENT"
}
}

0 comments on commit 9d8dd53

Please sign in to comment.