properly handle adding a new alarmsound from edit alarm dialog too

This commit is contained in:
tibbi
2018-03-29 21:16:45 +02:00
parent 2df47166a9
commit 8f07546831
5 changed files with 42 additions and 17 deletions

View File

@ -61,7 +61,7 @@ class MainActivity : SimpleActivity() {
val configTextColor = config.textColor val configTextColor = config.textColor
if (storedTextColor != configTextColor) { if (storedTextColor != configTextColor) {
getInactiveTabIndexes(viewpager.currentItem).forEach { getInactiveTabIndexes(view_pager.currentItem).forEach {
main_tabs_holder.getTabAt(it)?.icon?.applyColorFilter(configTextColor) main_tabs_holder.getTabAt(it)?.icon?.applyColorFilter(configTextColor)
} }
} }
@ -74,7 +74,7 @@ class MainActivity : SimpleActivity() {
val configPrimaryColor = config.primaryColor val configPrimaryColor = config.primaryColor
if (storedPrimaryColor != configPrimaryColor) { if (storedPrimaryColor != configPrimaryColor) {
main_tabs_holder.setSelectedTabIndicatorColor(getAdjustedPrimaryColor()) main_tabs_holder.setSelectedTabIndicatorColor(getAdjustedPrimaryColor())
main_tabs_holder.getTabAt(viewpager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor()) main_tabs_holder.getTabAt(view_pager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor())
} }
if (config.preventPhoneFromSleeping) { if (config.preventPhoneFromSleeping) {
@ -92,7 +92,7 @@ class MainActivity : SimpleActivity() {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
config.lastUsedViewPagerPage = viewpager.currentItem config.lastUsedViewPagerPage = view_pager.currentItem
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -111,7 +111,7 @@ class MainActivity : SimpleActivity() {
override fun onNewIntent(intent: Intent) { override fun onNewIntent(intent: Intent) {
if (intent.extras?.containsKey(OPEN_TAB) == true) { if (intent.extras?.containsKey(OPEN_TAB) == true) {
viewpager.setCurrentItem(intent.getIntExtra(OPEN_TAB, TAB_CLOCK), false) view_pager.setCurrentItem(intent.getIntExtra(OPEN_TAB, TAB_CLOCK), false)
} }
} }
@ -153,27 +153,30 @@ class MainActivity : SimpleActivity() {
contentResolver.takePersistableUriPermission(uri, takeFlags) contentResolver.takePersistableUriPermission(uri, takeFlags)
} }
when (viewpager.currentItem) { when (view_pager.currentItem) {
TAB_TIMER -> (viewpager.adapter as? ViewPagerAdapter)?.updateTimerAlarmSound(newAlarmSound) TAB_ALARM -> getViewPagerAdapter()?.updateAlarmTabAlarmSound(newAlarmSound)
TAB_TIMER -> getViewPagerAdapter()?.updateTimerTabAlarmSound(newAlarmSound)
} }
} }
private fun getViewPagerAdapter() = view_pager.adapter as? ViewPagerAdapter
private fun initFragments() { private fun initFragments() {
viewpager.adapter = ViewPagerAdapter(supportFragmentManager) view_pager.adapter = ViewPagerAdapter(supportFragmentManager)
viewpager.onPageChangeListener { view_pager.onPageChangeListener {
main_tabs_holder.getTabAt(it)?.select() main_tabs_holder.getTabAt(it)?.select()
} }
val tabToOpen = intent.getIntExtra(OPEN_TAB, config.lastUsedViewPagerPage) val tabToOpen = intent.getIntExtra(OPEN_TAB, config.lastUsedViewPagerPage)
intent.removeExtra(OPEN_TAB) intent.removeExtra(OPEN_TAB)
viewpager.currentItem = tabToOpen view_pager.currentItem = tabToOpen
viewpager.offscreenPageLimit = TABS_COUNT - 1 view_pager.offscreenPageLimit = TABS_COUNT - 1
main_tabs_holder.onTabSelectionChanged( main_tabs_holder.onTabSelectionChanged(
tabUnselectedAction = { tabUnselectedAction = {
it.icon?.applyColorFilter(config.textColor) it.icon?.applyColorFilter(config.textColor)
}, },
tabSelectedAction = { tabSelectedAction = {
viewpager.currentItem = it.position view_pager.currentItem = it.position
it.icon?.applyColorFilter(getAdjustedPrimaryColor()) it.icon?.applyColorFilter(getAdjustedPrimaryColor())
} }
) )

View File

@ -9,6 +9,7 @@ import com.simplemobiletools.clock.fragments.ClockFragment
import com.simplemobiletools.clock.fragments.StopwatchFragment import com.simplemobiletools.clock.fragments.StopwatchFragment
import com.simplemobiletools.clock.fragments.TimerFragment import com.simplemobiletools.clock.fragments.TimerFragment
import com.simplemobiletools.clock.helpers.TABS_COUNT import com.simplemobiletools.clock.helpers.TABS_COUNT
import com.simplemobiletools.clock.helpers.TAB_ALARM
import com.simplemobiletools.clock.helpers.TAB_TIMER import com.simplemobiletools.clock.helpers.TAB_TIMER
import com.simplemobiletools.clock.models.AlarmSound import com.simplemobiletools.clock.models.AlarmSound
@ -36,7 +37,11 @@ class ViewPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {
else -> throw RuntimeException("Trying to fetch unknown fragment id $position") else -> throw RuntimeException("Trying to fetch unknown fragment id $position")
} }
fun updateTimerAlarmSound(alarmSound: AlarmSound) { fun updateAlarmTabAlarmSound(alarmSound: AlarmSound) {
(fragments[TAB_ALARM] as AlarmFragment).updateAlarmSound(alarmSound)
}
fun updateTimerTabAlarmSound(alarmSound: AlarmSound) {
(fragments[TAB_TIMER] as TimerFragment).updateAlarmSound(alarmSound) (fragments[TAB_TIMER] as TimerFragment).updateAlarmSound(alarmSound)
} }
} }

View File

@ -12,6 +12,7 @@ import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.extensions.dbHelper import com.simplemobiletools.clock.extensions.dbHelper
import com.simplemobiletools.clock.extensions.getFormattedTime import com.simplemobiletools.clock.extensions.getFormattedTime
import com.simplemobiletools.clock.models.Alarm import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.clock.models.AlarmSound
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import kotlinx.android.synthetic.main.dialog_edit_alarm.view.* import kotlinx.android.synthetic.main.dialog_edit_alarm.view.*
@ -32,9 +33,7 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba
edit_alarm_sound.setOnClickListener { edit_alarm_sound.setOnClickListener {
SelectAlarmSoundDialog(activity, alarm.soundUri, AudioManager.STREAM_ALARM) { SelectAlarmSoundDialog(activity, alarm.soundUri, AudioManager.STREAM_ALARM) {
if (it != null) { if (it != null) {
alarm.soundTitle = it.title alarmSoundUpdated(it)
alarm.soundUri = it.uri
edit_alarm_sound.text = it.title
} }
} }
} }
@ -123,4 +122,14 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba
drawable.applyColorFilter(textColor) drawable.applyColorFilter(textColor)
return drawable return drawable
} }
private fun alarmSoundUpdated(alarmSound: AlarmSound) {
alarm.soundTitle = alarmSound.title
alarm.soundUri = alarmSound.uri
view.edit_alarm_sound.text = alarmSound.title
}
fun updateSelectedAlarmSound(alarmSound: AlarmSound) {
alarmSoundUpdated(alarmSound)
}
} }

View File

@ -13,6 +13,7 @@ import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.clock.helpers.DEFAULT_ALARM_MINUTES import com.simplemobiletools.clock.helpers.DEFAULT_ALARM_MINUTES
import com.simplemobiletools.clock.interfaces.ToggleAlarmInterface import com.simplemobiletools.clock.interfaces.ToggleAlarmInterface
import com.simplemobiletools.clock.models.Alarm import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.clock.models.AlarmSound
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.extensions.updateTextColors
import kotlinx.android.synthetic.main.fragment_alarm.view.* import kotlinx.android.synthetic.main.fragment_alarm.view.*
@ -20,6 +21,7 @@ import java.util.*
class AlarmFragment : Fragment(), ToggleAlarmInterface { class AlarmFragment : Fragment(), ToggleAlarmInterface {
private var alarms = ArrayList<Alarm>() private var alarms = ArrayList<Alarm>()
private var currentEditAlarmDialog: EditAlarmDialog? = null
private var storedTextColor = 0 private var storedTextColor = 0
@ -79,10 +81,12 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
} }
private fun openEditAlarm(alarm: Alarm) { private fun openEditAlarm(alarm: Alarm) {
EditAlarmDialog(activity as SimpleActivity, alarm) { currentEditAlarmDialog = EditAlarmDialog(activity as SimpleActivity, alarm) {
currentEditAlarmDialog = null
setupAlarms() setupAlarms()
checkAlarmState(alarm) checkAlarmState(alarm)
} }
} }
override fun alarmToggled(id: Int, isEnabled: Boolean) { override fun alarmToggled(id: Int, isEnabled: Boolean) {
@ -103,4 +107,8 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
context!!.cancelAlarmClock(alarm) context!!.cancelAlarmClock(alarm)
} }
} }
fun updateAlarmSound(alarmSound: AlarmSound) {
currentEditAlarmDialog?.updateSelectedAlarmSound(alarmSound)
}
} }

View File

@ -42,7 +42,7 @@
</android.support.design.widget.TabLayout> </android.support.design.widget.TabLayout>
<com.booking.rtlviewpager.RtlViewPager <com.booking.rtlviewpager.RtlViewPager
android:id="@+id/viewpager" android:id="@+id/view_pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_below="@+id/main_tabs_holder"/> android:layout_below="@+id/main_tabs_holder"/>