forked from mozilla-mobile/fenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#7094 - Adds save login exceptions
- Loading branch information
Showing
36 changed files
with
772 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
app/src/main/java/org/mozilla/fenix/loginexceptions/ExceptionsFragmentStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* 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/. */ | ||
|
||
package org.mozilla.fenix.loginexceptions | ||
|
||
import mozilla.components.feature.logins.exceptions.LoginException | ||
import mozilla.components.lib.state.Action | ||
import mozilla.components.lib.state.State | ||
import mozilla.components.lib.state.Store | ||
|
||
/** | ||
* The [Store] for holding the [ExceptionsFragmentState] and applying [ExceptionsFragmentAction]s. | ||
*/ | ||
class ExceptionsFragmentStore(initialState: ExceptionsFragmentState) : | ||
Store<ExceptionsFragmentState, ExceptionsFragmentAction>(initialState, ::exceptionsStateReducer) | ||
|
||
/** | ||
* Actions to dispatch through the `ExceptionsStore` to modify `ExceptionsState` through the reducer. | ||
*/ | ||
sealed class ExceptionsFragmentAction : Action { | ||
data class Change(val list: List<LoginException>) : ExceptionsFragmentAction() | ||
} | ||
|
||
/** | ||
* The state for the Exceptions Screen | ||
* @property items List of exceptions to display | ||
*/ | ||
data class ExceptionsFragmentState(val items: List<LoginException>) : State | ||
|
||
/** | ||
* The ExceptionsState Reducer. | ||
*/ | ||
private fun exceptionsStateReducer( | ||
state: ExceptionsFragmentState, | ||
action: ExceptionsFragmentAction | ||
): ExceptionsFragmentState { | ||
return when (action) { | ||
is ExceptionsFragmentAction.Change -> state.copy(items = action.list) | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
app/src/main/java/org/mozilla/fenix/loginexceptions/LoginExceptionsAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* 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/. */ | ||
|
||
package org.mozilla.fenix.loginexceptions | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import mozilla.components.feature.logins.exceptions.LoginException | ||
import org.mozilla.fenix.loginexceptions.viewholders.LoginExceptionsDeleteButtonViewHolder | ||
import org.mozilla.fenix.loginexceptions.viewholders.LoginExceptionsHeaderViewHolder | ||
import org.mozilla.fenix.loginexceptions.viewholders.LoginExceptionsListItemViewHolder | ||
|
||
sealed class AdapterItem { | ||
object DeleteButton : AdapterItem() | ||
object Header : AdapterItem() | ||
data class Item(val item: LoginException) : AdapterItem() | ||
} | ||
|
||
/** | ||
* Adapter for a list of sites that are exempted from saving logins, | ||
* along with controls to remove the exception. | ||
*/ | ||
class LoginExceptionsAdapter( | ||
private val interactor: LoginExceptionsInteractor | ||
) : ListAdapter<AdapterItem, RecyclerView.ViewHolder>(DiffCallback) { | ||
|
||
/** | ||
* Change the list of items that are displayed. | ||
* Header and footer items are added to the list as well. | ||
*/ | ||
fun updateData(exceptions: List<LoginException>) { | ||
val adapterItems = mutableListOf<AdapterItem>() | ||
adapterItems.add(AdapterItem.Header) | ||
exceptions.mapTo(adapterItems) { AdapterItem.Item(it) } | ||
adapterItems.add(AdapterItem.DeleteButton) | ||
submitList(adapterItems) | ||
} | ||
|
||
override fun getItemViewType(position: Int) = when (getItem(position)) { | ||
AdapterItem.DeleteButton -> LoginExceptionsDeleteButtonViewHolder.LAYOUT_ID | ||
AdapterItem.Header -> LoginExceptionsHeaderViewHolder.LAYOUT_ID | ||
is AdapterItem.Item -> LoginExceptionsListItemViewHolder.LAYOUT_ID | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | ||
val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false) | ||
|
||
return when (viewType) { | ||
LoginExceptionsDeleteButtonViewHolder.LAYOUT_ID -> LoginExceptionsDeleteButtonViewHolder( | ||
view, | ||
interactor | ||
) | ||
LoginExceptionsHeaderViewHolder.LAYOUT_ID -> LoginExceptionsHeaderViewHolder(view) | ||
LoginExceptionsListItemViewHolder.LAYOUT_ID -> LoginExceptionsListItemViewHolder( | ||
view, | ||
interactor | ||
) | ||
else -> throw IllegalStateException() | ||
} | ||
} | ||
|
||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | ||
if (holder is LoginExceptionsListItemViewHolder) { | ||
val adapterItem = getItem(position) as AdapterItem.Item | ||
holder.bind(adapterItem.item) | ||
} | ||
} | ||
|
||
private object DiffCallback : DiffUtil.ItemCallback<AdapterItem>() { | ||
override fun areItemsTheSame(oldItem: AdapterItem, newItem: AdapterItem) = | ||
areContentsTheSame(oldItem, newItem) | ||
|
||
@Suppress("DiffUtilEquals") | ||
override fun areContentsTheSame(oldItem: AdapterItem, newItem: AdapterItem) = | ||
oldItem == newItem | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
app/src/main/java/org/mozilla/fenix/loginexceptions/LoginExceptionsFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* 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/. */ | ||
|
||
package org.mozilla.fenix.loginexceptions | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.Observer | ||
import androidx.lifecycle.asLiveData | ||
import androidx.lifecycle.lifecycleScope | ||
import kotlinx.android.synthetic.main.fragment_exceptions.view.* | ||
import kotlinx.coroutines.Dispatchers.IO | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.launch | ||
import mozilla.components.feature.logins.exceptions.LoginException | ||
import mozilla.components.lib.state.ext.consumeFrom | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.components.StoreProvider | ||
import org.mozilla.fenix.ext.requireComponents | ||
import org.mozilla.fenix.ext.showToolbar | ||
|
||
/** | ||
* Displays a list of sites that are exempted from saving logins, | ||
* along with controls to remove the exception. | ||
*/ | ||
class LoginExceptionsFragment : Fragment() { | ||
private lateinit var exceptionsStore: ExceptionsFragmentStore | ||
private lateinit var exceptionsView: LoginExceptionsView | ||
private lateinit var exceptionsInteractor: LoginExceptionsInteractor | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
showToolbar(getString(R.string.preference_exceptions)) | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
val view = inflater.inflate(R.layout.fragment_exceptions, container, false) | ||
exceptionsStore = StoreProvider.get(this) { | ||
ExceptionsFragmentStore( | ||
ExceptionsFragmentState( | ||
items = listOf() | ||
) | ||
) | ||
} | ||
exceptionsInteractor = | ||
LoginExceptionsInteractor(::deleteOneItem, ::deleteAllItems) | ||
exceptionsView = LoginExceptionsView(view.exceptionsLayout, exceptionsInteractor) | ||
subscribeToLoginExceptions() | ||
return view | ||
} | ||
|
||
private fun subscribeToLoginExceptions(): Observer<List<LoginException>> { | ||
return Observer<List<LoginException>> { exceptions -> | ||
exceptionsStore.dispatch(ExceptionsFragmentAction.Change(exceptions)) | ||
}.also { observer -> | ||
requireComponents.core.loginExceptionStorage.getLoginExceptions().asLiveData() | ||
.observe(viewLifecycleOwner, observer) | ||
} | ||
} | ||
|
||
@ExperimentalCoroutinesApi | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
consumeFrom(exceptionsStore) { | ||
exceptionsView.update(it) | ||
} | ||
} | ||
|
||
private fun deleteAllItems() { | ||
viewLifecycleOwner.lifecycleScope.launch(IO) { | ||
requireComponents.core.loginExceptionStorage.deleteAllLoginExceptions() | ||
} | ||
} | ||
|
||
private fun deleteOneItem(item: LoginException) { | ||
viewLifecycleOwner.lifecycleScope.launch(IO) { | ||
requireComponents.core.loginExceptionStorage.removeLoginException(item) | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/org/mozilla/fenix/loginexceptions/LoginExceptionsInteractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* 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/. */ | ||
|
||
package org.mozilla.fenix.loginexceptions | ||
|
||
import mozilla.components.feature.logins.exceptions.LoginException | ||
|
||
/** | ||
* Interactor for the exceptions screen | ||
* Provides implementations for the ExceptionsViewInteractor | ||
*/ | ||
class LoginExceptionsInteractor( | ||
private val deleteOne: (LoginException) -> Unit, | ||
private val deleteAll: () -> Unit | ||
) : ExceptionsViewInteractor { | ||
override fun onDeleteAll() { | ||
deleteAll.invoke() | ||
} | ||
|
||
override fun onDeleteOne(item: LoginException) { | ||
deleteOne.invoke(item) | ||
} | ||
} |
Oops, something went wrong.