Skip to content

Commit

Permalink
Use sets for file selection
Browse files Browse the repository at this point in the history
  • Loading branch information
DSteve595 committed Oct 1, 2017
1 parent 8f994dc commit d3f97cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,8 @@ class FileDetailsFragment : RxFragment() {
val sendItem = downloadMorePopup.menu.add(R.string.send).setOnMenuItemClickListener {
Single.fromCallable {
fileDownloads.getByFileIdSynchronous(file.id)!!
}
}.subscribeOn(Schedulers.io())
.bindToLifecycle(this@FileDetailsFragment)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
val uri = Uri.parse(it.uri)
Expand All @@ -259,7 +258,8 @@ class FileDetailsFragment : RxFragment() {
}

val fileDownload = fileDownloads.getByFileId(file.id)
.bindToLifecycle(this)
.bindToLifecycle(this@FileDetailsFragment)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.toObservable()
val fileDownloadWithDefault = fileDownload.startWith(FileDownload(file.id,
Expand All @@ -276,7 +276,7 @@ class FileDetailsFragment : RxFragment() {
// Assume it's completed, most common case
status = PutioMp4Status.Status.Completed
})
.bindToLifecycle(this)
.bindToLifecycle(this@FileDetailsFragment)
} else if (useVideoTitleBackground) {
Observable.just(PutioMp4Status().apply { status = PutioMp4Status.Status.AlreadyMp4 })
} else {
Expand All @@ -289,7 +289,7 @@ class FileDetailsFragment : RxFragment() {
fileDownloadWithDefault, mp4Status, BiFunction { newDownload, newMp4Status ->
DownloadAndMp4Status(newDownload, newMp4Status)
})
downloadAndMp4Status.subscribe { (newDownload, newMp4Status) ->
downloadAndMp4Status.subscribe({ (newDownload, newMp4Status) ->
val downloadDone = newDownload.status == FileDownload.Status.Downloaded
val downloadedMp4 = newDownload.downloadedMp4 ?: false
val playMp4String = if (downloadDone && downloadedMp4) R.string.play else R.string.stream
Expand Down Expand Up @@ -397,10 +397,12 @@ class FileDetailsFragment : RxFragment() {
}
}
lastDownloadStatus = newDownload.status
}
}, { error ->
PutioUtils.getRxJavaThrowable(error).printStackTrace()
})

if (!useVideoTitleBackground) {
downloadStatus.subscribe {
downloadStatus.subscribe({
val animate = (lastDownloadStatus != null)
val backgroundColorRes = when (it!!) {
FileDownload.Status.Downloaded -> R.color.putio_filedetails_downloaded
Expand All @@ -409,7 +411,9 @@ class FileDetailsFragment : RxFragment() {
}
setTitleBackgroundColor(ContextCompat.getColor(
context, backgroundColorRes), animate)
}
}, { error ->
PutioUtils.getRxJavaThrowable(error).printStackTrace()
})
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stevenschoen.putionew.files

import android.support.v4.util.ArraySet
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
Expand All @@ -15,28 +16,23 @@ import java.util.*

class FileListAdapter(private val data: List<PutioFile>,
val onFileClicked: (file: PutioFile, holder: FileHolder) -> Unit,
val onFileLongClicked: (file: PutioFile, holder: FileHolder) -> Unit) : RecyclerView.Adapter<FileListAdapter.FileHolder>() {
val onFileLongClicked: (file: PutioFile, holder: FileHolder) -> Unit)
: RecyclerView.Adapter<FileListAdapter.FileHolder>() {

private var itemsCheckedChangedListener: OnItemsCheckedChangedListener? = null

val checkedIds = ArrayList<Long>()
val checkedIds = ArraySet<Long>()

init {
setHasStableIds(true)
registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onChanged() {
super.onChanged()
if (isInCheckMode()) {
val idsToRemove = ArrayList<Long>()
val idsToRemove = ArraySet<Long>()

for (checkedId in checkedIds) {
var stillHas = false
for (file in data) {
if (file.id == checkedId) {
stillHas = true
break
}
}
val stillHas = data.any { it.id == checkedId }
if (!stillHas) {
idsToRemove.add(checkedId)
}
Expand Down Expand Up @@ -102,7 +98,7 @@ class FileListAdapter(private val data: List<PutioFile>,
return -1
}

override fun getItemCount(): Int = data.size
override fun getItemCount() = data.size

fun isInCheckMode() = checkedIds.isNotEmpty()

Expand All @@ -113,21 +109,13 @@ class FileListAdapter(private val data: List<PutioFile>,
return (itemId != -1L) && (checkedIds.contains(itemId))
}

fun getCheckedPositions(): IntArray {
val checkedPositions = IntArray(checkedIds.size)
for (i in checkedIds.indices) {
val id = checkedIds[i]
checkedPositions[i] = getItemPosition(id)
}

return checkedPositions
}
fun getCheckedPositions() = checkedIds.map { getItemPosition(it) }

fun setPositionChecked(position: Int, checked: Boolean) {
val itemId = getItemId(position)
if (checked && !checkedIds.contains(itemId)) {
if (checked) {
checkedIds.add(itemId)
} else if (!checked) {
} else {
checkedIds.remove(itemId)
}
notifyItemChanged(position)
Expand All @@ -141,20 +129,17 @@ class FileListAdapter(private val data: List<PutioFile>,
}

fun addCheckedIds(vararg ids: Long) {
for (id in ids) {
if (!checkedIds.contains(id)) {
checkedIds.add(id)
notifyItemChanged(getItemPosition(id))
}
}
ids
.filter { checkedIds.add(it) }
.forEach { notifyItemChanged(getItemPosition(it)) }
if (itemsCheckedChangedListener != null) {
itemsCheckedChangedListener!!.onItemsCheckedChanged()
}
}

fun clearChecked() {
if (checkedIds.isNotEmpty()) {
val previouslyCheckedIds = ArrayList<Long>(checkedIds)
val previouslyCheckedIds = ArraySet<Long>(checkedIds)
checkedIds.clear()
for (id in previouslyCheckedIds) {
notifyItemChanged(getItemPosition(id))
Expand All @@ -174,10 +159,10 @@ class FileListAdapter(private val data: List<PutioFile>,
}

inner class FileHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textName = itemView.findViewById<TextView>(R.id.text_file_name)!!
val textDescription = itemView.findViewById<TextView>(R.id.text_file_description)!!
val iconImg = itemView.findViewById<ImageView>(R.id.icon_file_img)!!
val iconAccessed = itemView.findViewById<ImageView>(R.id.icon_file_accessed)!!
val textName: TextView = itemView.findViewById(R.id.text_file_name)
val textDescription: TextView = itemView.findViewById(R.id.text_file_description)
val iconImg: ImageView = itemView.findViewById(R.id.icon_file_img)
val iconAccessed: ImageView = itemView.findViewById(R.id.icon_file_accessed)

init {
itemView.setOnClickListener {
Expand Down

0 comments on commit d3f97cf

Please sign in to comment.