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.util.AttributeSet
import android.widget.SeekBar import android.widget.SeekBar
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isQPlus import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SimpleActivity import com.simplemobiletools.voicerecorder.activities.SimpleActivity
@ -138,44 +137,40 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
} }
private fun setupAdapter(recordings: ArrayList<Recording>) { private fun setupAdapter(recordings: ArrayList<Recording>) {
ensureBackgroundThread { recordings_fastscroller.beVisibleIf(recordings.isNotEmpty())
Handler(Looper.getMainLooper()).post { recordings_placeholder.beVisibleIf(recordings.isEmpty())
recordings_fastscroller.beVisibleIf(recordings.isNotEmpty()) if (recordings.isEmpty()) {
recordings_placeholder.beVisibleIf(recordings.isEmpty()) val stringId = if (lastSearchQuery.isEmpty()) {
if (recordings.isEmpty()) { if (isQPlus()) {
val stringId = if (lastSearchQuery.isEmpty()) { R.string.no_recordings_found
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()
}
} else { } 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 package com.simplemobiletools.voicerecorder.fragments
import android.content.Context import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet import android.util.AttributeSet
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SimpleActivity import com.simplemobiletools.voicerecorder.activities.SimpleActivity
import com.simplemobiletools.voicerecorder.adapters.TrashAdapter import com.simplemobiletools.voicerecorder.adapters.TrashAdapter
@ -63,34 +60,30 @@ class TrashFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
override fun playRecording(recording: Recording, playOnPrepared: Boolean) {} override fun playRecording(recording: Recording, playOnPrepared: Boolean) {}
private fun setupAdapter(recordings: ArrayList<Recording>) { private fun setupAdapter(recordings: ArrayList<Recording>) {
ensureBackgroundThread { recordings_fastscroller.beVisibleIf(recordings.isNotEmpty())
Handler(Looper.getMainLooper()).post { recordings_placeholder.beVisibleIf(recordings.isEmpty())
recordings_fastscroller.beVisibleIf(recordings.isNotEmpty()) if (recordings.isEmpty()) {
recordings_placeholder.beVisibleIf(recordings.isEmpty()) val stringId = if (lastSearchQuery.isEmpty()) {
if (recordings.isEmpty()) { R.string.recycle_bin_empty
val stringId = if (lastSearchQuery.isEmpty()) { } else {
R.string.recycle_bin_empty R.string.no_items_found
} 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_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)
} }
} }