Remove needless background thread and main thread wrappers in setupAdapter

This commit is contained in:
Ensar Sarajčić 2023-07-27 10:50:04 +02:00
parent 68166c318c
commit a0278914ee
2 changed files with 54 additions and 66 deletions

View File

@ -12,7 +12,6 @@ import android.provider.DocumentsContract
import android.util.AttributeSet
import android.widget.SeekBar
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SimpleActivity
@ -138,44 +137,40 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
}
private fun setupAdapter(recordings: ArrayList<Recording>) {
ensureBackgroundThread {
Handler(Looper.getMainLooper()).post {
recordings_fastscroller.beVisibleIf(recordings.isNotEmpty())
recordings_placeholder.beVisibleIf(recordings.isEmpty())
if (recordings.isEmpty()) {
val stringId = if (lastSearchQuery.isEmpty()) {
if (isQPlus()) {
R.string.no_recordings_found
} else {
R.string.no_recordings_in_folder_found
}
} else {
R.string.no_items_found
}
recordings_placeholder.text = context.getString(stringId)
resetProgress(null)
player?.stop()
}
val adapter = getRecordingsAdapter()
if (adapter == null) {
RecordingsAdapter(context as SimpleActivity, recordings, this, recordings_list) {
playRecording(it as Recording, true)
if (playedRecordingIDs.isEmpty() || playedRecordingIDs.peek() != it.id) {
playedRecordingIDs.push(it.id)
}
}.apply {
recordings_list.adapter = this
}
if (context.areSystemAnimationsEnabled) {
recordings_list.scheduleLayoutAnimation()
}
recordings_fastscroller.beVisibleIf(recordings.isNotEmpty())
recordings_placeholder.beVisibleIf(recordings.isEmpty())
if (recordings.isEmpty()) {
val stringId = if (lastSearchQuery.isEmpty()) {
if (isQPlus()) {
R.string.no_recordings_found
} else {
adapter.updateItems(recordings)
R.string.no_recordings_in_folder_found
}
} else {
R.string.no_items_found
}
recordings_placeholder.text = context.getString(stringId)
resetProgress(null)
player?.stop()
}
val adapter = getRecordingsAdapter()
if (adapter == null) {
RecordingsAdapter(context as SimpleActivity, recordings, this, recordings_list) {
playRecording(it as Recording, true)
if (playedRecordingIDs.isEmpty() || playedRecordingIDs.peek() != it.id) {
playedRecordingIDs.push(it.id)
}
}.apply {
recordings_list.adapter = this
}
if (context.areSystemAnimationsEnabled) {
recordings_list.scheduleLayoutAnimation()
}
} else {
adapter.updateItems(recordings)
}
}

View File

@ -1,11 +1,8 @@
package com.simplemobiletools.voicerecorder.fragments
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SimpleActivity
import com.simplemobiletools.voicerecorder.adapters.TrashAdapter
@ -63,34 +60,30 @@ class TrashFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
override fun playRecording(recording: Recording, playOnPrepared: Boolean) {}
private fun setupAdapter(recordings: ArrayList<Recording>) {
ensureBackgroundThread {
Handler(Looper.getMainLooper()).post {
recordings_fastscroller.beVisibleIf(recordings.isNotEmpty())
recordings_placeholder.beVisibleIf(recordings.isEmpty())
if (recordings.isEmpty()) {
val stringId = if (lastSearchQuery.isEmpty()) {
R.string.recycle_bin_empty
} else {
R.string.no_items_found
}
recordings_placeholder.text = context.getString(stringId)
}
val adapter = getRecordingsAdapter()
if (adapter == null) {
TrashAdapter(context as SimpleActivity, recordings, this, recordings_list)
.apply {
recordings_list.adapter = this
}
if (context.areSystemAnimationsEnabled) {
recordings_list.scheduleLayoutAnimation()
}
} else {
adapter.updateItems(recordings)
}
recordings_fastscroller.beVisibleIf(recordings.isNotEmpty())
recordings_placeholder.beVisibleIf(recordings.isEmpty())
if (recordings.isEmpty()) {
val stringId = if (lastSearchQuery.isEmpty()) {
R.string.recycle_bin_empty
} else {
R.string.no_items_found
}
recordings_placeholder.text = context.getString(stringId)
}
val adapter = getRecordingsAdapter()
if (adapter == null) {
TrashAdapter(context as SimpleActivity, recordings, this, recordings_list)
.apply {
recordings_list.adapter = this
}
if (context.areSystemAnimationsEnabled) {
recordings_list.scheduleLayoutAnimation()
}
} else {
adapter.updateItems(recordings)
}
}