diff --git a/app/src/main/java/com/stevenschoen/putionew/files/FileDetailsFragment.kt b/app/src/main/java/com/stevenschoen/putionew/files/FileDetailsFragment.kt index 1879d27d..a704956b 100644 --- a/app/src/main/java/com/stevenschoen/putionew/files/FileDetailsFragment.kt +++ b/app/src/main/java/com/stevenschoen/putionew/files/FileDetailsFragment.kt @@ -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) @@ -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, @@ -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 { @@ -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 @@ -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 @@ -409,7 +411,9 @@ class FileDetailsFragment : RxFragment() { } setTitleBackgroundColor(ContextCompat.getColor( context, backgroundColorRes), animate) - } + }, { error -> + PutioUtils.getRxJavaThrowable(error).printStackTrace() + }) } } } diff --git a/app/src/main/java/com/stevenschoen/putionew/files/FileListAdapter.kt b/app/src/main/java/com/stevenschoen/putionew/files/FileListAdapter.kt index caecfe4d..bd62a7d8 100644 --- a/app/src/main/java/com/stevenschoen/putionew/files/FileListAdapter.kt +++ b/app/src/main/java/com/stevenschoen/putionew/files/FileListAdapter.kt @@ -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 @@ -15,11 +16,12 @@ import java.util.* class FileListAdapter(private val data: List, val onFileClicked: (file: PutioFile, holder: FileHolder) -> Unit, - val onFileLongClicked: (file: PutioFile, holder: FileHolder) -> Unit) : RecyclerView.Adapter() { + val onFileLongClicked: (file: PutioFile, holder: FileHolder) -> Unit) + : RecyclerView.Adapter() { private var itemsCheckedChangedListener: OnItemsCheckedChangedListener? = null - val checkedIds = ArrayList() + val checkedIds = ArraySet() init { setHasStableIds(true) @@ -27,16 +29,10 @@ class FileListAdapter(private val data: List, override fun onChanged() { super.onChanged() if (isInCheckMode()) { - val idsToRemove = ArrayList() + val idsToRemove = ArraySet() 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) } @@ -102,7 +98,7 @@ class FileListAdapter(private val data: List, return -1 } - override fun getItemCount(): Int = data.size + override fun getItemCount() = data.size fun isInCheckMode() = checkedIds.isNotEmpty() @@ -113,21 +109,13 @@ class FileListAdapter(private val data: List, 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) @@ -141,12 +129,9 @@ class FileListAdapter(private val data: List, } 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() } @@ -154,7 +139,7 @@ class FileListAdapter(private val data: List, fun clearChecked() { if (checkedIds.isNotEmpty()) { - val previouslyCheckedIds = ArrayList(checkedIds) + val previouslyCheckedIds = ArraySet(checkedIds) checkedIds.clear() for (id in previouslyCheckedIds) { notifyItemChanged(getItemPosition(id)) @@ -174,10 +159,10 @@ class FileListAdapter(private val data: List, } inner class FileHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { - val textName = itemView.findViewById(R.id.text_file_name)!! - val textDescription = itemView.findViewById(R.id.text_file_description)!! - val iconImg = itemView.findViewById(R.id.icon_file_img)!! - val iconAccessed = itemView.findViewById(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 {