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
33 changed files
with
739 additions
and
143 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
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) | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
app/src/main/java/org/mozilla/fenix/loginexceptions/LoginExceptionsView.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,62 @@ | ||
/* 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 android.widget.FrameLayout | ||
import androidx.core.view.isVisible | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import kotlinx.android.extensions.LayoutContainer | ||
import kotlinx.android.synthetic.main.component_exceptions.view.* | ||
import mozilla.components.feature.logins.exceptions.LoginException | ||
import org.mozilla.fenix.R | ||
|
||
/** | ||
* Interface for the ExceptionsViewInteractor. This interface is implemented by objects that want | ||
* to respond to user interaction on the ExceptionsView | ||
*/ | ||
interface ExceptionsViewInteractor { | ||
/** | ||
* Called whenever all exception items are deleted | ||
*/ | ||
fun onDeleteAll() | ||
|
||
/** | ||
* Called whenever one exception item is deleted | ||
*/ | ||
fun onDeleteOne(item: LoginException) | ||
} | ||
|
||
/** | ||
* View that contains and configures the Exceptions List | ||
*/ | ||
class LoginExceptionsView( | ||
override val containerView: ViewGroup, | ||
val interactor: LoginExceptionsInteractor | ||
) : LayoutContainer { | ||
|
||
val view: FrameLayout = LayoutInflater.from(containerView.context) | ||
.inflate(R.layout.component_exceptions, containerView, true) | ||
.findViewById(R.id.exceptions_wrapper) | ||
|
||
private val exceptionsAdapter = LoginExceptionsAdapter(interactor) | ||
|
||
init { | ||
view.exceptions_learn_more.isVisible = false | ||
view.exceptions_empty_message.text = | ||
view.context.getString(R.string.preferences_passwords_exceptions_description_empty) | ||
view.exceptions_list.apply { | ||
adapter = exceptionsAdapter | ||
layoutManager = LinearLayoutManager(containerView.context) | ||
} | ||
} | ||
|
||
fun update(state: ExceptionsFragmentState) { | ||
view.exceptions_empty_view.isVisible = state.items.isEmpty() | ||
view.exceptions_list.isVisible = state.items.isNotEmpty() | ||
exceptionsAdapter.updateData(state.items) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...va/org/mozilla/fenix/loginexceptions/viewholders/LoginExceptionsDeleteButtonViewHolder.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,28 @@ | ||
/* 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.viewholders | ||
|
||
import android.view.View | ||
import androidx.recyclerview.widget.RecyclerView | ||
import kotlinx.android.synthetic.main.delete_exceptions_button.view.* | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.loginexceptions.LoginExceptionsInteractor | ||
|
||
class LoginExceptionsDeleteButtonViewHolder( | ||
view: View, | ||
private val interactor: LoginExceptionsInteractor | ||
) : RecyclerView.ViewHolder(view) { | ||
private val deleteButton = view.removeAllExceptions | ||
|
||
init { | ||
deleteButton.setOnClickListener { | ||
interactor.onDeleteAll() | ||
} | ||
} | ||
|
||
companion object { | ||
const val LAYOUT_ID = R.layout.delete_logins_exceptions_button | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ain/java/org/mozilla/fenix/loginexceptions/viewholders/LoginExceptionsHeaderViewHolder.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,23 @@ | ||
/* 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.viewholders | ||
|
||
import android.view.View | ||
import androidx.recyclerview.widget.RecyclerView | ||
import kotlinx.android.synthetic.main.exceptions_description.view.* | ||
import org.mozilla.fenix.R | ||
|
||
class LoginExceptionsHeaderViewHolder( | ||
view: View | ||
) : RecyclerView.ViewHolder(view) { | ||
companion object { | ||
const val LAYOUT_ID = R.layout.exceptions_description | ||
} | ||
|
||
init { | ||
view.exceptions_description.text = | ||
view.context.getString(R.string.preferences_passwords_exceptions_description) | ||
} | ||
} |
Oops, something went wrong.