mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Compare commits
7 Commits
fix/stackw
...
feature/ca
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e189d17fd5 | ||
|
|
30c39853e9 | ||
|
|
c1fa6c46ee | ||
|
|
6e4ed3837f | ||
|
|
cb65604581 | ||
|
|
2db4491328 | ||
|
|
25a3509988 |
@@ -26,6 +26,7 @@ import android.view.View
|
||||
import android.view.View.GONE
|
||||
import android.view.View.VISIBLE
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.activities.common.dialogs.MultipleDialogsHandler.Companion.dismissCurrent
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.NO
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.SKIP
|
||||
import org.isoron.uhabits.core.models.Entry.Companion.UNKNOWN
|
||||
@@ -35,7 +36,6 @@ import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.databinding.CheckmarkPopupBinding
|
||||
import org.isoron.uhabits.utils.InterfaceUtils.getFontAwesome
|
||||
import org.isoron.uhabits.utils.dimBehind
|
||||
import org.isoron.uhabits.utils.dismissCurrentAndShow
|
||||
import org.isoron.uhabits.utils.dp
|
||||
import org.isoron.uhabits.utils.sres
|
||||
|
||||
@@ -118,7 +118,8 @@ class CheckmarkPopup(
|
||||
view.unknownBtn.setOnClickListener { onClick(UNKNOWN) }
|
||||
dialog.setCanceledOnTouchOutside(true)
|
||||
dialog.dimBehind()
|
||||
dialog.dismissCurrentAndShow()
|
||||
dialog.dismissCurrent()
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
fun save() {
|
||||
|
||||
@@ -18,7 +18,10 @@
|
||||
*/
|
||||
package org.isoron.uhabits.activities.common.dialogs
|
||||
|
||||
import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import com.android.colorpicker.ColorPickerDialog
|
||||
import org.isoron.uhabits.activities.common.dialogs.MultipleDialogsHandler.Companion.dismissCurrent
|
||||
import org.isoron.uhabits.core.ui.callbacks.OnColorPickedCallback
|
||||
import org.isoron.uhabits.utils.toPaletteColor
|
||||
|
||||
@@ -32,4 +35,10 @@ class ColorPickerDialog : ColorPickerDialog() {
|
||||
callback.onColorPicked(pc)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
val dialog = super.onCreateDialog(savedInstanceState)
|
||||
dialog.dismissCurrent()
|
||||
return dialog
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.activities.common.dialogs.MultipleDialogsHandler.Companion.dismissCurrent
|
||||
import org.isoron.uhabits.core.ui.callbacks.OnConfirmedCallback
|
||||
import org.isoron.uhabits.inject.ActivityContext
|
||||
|
||||
@@ -45,5 +46,7 @@ class ConfirmDeleteDialog(
|
||||
BUTTON_NEGATIVE,
|
||||
res.getString(R.string.no)
|
||||
) { dialog: DialogInterface?, which: Int -> }
|
||||
|
||||
this.dismissCurrent()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatDialogFragment
|
||||
import kotlinx.android.synthetic.main.frequency_picker_dialog.view.*
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.activities.common.dialogs.MultipleDialogsHandler.Companion.dismissCurrent
|
||||
|
||||
class FrequencyPickerDialog(
|
||||
var freqNumerator: Int,
|
||||
@@ -111,10 +112,12 @@ class FrequencyPickerDialog(
|
||||
if (hasFocus) check(contentView.xTimesPerYDaysRadioButton)
|
||||
}
|
||||
|
||||
return AlertDialog.Builder(requireActivity())
|
||||
val dialog = AlertDialog.Builder(requireActivity())
|
||||
.setView(contentView)
|
||||
.setPositiveButton(R.string.save) { _, _ -> onSaveClicked() }
|
||||
.create()
|
||||
dialog.dismissCurrent()
|
||||
return dialog
|
||||
}
|
||||
|
||||
private fun addBeforeAfterText(
|
||||
@@ -149,10 +152,8 @@ class FrequencyPickerDialog(
|
||||
}
|
||||
contentView.xTimesPerYDaysRadioButton.isChecked -> {
|
||||
if (contentView.xTimesPerYDaysXTextView.text.isNotEmpty() && contentView.xTimesPerYDaysYTextView.text.isNotEmpty()) {
|
||||
numerator =
|
||||
Integer.parseInt(contentView.xTimesPerYDaysXTextView.text.toString())
|
||||
denominator =
|
||||
Integer.parseInt(contentView.xTimesPerYDaysYTextView.text.toString())
|
||||
numerator = Integer.parseInt(contentView.xTimesPerYDaysXTextView.text.toString())
|
||||
denominator = Integer.parseInt(contentView.xTimesPerYDaysYTextView.text.toString())
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.isoron.uhabits.activities.common.dialogs
|
||||
|
||||
import android.app.Dialog
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
class MultipleDialogsHandler {
|
||||
companion object {
|
||||
var currentDialog: WeakReference<Dialog> = WeakReference(null)
|
||||
|
||||
fun Dialog.dismissCurrent() {
|
||||
currentDialog.get()?.dismiss()
|
||||
currentDialog = WeakReference(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,11 +27,11 @@ import android.view.MotionEvent.ACTION_DOWN
|
||||
import android.view.View
|
||||
import android.view.View.GONE
|
||||
import android.view.View.VISIBLE
|
||||
import org.isoron.uhabits.activities.common.dialogs.MultipleDialogsHandler.Companion.dismissCurrent
|
||||
import org.isoron.uhabits.core.models.Entry
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.databinding.CheckmarkPopupBinding
|
||||
import org.isoron.uhabits.utils.dimBehind
|
||||
import org.isoron.uhabits.utils.dismissCurrentAndShow
|
||||
import org.isoron.uhabits.utils.dp
|
||||
import org.isoron.uhabits.utils.requestFocusWithKeyboard
|
||||
import java.text.DecimalFormat
|
||||
@@ -104,7 +104,8 @@ class NumberPopup(
|
||||
view.value.requestFocusWithKeyboard()
|
||||
dialog.setCanceledOnTouchOutside(true)
|
||||
dialog.dimBehind()
|
||||
dialog.dismissCurrentAndShow()
|
||||
dialog.dismissCurrent()
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
fun save() {
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.os.Bundle
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatDialogFragment
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.activities.common.dialogs.MultipleDialogsHandler.Companion.dismissCurrent
|
||||
import org.isoron.uhabits.core.models.WeekdayList
|
||||
import org.isoron.uhabits.core.utils.DateUtils
|
||||
import java.util.Calendar
|
||||
@@ -74,7 +75,9 @@ class WeekdayPickerDialog :
|
||||
android.R.string.cancel
|
||||
) { _: DialogInterface?, _: Int -> dismiss() }
|
||||
|
||||
return builder.create()
|
||||
val dialog = builder.create()
|
||||
dialog.dismissCurrent()
|
||||
return dialog
|
||||
}
|
||||
|
||||
fun setListener(listener: OnWeekdaysPickedListener?) {
|
||||
|
||||
@@ -46,6 +46,7 @@ import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.activities.AndroidThemeSwitcher
|
||||
import org.isoron.uhabits.activities.common.dialogs.ColorPickerDialogFactory
|
||||
import org.isoron.uhabits.activities.common.dialogs.FrequencyPickerDialog
|
||||
import org.isoron.uhabits.activities.common.dialogs.MultipleDialogsHandler.Companion.dismissCurrent
|
||||
import org.isoron.uhabits.activities.common.dialogs.WeekdayPickerDialog
|
||||
import org.isoron.uhabits.core.commands.CommandRunner
|
||||
import org.isoron.uhabits.core.commands.CreateHabitCommand
|
||||
@@ -59,7 +60,6 @@ import org.isoron.uhabits.core.models.Reminder
|
||||
import org.isoron.uhabits.core.models.WeekdayList
|
||||
import org.isoron.uhabits.databinding.ActivityEditHabitBinding
|
||||
import org.isoron.uhabits.utils.ColorUtils
|
||||
import org.isoron.uhabits.utils.dismissCurrentAndShow
|
||||
import org.isoron.uhabits.utils.formatTime
|
||||
import org.isoron.uhabits.utils.toFormattedString
|
||||
|
||||
@@ -157,23 +157,23 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
|
||||
val colorPickerDialogFactory = ColorPickerDialogFactory(this)
|
||||
binding.colorButton.setOnClickListener {
|
||||
val picker = colorPickerDialogFactory.create(color, themeSwitcher.currentTheme)
|
||||
picker.setListener { paletteColor ->
|
||||
val dialog = colorPickerDialogFactory.create(color, themeSwitcher.currentTheme)
|
||||
dialog.setListener { paletteColor ->
|
||||
this.color = paletteColor
|
||||
updateColors()
|
||||
}
|
||||
picker.dismissCurrentAndShow(supportFragmentManager, "colorPicker")
|
||||
dialog.show(supportFragmentManager, "colorPicker")
|
||||
}
|
||||
|
||||
populateFrequency()
|
||||
binding.booleanFrequencyPicker.setOnClickListener {
|
||||
val picker = FrequencyPickerDialog(freqNum, freqDen)
|
||||
picker.onFrequencyPicked = { num, den ->
|
||||
val dialog = FrequencyPickerDialog(freqNum, freqDen)
|
||||
dialog.onFrequencyPicked = { num, den ->
|
||||
freqNum = num
|
||||
freqDen = den
|
||||
populateFrequency()
|
||||
}
|
||||
picker.dismissCurrentAndShow(supportFragmentManager, "frequencyPicker")
|
||||
dialog.show(supportFragmentManager, "frequencyPicker")
|
||||
}
|
||||
|
||||
populateTargetType()
|
||||
@@ -191,7 +191,8 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
dialog.dismiss()
|
||||
}
|
||||
val dialog = builder.create()
|
||||
dialog.dismissCurrentAndShow()
|
||||
dialog.dismissCurrent()
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
binding.numericalFrequencyPicker.setOnClickListener {
|
||||
@@ -237,7 +238,7 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
is24HourMode,
|
||||
androidColor
|
||||
)
|
||||
dialog.dismissCurrentAndShow(supportFragmentManager, "timePicker")
|
||||
dialog.show(supportFragmentManager, "timePicker")
|
||||
}
|
||||
|
||||
binding.reminderDatePicker.setOnClickListener {
|
||||
@@ -249,7 +250,7 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
populateReminder()
|
||||
}
|
||||
dialog.setSelectedDays(reminderDays)
|
||||
dialog.dismissCurrentAndShow(supportFragmentManager, "dayPicker")
|
||||
dialog.show(supportFragmentManager, "dayPicker")
|
||||
}
|
||||
|
||||
binding.buttonSave.setOnClickListener {
|
||||
|
||||
@@ -21,7 +21,6 @@ package org.isoron.uhabits.activities.habits.list
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
@@ -85,6 +84,7 @@ class ListHabitsActivity : AppCompatActivity(), Preferences.Listener {
|
||||
Thread.setDefaultUncaughtExceptionHandler(BaseExceptionHandler(this))
|
||||
component.listHabitsBehavior.onStartup()
|
||||
setContentView(rootView)
|
||||
parseIntents()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
@@ -100,16 +100,11 @@ class ListHabitsActivity : AppCompatActivity(), Preferences.Listener {
|
||||
rootView.postInvalidate()
|
||||
midnightTimer.onResume()
|
||||
taskRunner.run {
|
||||
try {
|
||||
AutoBackup(this@ListHabitsActivity).run()
|
||||
} catch (e: Exception) {
|
||||
Log.e("ListHabitActivity", "AutoBackup task failed", e)
|
||||
}
|
||||
AutoBackup(this@ListHabitsActivity).run()
|
||||
}
|
||||
if (prefs.theme == THEME_DARK && prefs.isPureBlackEnabled != pureBlack) {
|
||||
restartWithFade(ListHabitsActivity::class.java)
|
||||
}
|
||||
parseIntents()
|
||||
super.onResume()
|
||||
}
|
||||
|
||||
@@ -129,7 +124,6 @@ class ListHabitsActivity : AppCompatActivity(), Preferences.Listener {
|
||||
}
|
||||
|
||||
private fun parseIntents() {
|
||||
if (intent == null) return
|
||||
if (intent.action == ACTION_EDIT) {
|
||||
val habitId = intent.extras?.getLong("habit")
|
||||
val timestamp = intent.extras?.getLong("timestamp")
|
||||
@@ -138,12 +132,6 @@ class ListHabitsActivity : AppCompatActivity(), Preferences.Listener {
|
||||
component.listHabitsBehavior.onEdit(habit, Timestamp(timestamp))
|
||||
}
|
||||
}
|
||||
intent = null
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent?) {
|
||||
super.onNewIntent(intent)
|
||||
setIntent(intent)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -64,7 +64,6 @@ import org.isoron.uhabits.tasks.ImportDataTask
|
||||
import org.isoron.uhabits.tasks.ImportDataTaskFactory
|
||||
import org.isoron.uhabits.utils.copyTo
|
||||
import org.isoron.uhabits.utils.currentTheme
|
||||
import org.isoron.uhabits.utils.dismissCurrentAndShow
|
||||
import org.isoron.uhabits.utils.restartWithFade
|
||||
import org.isoron.uhabits.utils.showMessage
|
||||
import org.isoron.uhabits.utils.showSendEmailScreen
|
||||
@@ -164,7 +163,7 @@ class ListHabitsScreen
|
||||
}
|
||||
|
||||
override fun showDeleteConfirmationScreen(callback: OnConfirmedCallback, quantity: Int) {
|
||||
ConfirmDeleteDialog(activity, callback, quantity).dismissCurrentAndShow()
|
||||
ConfirmDeleteDialog(activity, callback, quantity).show()
|
||||
}
|
||||
|
||||
override fun showEditHabitsScreen(selected: List<Habit>) {
|
||||
@@ -225,7 +224,7 @@ class ListHabitsScreen
|
||||
override fun showColorPicker(defaultColor: PaletteColor, callback: OnColorPickedCallback) {
|
||||
val picker = colorPickerFactory.create(defaultColor, themeSwitcher.currentTheme!!)
|
||||
picker.setListener(callback)
|
||||
picker.dialog?.dismissCurrentAndShow()
|
||||
picker.show(activity.supportFragmentManager, "picker")
|
||||
}
|
||||
|
||||
override fun showNumberPopup(
|
||||
|
||||
@@ -50,7 +50,6 @@ import org.isoron.uhabits.core.ui.screens.habits.show.ShowHabitPresenter
|
||||
import org.isoron.uhabits.core.ui.views.OnDateClickedListener
|
||||
import org.isoron.uhabits.intents.IntentFactory
|
||||
import org.isoron.uhabits.utils.currentTheme
|
||||
import org.isoron.uhabits.utils.dismissCurrentAndShow
|
||||
import org.isoron.uhabits.utils.showMessage
|
||||
import org.isoron.uhabits.utils.showSendFileScreen
|
||||
import org.isoron.uhabits.widgets.WidgetUpdater
|
||||
@@ -229,7 +228,7 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener {
|
||||
}
|
||||
|
||||
override fun showDeleteConfirmationScreen(callback: OnConfirmedCallback) {
|
||||
ConfirmDeleteDialog(this@ShowHabitActivity, callback, 1).dismissCurrentAndShow()
|
||||
ConfirmDeleteDialog(this@ShowHabitActivity, callback, 1).show()
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
|
||||
@@ -21,16 +21,13 @@ package org.isoron.uhabits.intents
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.app.PendingIntent.FLAG_IMMUTABLE
|
||||
import android.app.PendingIntent.FLAG_MUTABLE
|
||||
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
|
||||
import android.app.PendingIntent.getActivity
|
||||
import android.app.PendingIntent.getBroadcast
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import org.isoron.uhabits.activities.habits.list.ListHabitsActivity
|
||||
import org.isoron.uhabits.activities.habits.show.ShowHabitActivity
|
||||
import org.isoron.uhabits.core.AppScope
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.isoron.uhabits.core.models.Timestamp
|
||||
@@ -92,20 +89,6 @@ class PendingIntentFactory
|
||||
)
|
||||
.getPendingIntent(0, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT)!!
|
||||
|
||||
fun showHabitTemplate(): PendingIntent {
|
||||
return getActivity(
|
||||
context,
|
||||
0,
|
||||
Intent(context, ShowHabitActivity::class.java),
|
||||
getIntentTemplateFlags()
|
||||
)
|
||||
}
|
||||
|
||||
fun showHabitFillIn(habit: Habit) =
|
||||
Intent().apply {
|
||||
data = Uri.parse(habit.uriString)
|
||||
}
|
||||
|
||||
fun showReminder(
|
||||
habit: Habit,
|
||||
reminderTime: Long?,
|
||||
@@ -159,7 +142,7 @@ class PendingIntentFactory
|
||||
fun showNumberPicker(habit: Habit, timestamp: Timestamp): PendingIntent? {
|
||||
return getActivity(
|
||||
context,
|
||||
(habit.id!! % Integer.MAX_VALUE).toInt() + 1,
|
||||
0,
|
||||
Intent(context, ListHabitsActivity::class.java).apply {
|
||||
action = ListHabitsActivity.ACTION_EDIT
|
||||
putExtra("habit", habit.id)
|
||||
@@ -168,43 +151,4 @@ class PendingIntentFactory
|
||||
FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT
|
||||
)
|
||||
}
|
||||
|
||||
fun showNumberPickerTemplate(): PendingIntent {
|
||||
return getActivity(
|
||||
context,
|
||||
1,
|
||||
Intent(context, ListHabitsActivity::class.java).apply {
|
||||
action = ListHabitsActivity.ACTION_EDIT
|
||||
},
|
||||
getIntentTemplateFlags()
|
||||
)
|
||||
}
|
||||
|
||||
fun showNumberPickerFillIn(habit: Habit, timestamp: Timestamp) = Intent().apply {
|
||||
putExtra("habit", habit.id)
|
||||
putExtra("timestamp", timestamp.unixTime)
|
||||
}
|
||||
|
||||
private fun getIntentTemplateFlags(): Int {
|
||||
var flags = 0
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
flags = flags or FLAG_MUTABLE
|
||||
}
|
||||
return flags
|
||||
}
|
||||
|
||||
fun toggleCheckmarkTemplate(): PendingIntent =
|
||||
getBroadcast(
|
||||
context,
|
||||
2,
|
||||
Intent(context, WidgetReceiver::class.java).apply {
|
||||
action = WidgetReceiver.ACTION_TOGGLE_REPETITION
|
||||
},
|
||||
getIntentTemplateFlags()
|
||||
)
|
||||
|
||||
fun toggleCheckmarkFillIn(habit: Habit, timestamp: Timestamp) = Intent().apply {
|
||||
data = Uri.parse(habit.uriString)
|
||||
putExtra("timestamp", timestamp.unixTime)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package org.isoron.uhabits.utils
|
||||
|
||||
import android.app.Dialog
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
var currentDialog: WeakReference<Dialog> = WeakReference(null)
|
||||
|
||||
fun Dialog.dismissCurrentAndShow() {
|
||||
currentDialog.get()?.dismiss()
|
||||
currentDialog = WeakReference(this)
|
||||
show()
|
||||
}
|
||||
|
||||
fun DialogFragment.dismissCurrentAndShow(fragmentManager: FragmentManager, tag: String) {
|
||||
currentDialog.get()?.dismiss()
|
||||
show(fragmentManager, tag)
|
||||
fragmentManager.executePendingTransactions()
|
||||
currentDialog = WeakReference(this.dialog)
|
||||
}
|
||||
@@ -73,10 +73,6 @@ class StackWidget(
|
||||
StackWidgetType.getStackWidgetAdapterViewId(widgetType),
|
||||
StackWidgetType.getStackWidgetEmptyViewId(widgetType)
|
||||
)
|
||||
remoteViews.setPendingIntentTemplate(
|
||||
StackWidgetType.getStackWidgetAdapterViewId(widgetType),
|
||||
StackWidgetType.getPendingIntentTemplate(pendingIntentFactory, widgetType, habits)
|
||||
)
|
||||
return remoteViews
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,14 +29,11 @@ import android.widget.RemoteViewsService
|
||||
import android.widget.RemoteViewsService.RemoteViewsFactory
|
||||
import org.isoron.platform.utils.StringUtils.Companion.splitLongs
|
||||
import org.isoron.uhabits.HabitsApplication
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.isoron.uhabits.core.models.HabitNotFoundException
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday
|
||||
import org.isoron.uhabits.intents.IntentFactory
|
||||
import org.isoron.uhabits.intents.PendingIntentFactory
|
||||
import org.isoron.uhabits.utils.InterfaceUtils.dpToPixels
|
||||
import java.util.ArrayList
|
||||
|
||||
class StackWidgetService : RemoteViewsService() {
|
||||
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
|
||||
@@ -57,6 +54,7 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
|
||||
)
|
||||
private val habitIds: LongArray
|
||||
private val widgetType: StackWidgetType
|
||||
private var remoteViews = ArrayList<RemoteViews>()
|
||||
override fun onCreate() {}
|
||||
override fun onDestroy() {}
|
||||
override fun getCount(): Int {
|
||||
@@ -87,26 +85,8 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
|
||||
}
|
||||
|
||||
override fun getViewAt(position: Int): RemoteViews? {
|
||||
Log.i("StackRemoteViewsFactory", "getViewAt $position started")
|
||||
if (position < 0 || position >= habitIds.size) return null
|
||||
val app = context.applicationContext as HabitsApplication
|
||||
val prefs = app.component.preferences
|
||||
val habitList = app.component.habitList
|
||||
val options = AppWidgetManager.getInstance(context).getAppWidgetOptions(widgetId)
|
||||
if (Looper.myLooper() == null) Looper.prepare()
|
||||
val habits = habitIds.map { habitList.getById(it) ?: throw HabitNotFoundException() }
|
||||
val h = habits[position]
|
||||
val widget = constructWidget(h, prefs)
|
||||
widget.setDimensions(getDimensionsFromOptions(context, options))
|
||||
val landscapeViews = widget.landscapeRemoteViews
|
||||
val portraitViews = widget.portraitRemoteViews
|
||||
val factory = PendingIntentFactory(context, IntentFactory())
|
||||
val intent = StackWidgetType.getIntentFillIn(factory, widgetType, h, habits, getToday())
|
||||
landscapeViews.setOnClickFillInIntent(R.id.button, intent)
|
||||
portraitViews.setOnClickFillInIntent(R.id.button, intent)
|
||||
val remoteViews = RemoteViews(landscapeViews, portraitViews)
|
||||
Log.i("StackRemoteViewsFactory", "getViewAt $position ended")
|
||||
return remoteViews
|
||||
Log.i("StackRemoteViewsFactory", "getViewAt $position")
|
||||
return if (0 <= position && position < remoteViews.size) remoteViews[position] else null
|
||||
}
|
||||
|
||||
private fun constructWidget(
|
||||
@@ -151,6 +131,24 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
|
||||
}
|
||||
|
||||
override fun onDataSetChanged() {
|
||||
Log.i("StackRemoteViewsFactory", "onDataSetChanged started")
|
||||
val app = context.applicationContext as HabitsApplication
|
||||
val prefs = app.component.preferences
|
||||
val habitList = app.component.habitList
|
||||
val options = AppWidgetManager.getInstance(context).getAppWidgetOptions(widgetId)
|
||||
val newRemoteViews = ArrayList<RemoteViews>()
|
||||
if (Looper.myLooper() == null) Looper.prepare()
|
||||
for (id in habitIds) {
|
||||
val h = habitList.getById(id) ?: throw HabitNotFoundException()
|
||||
val widget = constructWidget(h, prefs)
|
||||
widget.setDimensions(getDimensionsFromOptions(context, options))
|
||||
val landscapeViews = widget.landscapeRemoteViews
|
||||
val portraitViews = widget.portraitRemoteViews
|
||||
newRemoteViews.add(RemoteViews(landscapeViews, portraitViews))
|
||||
Log.i("StackRemoteViewsFactory", "onDataSetChanged constructed widget $id")
|
||||
}
|
||||
remoteViews = newRemoteViews
|
||||
Log.i("StackRemoteViewsFactory", "onDataSetChanged ended")
|
||||
}
|
||||
|
||||
init {
|
||||
|
||||
@@ -18,12 +18,7 @@
|
||||
*/
|
||||
package org.isoron.uhabits.widgets
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import org.isoron.uhabits.core.models.Timestamp
|
||||
import org.isoron.uhabits.intents.PendingIntentFactory
|
||||
import java.lang.IllegalStateException
|
||||
|
||||
enum class StackWidgetType(val value: Int) {
|
||||
@@ -78,39 +73,5 @@ enum class StackWidgetType(val value: Int) {
|
||||
else -> throw IllegalStateException()
|
||||
}
|
||||
}
|
||||
|
||||
fun getPendingIntentTemplate(
|
||||
factory: PendingIntentFactory,
|
||||
widgetType: StackWidgetType,
|
||||
habits: List<Habit>
|
||||
): PendingIntent {
|
||||
val containsNumerical = habits.any { it.isNumerical }
|
||||
return when (widgetType) {
|
||||
CHECKMARK -> if (containsNumerical) {
|
||||
factory.showNumberPickerTemplate()
|
||||
} else {
|
||||
factory.toggleCheckmarkTemplate()
|
||||
}
|
||||
FREQUENCY, SCORE, HISTORY, STREAKS, TARGET -> factory.showHabitTemplate()
|
||||
}
|
||||
}
|
||||
|
||||
fun getIntentFillIn(
|
||||
factory: PendingIntentFactory,
|
||||
widgetType: StackWidgetType,
|
||||
habit: Habit,
|
||||
allHabitsInStackWidget: List<Habit>,
|
||||
timestamp: Timestamp
|
||||
): Intent {
|
||||
val containsNumerical = allHabitsInStackWidget.any { it.isNumerical }
|
||||
return when (widgetType) {
|
||||
CHECKMARK -> if (containsNumerical) {
|
||||
factory.showNumberPickerFillIn(habit, timestamp)
|
||||
} else {
|
||||
factory.toggleCheckmarkFillIn(habit, timestamp)
|
||||
}
|
||||
FREQUENCY, SCORE, HISTORY, STREAKS, TARGET -> factory.showHabitFillIn(habit)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,12 +25,6 @@ class StringUtils {
|
||||
|
||||
fun joinLongs(values: LongArray): String = values.joinToString(separator = ",")
|
||||
|
||||
fun splitLongs(str: String): LongArray {
|
||||
return try {
|
||||
str.split(",").map { it.toLong() }.toLongArray()
|
||||
} catch (e: NumberFormatException) {
|
||||
LongArray(0)
|
||||
}
|
||||
}
|
||||
fun splitLongs(str: String): LongArray = str.split(",").map { it.toLong() }.toLongArray()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user