ntfy-android/app/src/main/java/io/heckel/ntfy/ui/NotificationFragment.kt

97 lines
3.6 KiB
Kotlin
Raw Normal View History

2021-11-22 08:54:13 +13:00
package io.heckel.ntfy.ui
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.os.Bundle
2021-11-23 09:45:43 +13:00
import android.widget.RadioButton
2021-11-22 08:54:13 +13:00
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import io.heckel.ntfy.R
import io.heckel.ntfy.data.Database
import io.heckel.ntfy.data.Repository
import kotlinx.coroutines.Dispatchers
2021-11-23 09:45:43 +13:00
import kotlinx.coroutines.delay
2021-11-22 08:54:13 +13:00
import kotlinx.coroutines.launch
2021-11-23 09:45:43 +13:00
import java.util.*
2021-11-22 08:54:13 +13:00
class NotificationFragment : DialogFragment() {
private lateinit var repository: Repository
private lateinit var settingsListener: NotificationSettingsListener
2021-11-23 09:45:43 +13:00
private lateinit var muteFor30minButton: RadioButton
private lateinit var muteFor1hButton: RadioButton
private lateinit var muteFor2hButton: RadioButton
private lateinit var muteFor8hButton: RadioButton
private lateinit var muteUntilTomorrowButton: RadioButton
private lateinit var muteForeverButton: RadioButton
2021-11-22 08:54:13 +13:00
interface NotificationSettingsListener {
2021-11-23 09:45:43 +13:00
fun onNotificationMutedUntilChanged(mutedUntilTimestamp: Long)
2021-11-22 08:54:13 +13:00
}
override fun onAttach(context: Context) {
super.onAttach(context)
settingsListener = activity as NotificationSettingsListener
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
if (activity == null) {
throw IllegalStateException("Activity cannot be null")
}
// Dependencies
2021-11-23 09:45:43 +13:00
val database = Database.getInstance(requireActivity().applicationContext)
val sharedPrefs = requireActivity().getSharedPreferences(Repository.SHARED_PREFS_ID, Context.MODE_PRIVATE)
repository = Repository.getInstance(sharedPrefs, database.subscriptionDao(), database.notificationDao())
2021-11-22 08:54:13 +13:00
// Build root view
2021-11-23 09:45:43 +13:00
val view = requireActivity().layoutInflater.inflate(R.layout.fragment_notification_dialog, null)
2021-11-22 08:54:13 +13:00
2021-11-23 09:45:43 +13:00
muteFor30minButton = view.findViewById(R.id.notification_dialog_30min)
muteFor30minButton.setOnClickListener { onClickMinutes(30) }
muteFor1hButton = view.findViewById(R.id.notification_dialog_1h)
muteFor1hButton.setOnClickListener { onClickMinutes(60) }
2021-11-22 08:54:13 +13:00
2021-11-23 09:45:43 +13:00
muteFor2hButton = view.findViewById(R.id.notification_dialog_2h)
muteFor2hButton.setOnClickListener { onClickMinutes(2 * 60) }
muteFor8hButton = view.findViewById(R.id.notification_dialog_8h)
muteFor8hButton.setOnClickListener{ onClickMinutes(8 * 60) }
muteUntilTomorrowButton = view.findViewById(R.id.notification_dialog_tomorrow)
muteUntilTomorrowButton.setOnClickListener {
val date = Calendar.getInstance()
date.add(Calendar.DAY_OF_MONTH, 1)
date.set(Calendar.HOUR_OF_DAY, 8)
date.set(Calendar.MINUTE, 30)
date.set(Calendar.SECOND, 0)
date.set(Calendar.MILLISECOND, 0)
onClick(date.timeInMillis/1000)
2021-11-22 08:54:13 +13:00
}
2021-11-23 09:45:43 +13:00
muteForeverButton = view.findViewById(R.id.notification_dialog_forever)
muteForeverButton.setOnClickListener{ onClick(1) }
return AlertDialog.Builder(activity)
.setView(view)
.create()
}
private fun onClickMinutes(minutes: Int) {
onClick(System.currentTimeMillis()/1000 + minutes * 60)
2021-11-22 08:54:13 +13:00
}
2021-11-23 09:45:43 +13:00
private fun onClick(mutedUntilTimestamp: Long) {
lifecycleScope.launch(Dispatchers.Main) {
delay(150) // Another hack: Let the animation finish before dismissing the window
settingsListener.onNotificationMutedUntilChanged(mutedUntilTimestamp)
dismiss()
}
}
2021-11-22 08:54:13 +13:00
companion object {
const val TAG = "NtfyNotificationFragment"
}
}