refresh the recordings list after renaming an item

This commit is contained in:
tibbi 2020-04-02 11:24:19 +02:00
parent 35ea5868c3
commit cda6ccdabf
3 changed files with 37 additions and 14 deletions

View file

@ -14,12 +14,13 @@ import com.simplemobiletools.commons.views.MyRecyclerView
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.dialogs.RenameRecordingDialog import com.simplemobiletools.voicerecorder.dialogs.RenameRecordingDialog
import com.simplemobiletools.voicerecorder.interfaces.RefreshRecordingsListener
import com.simplemobiletools.voicerecorder.models.Recording import com.simplemobiletools.voicerecorder.models.Recording
import kotlinx.android.synthetic.main.item_recording.view.* import kotlinx.android.synthetic.main.item_recording.view.*
import java.util.* import java.util.*
class RecordingsAdapter(activity: SimpleActivity, var recordings: ArrayList<Recording>, recyclerView: MyRecyclerView, fastScroller: FastScroller, class RecordingsAdapter(activity: SimpleActivity, var recordings: ArrayList<Recording>, val refreshListener: RefreshRecordingsListener,
itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) { recyclerView: MyRecyclerView, fastScroller: FastScroller, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) {
var currRecordingId = 0 var currRecordingId = 0
init { init {
@ -76,6 +77,7 @@ class RecordingsAdapter(activity: SimpleActivity, var recordings: ArrayList<Reco
val recording = getItemWithKey(selectedKeys.first()) ?: return val recording = getItemWithKey(selectedKeys.first()) ?: return
RenameRecordingDialog(activity, recording) { RenameRecordingDialog(activity, recording) {
finishActMode() finishActMode()
refreshListener.refreshRecordings()
} }
} }

View file

@ -17,12 +17,13 @@ import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SimpleActivity import com.simplemobiletools.voicerecorder.activities.SimpleActivity
import com.simplemobiletools.voicerecorder.adapters.RecordingsAdapter import com.simplemobiletools.voicerecorder.adapters.RecordingsAdapter
import com.simplemobiletools.voicerecorder.extensions.config import com.simplemobiletools.voicerecorder.extensions.config
import com.simplemobiletools.voicerecorder.interfaces.RefreshRecordingsListener
import com.simplemobiletools.voicerecorder.models.Recording import com.simplemobiletools.voicerecorder.models.Recording
import kotlinx.android.synthetic.main.fragment_player.view.* import kotlinx.android.synthetic.main.fragment_player.view.*
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) { class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshRecordingsListener {
private val FAST_FORWARD_SKIP_MS = 10000 private val FAST_FORWARD_SKIP_MS = 10000
private var player: MediaPlayer? = null private var player: MediaPlayer? = null
@ -45,24 +46,19 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
super.onAttachedToWindow() super.onAttachedToWindow()
setupColors() setupColors()
val recordings = getRecordings() setupAdapter()
val adapter = RecordingsAdapter(context as SimpleActivity, recordings, recordings_list, recordings_fastscroller) { initMediaPlayer()
playRecording(it as Recording) setupViews()
if (playedRecordingIDs.isEmpty() || playedRecordingIDs.peek() != it.id) { }
playedRecordingIDs.push(it.id)
}
}.apply {
recordings_list.adapter = this
}
private fun setupViews() {
recordings_fastscroller.setScrollToY(0) recordings_fastscroller.setScrollToY(0)
recordings_fastscroller.setViews(recordings_list) { recordings_fastscroller.setViews(recordings_list) {
val adapter = getRecordingsAdapter() ?: return@setViews
val item = adapter.recordings.getOrNull(it) val item = adapter.recordings.getOrNull(it)
recordings_fastscroller.updateBubbleText(item?.title ?: "") recordings_fastscroller.updateBubbleText(item?.title ?: "")
} }
initMediaPlayer()
play_pause_btn.setOnClickListener { play_pause_btn.setOnClickListener {
if (playedRecordingIDs.empty() || player_progressbar.max == 0) { if (playedRecordingIDs.empty() || player_progressbar.max == 0) {
next_btn.callOnClick() next_btn.callOnClick()
@ -84,6 +80,7 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
return@setOnClickListener return@setOnClickListener
} }
val adapter = getRecordingsAdapter() ?: return@setOnClickListener
var wantedRecordingID = playedRecordingIDs.pop() var wantedRecordingID = playedRecordingIDs.pop()
if (wantedRecordingID == adapter.currRecordingId && !playedRecordingIDs.isEmpty()) { if (wantedRecordingID == adapter.currRecordingId && !playedRecordingIDs.isEmpty()) {
wantedRecordingID = playedRecordingIDs.pop() wantedRecordingID = playedRecordingIDs.pop()
@ -95,6 +92,7 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
} }
next_btn.setOnClickListener { next_btn.setOnClickListener {
val adapter = getRecordingsAdapter() ?: return@setOnClickListener
val oldRecordingIndex = adapter.recordings.indexOfFirst { it.id == adapter.currRecordingId } val oldRecordingIndex = adapter.recordings.indexOfFirst { it.id == adapter.currRecordingId }
val newRecordingIndex = (oldRecordingIndex + 1) % adapter.recordings.size val newRecordingIndex = (oldRecordingIndex + 1) % adapter.recordings.size
val newRecording = adapter.recordings.getOrNull(newRecordingIndex) ?: return@setOnClickListener val newRecording = adapter.recordings.getOrNull(newRecordingIndex) ?: return@setOnClickListener
@ -103,6 +101,22 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
} }
} }
override fun refreshRecordings() {
setupAdapter()
}
private fun setupAdapter() {
val recordings = getRecordings()
RecordingsAdapter(context as SimpleActivity, recordings, this, recordings_list, recordings_fastscroller) {
playRecording(it as Recording)
if (playedRecordingIDs.isEmpty() || playedRecordingIDs.peek() != it.id) {
playedRecordingIDs.push(it.id)
}
}.apply {
recordings_list.adapter = this
}
}
@SuppressLint("InlinedApi") @SuppressLint("InlinedApi")
private fun getRecordings(): ArrayList<Recording> { private fun getRecordings(): ArrayList<Recording> {
val recordings = ArrayList<Recording>() val recordings = ArrayList<Recording>()
@ -263,6 +277,8 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
private fun getIsPlaying() = player?.isPlaying == true private fun getIsPlaying() = player?.isPlaying == true
private fun getRecordingsAdapter() = recordings_list.adapter as? RecordingsAdapter
private fun setupColors() { private fun setupColors() {
recordings_fastscroller.updatePrimaryColor() recordings_fastscroller.updatePrimaryColor()
recordings_fastscroller.updateBubbleColors() recordings_fastscroller.updateBubbleColors()

View file

@ -0,0 +1,5 @@
package com.simplemobiletools.voicerecorder.interfaces
interface RefreshRecordingsListener {
fun refreshRecordings()
}