update picked alarm sound when a brand new file is added

This commit is contained in:
tibbi 2018-03-29 21:01:52 +02:00
parent c1b056c858
commit 2df47166a9
3 changed files with 20 additions and 4 deletions

View File

@ -141,8 +141,9 @@ class MainActivity : SimpleActivity() {
val token = object : TypeToken<ArrayList<AlarmSound>>() {}.type
val yourAlarmSounds = Gson().fromJson<ArrayList<AlarmSound>>(config.yourAlarmSounds, token) ?: ArrayList()
val newAlarmSoundId = (yourAlarmSounds.maxBy { it.id }?.id ?: YOUR_ALARM_SOUNDS_MIN_ID) + 1
val newAlarmSound = AlarmSound(newAlarmSoundId, filename, uri.toString())
if (yourAlarmSounds.firstOrNull { it.uri == uri.toString() } == null) {
yourAlarmSounds.add(AlarmSound(newAlarmSoundId, filename, uri.toString()))
yourAlarmSounds.add(newAlarmSound)
}
config.yourAlarmSounds = Gson().toJson(yourAlarmSounds)
@ -151,6 +152,10 @@ class MainActivity : SimpleActivity() {
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION
contentResolver.takePersistableUriPermission(uri, takeFlags)
}
when (viewpager.currentItem) {
TAB_TIMER -> (viewpager.adapter as? ViewPagerAdapter)?.updateTimerAlarmSound(newAlarmSound)
}
}
private fun initFragments() {

View File

@ -9,6 +9,8 @@ import com.simplemobiletools.clock.fragments.ClockFragment
import com.simplemobiletools.clock.fragments.StopwatchFragment
import com.simplemobiletools.clock.fragments.TimerFragment
import com.simplemobiletools.clock.helpers.TABS_COUNT
import com.simplemobiletools.clock.helpers.TAB_TIMER
import com.simplemobiletools.clock.models.AlarmSound
class ViewPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
private val fragments = HashMap<Int, Fragment>()
@ -33,4 +35,8 @@ class ViewPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
3 -> TimerFragment()
else -> throw RuntimeException("Trying to fetch unknown fragment id $position")
}
fun updateTimerAlarmSound(alarmSound: AlarmSound) {
(fragments[TAB_TIMER] as TimerFragment).updateAlarmSound(alarmSound)
}
}

View File

@ -16,6 +16,7 @@ import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.dialogs.MyTimePickerDialogDialog
import com.simplemobiletools.clock.dialogs.SelectAlarmSoundDialog
import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.clock.models.AlarmSound
import com.simplemobiletools.commons.extensions.*
import kotlinx.android.synthetic.main.fragment_timer.view.*
@ -67,9 +68,7 @@ class TimerFragment : Fragment() {
timer_sound.setOnClickListener {
SelectAlarmSoundDialog(activity as SimpleActivity, config.timerSoundUri, AudioManager.STREAM_SYSTEM) {
if (it != null) {
config.timerSoundTitle = it.title
config.timerSoundUri = it.uri
timer_sound.text = it.title
updateAlarmSound(it)
}
}
}
@ -104,6 +103,12 @@ class TimerFragment : Fragment() {
updateHandler.removeCallbacks(updateRunnable)
}
fun updateAlarmSound(alarmSound: AlarmSound) {
context!!.config.timerSoundTitle = alarmSound.title
context!!.config.timerSoundUri = alarmSound.uri
view.timer_sound.text = alarmSound.title
}
private fun setupViews() {
val config = context!!.config
val textColor = config.textColor