diff --git a/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationComponent.kt b/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationComponent.kt index d970f238d..043969d4a 100644 --- a/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationComponent.kt +++ b/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationComponent.kt @@ -19,13 +19,29 @@ data class Tab( val title: String ) -data class CollectionCreationState(val tabs: List = listOf(), val selectedTabs: Set = setOf()) : ViewState +data class Collection( + val collectionId: String, + val title: String +) + +sealed class SaveCollectionStep { + object SelectTabs : SaveCollectionStep() + object SelectCollection : SaveCollectionStep() + object NameCollection : SaveCollectionStep() +} + +data class CollectionCreationState( + val tabs: List = listOf(), + val selectedTabs: Set = setOf(), + val saveCollectionStep: SaveCollectionStep = SaveCollectionStep.SelectTabs +) : ViewState sealed class CollectionCreationChange : Change { data class TabListChange(val tabs: List) : CollectionCreationChange() object AddAllTabs : CollectionCreationChange() data class TabAdded(val tab: Tab) : CollectionCreationChange() data class TabRemoved(val tab: Tab) : CollectionCreationChange() + data class StepChanged(val saveCollectionStep: SaveCollectionStep) : CollectionCreationChange() } sealed class CollectionCreationAction : Action { @@ -34,6 +50,14 @@ sealed class CollectionCreationAction : Action { data class AddTabToSelection(val tab: Tab) : CollectionCreationAction() data class RemoveTabFromSelection(val tab: Tab) : CollectionCreationAction() data class SaveTabsToCollection(val tabs: List) : CollectionCreationAction() + data class BackPressed(val backPressFrom: SaveCollectionStep) : CollectionCreationAction() + data class SaveCollectionName(val tabs: List, val name: String) : + CollectionCreationAction() + + data class SelectCollection(val collection: Collection) : + CollectionCreationAction() + + data class AddNewCollection(val tabs: List) : CollectionCreationAction() } class CollectionCreationComponent( @@ -44,20 +68,24 @@ class CollectionCreationComponent( bus.getManagedEmitter(CollectionCreationAction::class.java), bus.getSafeManagedObservable(CollectionCreationChange::class.java) ) { - override val reducer: Reducer = { state, change -> - when (change) { - is CollectionCreationChange.AddAllTabs -> state.copy(selectedTabs = state.tabs.toSet()) - is CollectionCreationChange.TabListChange -> state.copy(tabs = change.tabs) - is CollectionCreationChange.TabAdded -> { - val selectedTabs = state.selectedTabs + setOf(change.tab) - state.copy(selectedTabs = selectedTabs) - } - is CollectionCreationChange.TabRemoved -> { - val selectedTabs = state.selectedTabs - setOf(change.tab) - state.copy(selectedTabs = selectedTabs) + override val reducer: Reducer = + { state, change -> + when (change) { + is CollectionCreationChange.AddAllTabs -> state.copy(selectedTabs = state.tabs.toSet()) + is CollectionCreationChange.TabListChange -> state.copy(tabs = change.tabs) + is CollectionCreationChange.TabAdded -> { + val selectedTabs = state.selectedTabs + setOf(change.tab) + state.copy(selectedTabs = selectedTabs) + } + is CollectionCreationChange.TabRemoved -> { + val selectedTabs = state.selectedTabs - setOf(change.tab) + state.copy(selectedTabs = selectedTabs) + } + is CollectionCreationChange.StepChanged -> { + state.copy(saveCollectionStep = change.saveCollectionStep) + } } } - } override fun initView() = CollectionCreationUIView(container, actionEmitter, changesObservable) diff --git a/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationUIView.kt b/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationUIView.kt index 430583130..470bd042c 100644 --- a/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationUIView.kt +++ b/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationUIView.kt @@ -4,15 +4,22 @@ package org.mozilla.fenix.collections 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/. */ +import android.content.Context.INPUT_METHOD_SERVICE +import android.view.KeyEvent import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup +import android.view.inputmethod.EditorInfo +import android.view.inputmethod.InputMethodManager +import android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView -import org.mozilla.fenix.R -import io.reactivex.Observer import io.reactivex.Observable +import io.reactivex.Observer import io.reactivex.functions.Consumer +import kotlinx.android.synthetic.main.component_collection_creation.* import kotlinx.android.synthetic.main.component_collection_creation.view.* +import org.mozilla.fenix.R import org.mozilla.fenix.ext.increaseTapArea import org.mozilla.fenix.mvi.UIView @@ -28,14 +35,14 @@ class CollectionCreationUIView( override val view = LayoutInflater.from(container.context) .inflate(R.layout.component_collection_creation, container, true) + var step: SaveCollectionStep = SaveCollectionStep.SelectTabs + private set + private val collectionCreationTabListAdapter = CollectionCreationTabListAdapter(actionEmitter) + private val collectionSaveListAdapter = SaveCollectionListAdapter(actionEmitter) private var selectedTabs: Set = setOf() init { - view.back_button.setOnClickListener { - actionEmitter.onNext(CollectionCreationAction.Close) - } - view.select_all_button.setOnClickListener { actionEmitter.onNext(CollectionCreationAction.SelectAllTapped) } @@ -47,31 +54,127 @@ class CollectionCreationUIView( } } + name_collection_edittext.setOnEditorActionListener { v, actionId, event -> + if (actionId == EditorInfo.IME_ACTION_DONE) { + actionEmitter.onNext( + CollectionCreationAction.SaveCollectionName( + selectedTabs.toList(), + v.text.toString() + ) + ) + true + } + false + } + view.add_tabs_button.setOnClickListener { actionEmitter.onNext(CollectionCreationAction.SaveTabsToCollection(selectedTabs.toList())) } + view.add_collection_button.setOnClickListener { + actionEmitter.onNext(CollectionCreationAction.AddNewCollection(selectedTabs.toList())) + } + view.tab_list.run { adapter = collectionCreationTabListAdapter layoutManager = LinearLayoutManager(container.context, RecyclerView.VERTICAL, true) } + + view.collections_list.run { + adapter = collectionSaveListAdapter + layoutManager = LinearLayoutManager(container.context, RecyclerView.VERTICAL, true) + } } override fun updateView() = Consumer { - this.selectedTabs = it.selectedTabs - collectionCreationTabListAdapter.updateData(it.tabs, it.selectedTabs) + step = it.saveCollectionStep + when (it.saveCollectionStep) { + is SaveCollectionStep.SelectTabs -> { + back_button.setOnClickListener { + actionEmitter.onNext(CollectionCreationAction.BackPressed(SaveCollectionStep.SelectTabs)) + } - val buttonText = if (it.selectedTabs.isEmpty()) { - view.context.getString(R.string.create_collection_save_to_collection_empty) - } else { - view.context.getString(R.string.create_collection_save_to_collection_full, it.selectedTabs.size) + name_collection_edittext.visibility = View.GONE + collections_list.visibility = View.GONE + add_collection_button.visibility = View.GONE + divider.visibility = View.GONE + + this.selectedTabs = it.selectedTabs + collectionCreationTabListAdapter.updateData(it.tabs, it.selectedTabs) + + back_button.text = view.context.getString(R.string.create_collection_select_tabs) + + val buttonText = if (it.selectedTabs.isEmpty()) { + view.context.getString(R.string.create_collection_save_to_collection_empty) + } else { + view.context.getString( + R.string.create_collection_save_to_collection_full, + it.selectedTabs.size + ) + } + + tab_list.visibility = View.VISIBLE + select_all_button.visibility = View.VISIBLE + add_tabs_button.visibility = View.VISIBLE + + val enableSaveButton = it.selectedTabs.isNotEmpty() + view.add_tabs_button.isClickable = enableSaveButton + + view.add_tabs_button.contentDescription = buttonText + view.add_tabs_button_text.text = buttonText + } + is SaveCollectionStep.SelectCollection -> { + back_button.setOnClickListener { + actionEmitter.onNext(CollectionCreationAction.BackPressed(SaveCollectionStep.SelectCollection)) + } + collections_list.visibility = View.VISIBLE + add_collection_button.visibility = View.VISIBLE + divider.visibility = View.VISIBLE + tab_list.visibility = View.GONE + select_all_button.visibility = View.GONE + add_tabs_button.visibility = View.GONE + name_collection_edittext.visibility = View.GONE + + back_button.text = + view.context.getString(R.string.create_collection_select_collection) + } + is SaveCollectionStep.NameCollection -> { + back_button.setOnClickListener { + actionEmitter.onNext(CollectionCreationAction.BackPressed(SaveCollectionStep.NameCollection)) + } + name_collection_edittext.visibility = View.VISIBLE + name_collection_edittext.requestFocus() + val imm = + view.context.getSystemService(INPUT_METHOD_SERVICE) as? InputMethodManager + imm?.showSoftInput(name_collection_edittext, SHOW_IMPLICIT) + collections_list.visibility = View.GONE + add_collection_button.visibility = View.GONE + divider.visibility = View.GONE + + tab_list.visibility = View.GONE + select_all_button.visibility = View.GONE + add_tabs_button.visibility = View.GONE + back_button.text = + view.context.getString(R.string.create_collection_name_collection) + } } + } - val enableSaveButton = it.selectedTabs.isNotEmpty() - view.add_tabs_button.isClickable = enableSaveButton - - view.add_tabs_button.contentDescription = buttonText - view.add_tabs_button_text.text = buttonText + fun onKey(keyCode: Int, event: KeyEvent?): Boolean { + if (event?.action == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) { + when (step) { + SaveCollectionStep.SelectTabs -> { + actionEmitter.onNext(CollectionCreationAction.BackPressed(SaveCollectionStep.SelectTabs)) + } + SaveCollectionStep.SelectCollection -> { + actionEmitter.onNext(CollectionCreationAction.BackPressed(SaveCollectionStep.SelectCollection)) + } + SaveCollectionStep.NameCollection -> { + actionEmitter.onNext(CollectionCreationAction.BackPressed(SaveCollectionStep.NameCollection)) + } + } + } + return true } companion object { diff --git a/app/src/main/java/org/mozilla/fenix/collections/CreateCollectionFragment.kt b/app/src/main/java/org/mozilla/fenix/collections/CreateCollectionFragment.kt index fb95329a8..4e8ecdc53 100644 --- a/app/src/main/java/org/mozilla/fenix/collections/CreateCollectionFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/collections/CreateCollectionFragment.kt @@ -4,10 +4,13 @@ package org.mozilla.fenix.collections 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/. */ +import android.app.Dialog +import android.content.Context import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.view.inputmethod.InputMethodManager import androidx.fragment.app.DialogFragment import androidx.lifecycle.ViewModelProviders import kotlinx.android.synthetic.main.fragment_create_collection.view.* @@ -15,15 +18,14 @@ import org.mozilla.fenix.R import org.mozilla.fenix.mvi.ActionBusFactory import org.mozilla.fenix.mvi.getAutoDisposeObservable import org.mozilla.fenix.mvi.getManagedEmitter -import org.mozilla.fenix.utils.ItsNotBrokenSnack class CreateCollectionFragment : DialogFragment() { - private lateinit var collectionCreationComponent: CollectionCreationComponent override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CreateCollectionDialogStyle) + isCancelable = false + setStyle(STYLE_NO_TITLE, R.style.CreateCollectionDialogStyle) } override fun onCreateView( @@ -44,19 +46,29 @@ class CreateCollectionFragment : DialogFragment() { ActionBusFactory.get(this), CollectionCreationState(tabs = tabs, selectedTabs = selectedTabs) ) - return view } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val dialog = super.onCreateDialog(savedInstanceState) + dialog.setOnKeyListener { _, keyCode, event -> + (collectionCreationComponent.uiView as CollectionCreationUIView).onKey(keyCode, event) + } + return dialog + } + + override fun onResume() { + super.onResume() + subscribeToActions() + } + + private fun subscribeToActions() { getAutoDisposeObservable().subscribe { when (it) { is CollectionCreationAction.Close -> dismiss() is CollectionCreationAction.SaveTabsToCollection -> { - dismiss() - ItsNotBrokenSnack(requireContext()) - .showSnackbar("1843") + getManagedEmitter() + .onNext(CollectionCreationChange.StepChanged(SaveCollectionStep.SelectCollection)) } is CollectionCreationAction.AddTabToSelection -> getManagedEmitter() .onNext(CollectionCreationChange.TabAdded(it.tab)) @@ -64,6 +76,27 @@ class CreateCollectionFragment : DialogFragment() { .onNext(CollectionCreationChange.TabRemoved(it.tab)) is CollectionCreationAction.SelectAllTapped -> getManagedEmitter() .onNext(CollectionCreationChange.AddAllTabs) + is CollectionCreationAction.AddNewCollection -> getManagedEmitter().onNext( + CollectionCreationChange.StepChanged(SaveCollectionStep.NameCollection) + ) + is CollectionCreationAction.BackPressed -> handleBackPress(backPressFrom = it.backPressFrom) + } + } + } + + private fun handleBackPress(backPressFrom: SaveCollectionStep) { + when (backPressFrom) { + SaveCollectionStep.SelectTabs -> dismiss() + SaveCollectionStep.SelectCollection -> getManagedEmitter().onNext( + CollectionCreationChange.StepChanged(SaveCollectionStep.SelectTabs) + ) + SaveCollectionStep.NameCollection -> { + val imm = + view?.context?.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager + imm?.hideSoftInputFromWindow(view?.windowToken, 0) + getManagedEmitter().onNext( + CollectionCreationChange.StepChanged(SaveCollectionStep.SelectCollection) + ) } } } diff --git a/app/src/main/java/org/mozilla/fenix/collections/SaveToCollectionListAdapter.kt b/app/src/main/java/org/mozilla/fenix/collections/SaveToCollectionListAdapter.kt new file mode 100644 index 000000000..68147d827 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/collections/SaveToCollectionListAdapter.kt @@ -0,0 +1,82 @@ +package org.mozilla.fenix.collections + +/* 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/. */ + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import io.reactivex.Observer +import kotlinx.android.synthetic.main.collections_list_item.view.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import org.mozilla.fenix.R +import kotlin.coroutines.CoroutineContext + +class SaveCollectionListAdapter( + val actionEmitter: Observer +) : RecyclerView.Adapter() { + + private var collections: List = listOf() + private lateinit var job: Job + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CollectionViewHolder { + val view = LayoutInflater.from(parent.context) + .inflate(CollectionViewHolder.LAYOUT_ID, parent, false) + + return CollectionViewHolder(view, actionEmitter, job) + } + + override fun onBindViewHolder(holder: CollectionViewHolder, position: Int) { + val collection = collections[position] + holder.bind(collection) + } + + override fun getItemCount(): Int = collections.size + + override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { + super.onAttachedToRecyclerView(recyclerView) + job = Job() + } + + override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { + super.onDetachedFromRecyclerView(recyclerView) + job.cancel() + } +} + +class CollectionViewHolder( + val view: View, + actionEmitter: Observer, + val job: Job +) : + RecyclerView.ViewHolder(view), CoroutineScope { + + override val coroutineContext: CoroutineContext + get() = Dispatchers.IO + job + + private var collection: Collection? = null + + private val listener = View.OnClickListener { + collection?.apply { + val action = CollectionCreationAction.SelectCollection(this) + actionEmitter.onNext(action) + } + } + + init { + view.setOnClickListener(listener) + } + + fun bind(collection: Collection) { + this.collection = collection + view.collection_item.text = collection.title + } + + companion object { + const val LAYOUT_ID = R.layout.collections_list_item + } +} diff --git a/app/src/main/res/layout/collections_list_item.xml b/app/src/main/res/layout/collections_list_item.xml new file mode 100644 index 000000000..78dffb26b --- /dev/null +++ b/app/src/main/res/layout/collections_list_item.xml @@ -0,0 +1,35 @@ + + + + + + + + diff --git a/app/src/main/res/layout/component_collection_creation.xml b/app/src/main/res/layout/component_collection_creation.xml index 0d538363c..803c2fd48 100644 --- a/app/src/main/res/layout/component_collection_creation.xml +++ b/app/src/main/res/layout/component_collection_creation.xml @@ -2,108 +2,164 @@ - + android:clipToPadding="false">