|
|
|
@ -58,7 +58,6 @@ import org.tasks.data.UserActivityDao
|
|
|
|
|
import org.tasks.date.DateTimeUtils.toDateTime
|
|
|
|
|
import org.tasks.files.FileHelper
|
|
|
|
|
import org.tasks.location.GeofenceApi
|
|
|
|
|
import org.tasks.preferences.DefaultFilterProvider
|
|
|
|
|
import org.tasks.preferences.PermissionChecker
|
|
|
|
|
import org.tasks.preferences.Preferences
|
|
|
|
|
import org.tasks.time.DateTimeUtils.currentTimeMillis
|
|
|
|
@ -68,33 +67,32 @@ import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
@HiltViewModel
|
|
|
|
|
class TaskEditViewModel @Inject constructor(
|
|
|
|
|
@ApplicationContext private val applicationContext: Context,
|
|
|
|
|
savedStateHandle: SavedStateHandle,
|
|
|
|
|
private val taskDao: TaskDao,
|
|
|
|
|
private val taskDeleter: TaskDeleter,
|
|
|
|
|
private val timerPlugin: TimerPlugin,
|
|
|
|
|
private val permissionChecker: PermissionChecker,
|
|
|
|
|
private val calendarEventProvider: CalendarEventProvider,
|
|
|
|
|
private val gCalHelper: GCalHelper,
|
|
|
|
|
private val taskMover: TaskMover,
|
|
|
|
|
private val locationDao: LocationDao,
|
|
|
|
|
private val geofenceApi: GeofenceApi,
|
|
|
|
|
private val tagDao: TagDao,
|
|
|
|
|
private val tagDataDao: TagDataDao,
|
|
|
|
|
private val preferences: Preferences,
|
|
|
|
|
private val googleTaskDao: GoogleTaskDao,
|
|
|
|
|
private val caldavDao: CaldavDao,
|
|
|
|
|
private val taskCompleter: TaskCompleter,
|
|
|
|
|
private val alarmService: AlarmService,
|
|
|
|
|
private val taskListEvents: TaskListEventBus,
|
|
|
|
|
private val mainActivityEvents: MainActivityEventBus,
|
|
|
|
|
private val firebase: Firebase? = null,
|
|
|
|
|
private val userActivityDao: UserActivityDao,
|
|
|
|
|
private val alarmDao: AlarmDao,
|
|
|
|
|
private val taskAttachmentDao: TaskAttachmentDao,
|
|
|
|
|
private val defaultFilterProvider: DefaultFilterProvider,
|
|
|
|
|
@ApplicationContext private val context: Context,
|
|
|
|
|
savedStateHandle: SavedStateHandle,
|
|
|
|
|
private val taskDao: TaskDao,
|
|
|
|
|
private val taskDeleter: TaskDeleter,
|
|
|
|
|
private val timerPlugin: TimerPlugin,
|
|
|
|
|
private val permissionChecker: PermissionChecker,
|
|
|
|
|
private val calendarEventProvider: CalendarEventProvider,
|
|
|
|
|
private val gCalHelper: GCalHelper,
|
|
|
|
|
private val taskMover: TaskMover,
|
|
|
|
|
private val locationDao: LocationDao,
|
|
|
|
|
private val geofenceApi: GeofenceApi,
|
|
|
|
|
private val tagDao: TagDao,
|
|
|
|
|
private val tagDataDao: TagDataDao,
|
|
|
|
|
private val preferences: Preferences,
|
|
|
|
|
private val googleTaskDao: GoogleTaskDao,
|
|
|
|
|
private val caldavDao: CaldavDao,
|
|
|
|
|
private val taskCompleter: TaskCompleter,
|
|
|
|
|
private val alarmService: AlarmService,
|
|
|
|
|
private val taskListEvents: TaskListEventBus,
|
|
|
|
|
private val mainActivityEvents: MainActivityEventBus,
|
|
|
|
|
private val firebase: Firebase? = null,
|
|
|
|
|
private val userActivityDao: UserActivityDao,
|
|
|
|
|
private val alarmDao: AlarmDao,
|
|
|
|
|
private val taskAttachmentDao: TaskAttachmentDao,
|
|
|
|
|
) : ViewModel() {
|
|
|
|
|
private val resources = applicationContext.resources
|
|
|
|
|
private val resources = context.resources
|
|
|
|
|
private var cleared = false
|
|
|
|
|
|
|
|
|
|
val task: Task = savedStateHandle[TaskEditFragment.EXTRA_TASK]!!
|
|
|
|
@ -146,19 +144,18 @@ class TaskEditViewModel @Inject constructor(
|
|
|
|
|
}
|
|
|
|
|
var selectedCalendar = MutableStateFlow(originalCalendar)
|
|
|
|
|
|
|
|
|
|
val originalList: Filter = runBlocking { defaultFilterProvider.getList(task) }
|
|
|
|
|
val originalList: Filter = savedStateHandle[TaskEditFragment.EXTRA_LIST]!!
|
|
|
|
|
var selectedList = MutableStateFlow(originalList)
|
|
|
|
|
|
|
|
|
|
private val originalLocation: Location? =
|
|
|
|
|
runBlocking { locationDao.getLocation(task, preferences) }
|
|
|
|
|
private var originalLocation: Location? = savedStateHandle[TaskEditFragment.EXTRA_LOCATION]
|
|
|
|
|
var selectedLocation = MutableStateFlow(originalLocation)
|
|
|
|
|
|
|
|
|
|
private val originalTags: List<TagData> = runBlocking { tagDataDao.getTags(task) }
|
|
|
|
|
private val originalTags: List<TagData> =
|
|
|
|
|
savedStateHandle.get<ArrayList<TagData>>(TaskEditFragment.EXTRA_TAGS) ?: emptyList()
|
|
|
|
|
val selectedTags = MutableStateFlow(ArrayList(originalTags))
|
|
|
|
|
|
|
|
|
|
private val originalAttachments: List<TaskAttachment> =
|
|
|
|
|
runBlocking { taskAttachmentDao.getAttachments(task.id) }
|
|
|
|
|
val selectedAttachments = MutableStateFlow(originalAttachments)
|
|
|
|
|
private lateinit var originalAttachments: List<TaskAttachment>
|
|
|
|
|
val selectedAttachments = MutableStateFlow(emptyList<TaskAttachment>())
|
|
|
|
|
|
|
|
|
|
private val originalAlarms: List<Alarm> = if (isNew) {
|
|
|
|
|
ArrayList<Alarm>().apply {
|
|
|
|
@ -176,8 +173,9 @@ class TaskEditViewModel @Inject constructor(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
runBlocking { alarmDao.getAlarms(task) }
|
|
|
|
|
savedStateHandle[TaskEditFragment.EXTRA_ALARMS]!!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var selectedAlarms = MutableStateFlow(originalAlarms)
|
|
|
|
|
|
|
|
|
|
var ringNonstop: Boolean = task.isNotifyModeNonstop
|
|
|
|
@ -228,7 +226,8 @@ class TaskEditViewModel @Inject constructor(
|
|
|
|
|
originalList != selectedList.value ||
|
|
|
|
|
originalLocation != selectedLocation.value ||
|
|
|
|
|
originalTags.toHashSet() != selectedTags.value.toHashSet() ||
|
|
|
|
|
originalAttachments.toHashSet() != selectedAttachments.value.toHashSet() ||
|
|
|
|
|
(::originalAttachments.isInitialized &&
|
|
|
|
|
originalAttachments.toHashSet() != selectedAttachments.value.toHashSet()) ||
|
|
|
|
|
newSubtasks.value.isNotEmpty() ||
|
|
|
|
|
getRingFlags() != when {
|
|
|
|
|
task.isNotifyModeFive -> NOTIFY_MODE_FIVE
|
|
|
|
@ -363,7 +362,10 @@ class TaskEditViewModel @Inject constructor(
|
|
|
|
|
task.modificationDate = now()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selectedAttachments.value.toHashSet() != originalAttachments.toHashSet()) {
|
|
|
|
|
if (
|
|
|
|
|
this@TaskEditViewModel::originalAttachments.isInitialized &&
|
|
|
|
|
selectedAttachments.value.toHashSet() != originalAttachments.toHashSet()
|
|
|
|
|
) {
|
|
|
|
|
originalAttachments
|
|
|
|
|
.minus(selectedAttachments.value.toSet())
|
|
|
|
|
.map { it.remoteId }
|
|
|
|
@ -428,7 +430,7 @@ class TaskEditViewModel @Inject constructor(
|
|
|
|
|
if (isNew) {
|
|
|
|
|
timerPlugin.stopTimer(task)
|
|
|
|
|
originalAttachments.plus(selectedAttachments.value).toSet().takeIf { it.isNotEmpty() }
|
|
|
|
|
?.onEach { FileHelper.delete(applicationContext, it.uri.toUri()) }
|
|
|
|
|
?.onEach { FileHelper.delete(context, it.uri.toUri()) }
|
|
|
|
|
?.let { taskAttachmentDao.delete(it.toList()) }
|
|
|
|
|
}
|
|
|
|
|
clear(remove)
|
|
|
|
@ -468,7 +470,7 @@ class TaskEditViewModel @Inject constructor(
|
|
|
|
|
fun addComment(message: String?, picture: Uri?) {
|
|
|
|
|
val userActivity = UserActivity()
|
|
|
|
|
if (picture != null) {
|
|
|
|
|
val output = FileHelper.copyToUri(applicationContext, preferences.attachmentsDirectory!!, picture)
|
|
|
|
|
val output = FileHelper.copyToUri(context, preferences.attachmentsDirectory!!, picture)
|
|
|
|
|
userActivity.setPicture(output)
|
|
|
|
|
}
|
|
|
|
|
userActivity.message = message
|
|
|
|
@ -481,6 +483,15 @@ class TaskEditViewModel @Inject constructor(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init {
|
|
|
|
|
viewModelScope.launch {
|
|
|
|
|
taskAttachmentDao.getAttachments(task.id).let { attachments ->
|
|
|
|
|
selectedAttachments.update { attachments }
|
|
|
|
|
originalAttachments = attachments
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
fun String?.stripCarriageReturns(): String? = this?.replace("\\r\\n?".toRegex(), "\n")
|
|
|
|
|
}
|
|
|
|
|