2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-02 03:40:16 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/10473: Fetch the addon from addon manager instead of the store

This commit is contained in:
Simon Chae 2020-05-08 14:37:51 -04:00 committed by liuche
parent 1260014bb3
commit e2adba7ca7
2 changed files with 43 additions and 29 deletions

View File

@ -11,17 +11,15 @@ import android.view.ViewGroup
import android.widget.Switch import android.widget.Switch
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation import androidx.navigation.Navigation
import androidx.navigation.findNavController import androidx.navigation.findNavController
import kotlinx.android.synthetic.main.fragment_installed_add_on_details.view.* import kotlinx.android.synthetic.main.fragment_installed_add_on_details.view.*
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import mozilla.components.feature.addons.Addon import mozilla.components.feature.addons.Addon
import mozilla.components.feature.addons.AddonManagerException
import mozilla.components.feature.addons.ui.translatedName import mozilla.components.feature.addons.ui.translatedName
import mozilla.components.lib.state.ext.flowScoped
import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifChanged
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.showToolbar import org.mozilla.fenix.ext.showToolbar
@ -33,7 +31,6 @@ import org.mozilla.fenix.utils.Settings
@Suppress("LargeClass", "TooManyFunctions") @Suppress("LargeClass", "TooManyFunctions")
class InstalledAddonDetailsFragment : Fragment() { class InstalledAddonDetailsFragment : Fragment() {
private lateinit var addon: Addon private lateinit var addon: Addon
private var scope: CoroutineScope? = null
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -44,36 +41,46 @@ class InstalledAddonDetailsFragment : Fragment() {
addon = AddonDetailsFragmentArgs.fromBundle(requireNotNull(arguments)).addon addon = AddonDetailsFragmentArgs.fromBundle(requireNotNull(arguments)).addon
} }
return inflater.inflate(R.layout.fragment_installed_add_on_details, container, false).also { return inflater.inflate(R.layout.fragment_installed_add_on_details, container, false)
bind(it)
}
} }
override fun onStart() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onStart() super.onViewCreated(view, savedInstanceState)
scope = requireContext().components.core.store.flowScoped { flow -> bindAddon(view)
flow.ifChanged { it.extensions } }
.map { it.extensions.filterValues { extension -> extension.id == addon.id } }
.ifChanged() private fun bindAddon(view: View) {
.collect { lifecycleScope.launch(Dispatchers.IO) {
val addonState = it[addon.id] try {
if (addonState != null && addonState.enabled != addon.isEnabled()) { val addons = requireContext().components.addonManager.getAddons()
view?.let { view -> lifecycleScope.launch(Dispatchers.Main) {
val newState = addon.installedState?.copy(enabled = addonState.enabled) runIfFragmentIsAttached {
this.addon = addon.copy(installedState = newState) addons.find { addon.id == it.id }.let {
view.enable_switch.setState(addon.isEnabled()) if (it == null) {
throw AddonManagerException(Exception("Addon ${addon.id} not found"))
} else {
addon = it
bindUI(view)
}
view.add_on_progress_bar.isVisible = false
}
}
}
} catch (e: AddonManagerException) {
lifecycleScope.launch(Dispatchers.Main) {
runIfFragmentIsAttached {
showSnackBar(
view,
getString(R.string.mozac_feature_addons_failed_to_query_add_ons)
)
view.add_on_progress_bar.isVisible = false
} }
} }
} }
} }
} }
override fun onStop() { private fun bindUI(view: View) {
super.onStop()
scope?.cancel()
}
private fun bind(view: View) {
val title = addon.translatedName val title = addon.translatedName
showToolbar(title) showToolbar(title)

View File

@ -99,4 +99,11 @@
android:text="@string/mozac_feature_addons_remove" /> android:text="@string/mozac_feature_addons_remove" />
</RelativeLayout> </RelativeLayout>
</ScrollView> </ScrollView>
<ProgressBar
android:id="@+id/add_on_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout> </FrameLayout>