2019-05-07 22:28:38 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2019-07-12 20:38:15 +02:00
|
|
|
package org.mozilla.fenix.exceptions
|
|
|
|
|
2019-05-07 22:28:38 +02:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import androidx.fragment.app.Fragment
|
2019-06-13 02:14:46 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2019-07-19 01:07:48 +02:00
|
|
|
import androidx.lifecycle.whenStarted
|
2019-05-07 22:28:38 +02:00
|
|
|
import androidx.navigation.Navigation
|
|
|
|
import kotlinx.android.synthetic.main.fragment_exceptions.view.*
|
2019-06-13 02:14:46 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2019-05-07 22:28:38 +02:00
|
|
|
import kotlinx.coroutines.coroutineScope
|
|
|
|
import kotlinx.coroutines.launch
|
2019-07-19 01:07:48 +02:00
|
|
|
import mozilla.components.lib.state.ext.observe
|
2019-07-19 00:15:24 +02:00
|
|
|
import org.mozilla.fenix.BrowserDirection
|
|
|
|
import org.mozilla.fenix.HomeActivity
|
2019-05-07 22:28:38 +02:00
|
|
|
import org.mozilla.fenix.R
|
2019-07-19 01:07:48 +02:00
|
|
|
import org.mozilla.fenix.components.StoreProvider
|
2019-07-19 00:15:24 +02:00
|
|
|
import org.mozilla.fenix.settings.SupportUtils
|
2019-05-07 22:28:38 +02:00
|
|
|
|
2019-06-13 02:14:46 +02:00
|
|
|
class ExceptionsFragment : Fragment() {
|
2019-07-19 01:07:48 +02:00
|
|
|
private lateinit var exceptionsStore: ExceptionsStore
|
|
|
|
private lateinit var exceptionsView: ExceptionsView
|
|
|
|
private lateinit var exceptionsInteractor: ExceptionsInteractor
|
2019-05-07 22:28:38 +02:00
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
activity?.title = getString(R.string.preference_exceptions)
|
|
|
|
(activity as AppCompatActivity).supportActionBar?.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
|
|
|
val view = inflater.inflate(R.layout.fragment_exceptions, container, false)
|
2019-07-19 01:07:48 +02:00
|
|
|
exceptionsStore = StoreProvider.get(this) {
|
|
|
|
ExceptionsStore(
|
|
|
|
ExceptionsState(
|
|
|
|
items = loadAndMapExceptions()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
exceptionsInteractor =
|
|
|
|
ExceptionsInteractor(::openLearnMore, ::deleteOneItem, ::deleteAllItems)
|
|
|
|
exceptionsView = ExceptionsView(view.exceptions_layout, exceptionsInteractor)
|
2019-05-07 22:28:38 +02:00
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
2019-07-19 01:07:48 +02:00
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
exceptionsStore.observe(view) {
|
|
|
|
viewLifecycleOwner.lifecycleScope.launch {
|
|
|
|
whenStarted {
|
|
|
|
exceptionsView.update(it)
|
2019-05-07 22:28:38 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-19 01:07:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun deleteAllItems() {
|
|
|
|
viewLifecycleOwner.lifecycleScope.launch(IO) {
|
|
|
|
val domains = ExceptionDomains.load(context!!)
|
|
|
|
ExceptionDomains.remove(context!!, domains)
|
|
|
|
launch(Main) {
|
|
|
|
view?.let { view -> Navigation.findNavController(view).navigateUp() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun deleteOneItem(item: ExceptionsItem) {
|
|
|
|
viewLifecycleOwner.lifecycleScope.launch(IO) {
|
|
|
|
ExceptionDomains.remove(context!!, listOf(item.url))
|
|
|
|
reloadData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun openLearnMore() {
|
|
|
|
(activity as HomeActivity).openToBrowserAndLoad(
|
|
|
|
searchTermOrURL = SupportUtils.getGenericSumoURLForTopic
|
|
|
|
(SupportUtils.SumoTopic.TRACKING_PROTECTION),
|
|
|
|
newTab = true,
|
|
|
|
from = BrowserDirection.FromExceptions
|
|
|
|
)
|
2019-05-07 22:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadAndMapExceptions(): List<ExceptionsItem> {
|
|
|
|
return ExceptionDomains.load(context!!)
|
|
|
|
.map { item ->
|
|
|
|
ExceptionsItem(
|
|
|
|
item
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private suspend fun reloadData() {
|
|
|
|
val items = loadAndMapExceptions()
|
|
|
|
|
|
|
|
coroutineScope {
|
2019-06-13 02:14:46 +02:00
|
|
|
launch(Main) {
|
2019-05-07 22:28:38 +02:00
|
|
|
if (items.isEmpty()) {
|
|
|
|
view?.let { view: View -> Navigation.findNavController(view).navigateUp() }
|
|
|
|
return@launch
|
|
|
|
}
|
2019-07-19 01:07:48 +02:00
|
|
|
exceptionsStore.dispatch(ExceptionsAction.Change(items))
|
2019-05-07 22:28:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|