[fenix] For https://github.com/mozilla-mobile/fenix/issues/1573 - Create Save to Collection Component and Views
parent
5d452b9c10
commit
65ca28a7b2
@ -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<CollectionCreationAction>
|
||||
) : RecyclerView.Adapter<CollectionViewHolder>() {
|
||||
|
||||
private var collections: List<Collection> = 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<CollectionCreationAction>,
|
||||
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
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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/. -->
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/collection_item"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?foundation"
|
||||
android:drawableStart="@drawable/ic_archive"
|
||||
android:drawablePadding="14dp"
|
||||
android:drawableTint="?accent"
|
||||
android:paddingStart="20dp"
|
||||
android:paddingTop="12dp"
|
||||
android:paddingBottom="12dp"
|
||||
android:textColor="?primaryText"
|
||||
android:textSize="16sp"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toTopOf="@+id/divider"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:targetApi="m" />
|
||||
|
||||
<View
|
||||
android:id="@+id/divider"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="?neutralFaded"
|
||||
app:layout_constraintBottom_toBottomOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue