Skip to content

Commit

Permalink
add all records and all data delete from data edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Oct 6, 2024
1 parent edd8b2b commit 8e9362d
Show file tree
Hide file tree
Showing 36 changed files with 421 additions and 103 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BackupInteractor @Inject constructor(
return backupRepo.readBackupFile(uriString)
}

private suspend fun doAfterRestore() {
suspend fun doAfterRestore() {
notificationTypeInteractor.updateNotifications()
notificationGoalTimeInteractor.checkAndReschedule()
widgetInteractor.updateWidgets()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ class RecordInteractor @Inject constructor(
recordRepo.remove(id)
}

suspend fun removeAll() {
recordToRecordTagRepo.clear()
recordRepo.clear()
}

private suspend fun updateTags(
recordId: Long,
tagIds: List<Long>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.example.util.simpletimetracker.feature_data_edit.interactor

import com.example.util.simpletimetracker.core.interactor.RecordFilterInteractor
import com.example.util.simpletimetracker.domain.interactor.AddRecordMediator
import com.example.util.simpletimetracker.domain.interactor.BackupInteractor
import com.example.util.simpletimetracker.domain.interactor.ClearDataInteractor
import com.example.util.simpletimetracker.domain.interactor.FilterSelectableTagsInteractor
import com.example.util.simpletimetracker.domain.interactor.NotificationGoalTimeInteractor
import com.example.util.simpletimetracker.domain.interactor.RecordInteractor
Expand All @@ -25,6 +27,8 @@ class DateEditChangeInteractor @Inject constructor(
private val recordTypeToTagInteractor: RecordTypeToTagInteractor,
private val notificationGoalTimeInteractor: NotificationGoalTimeInteractor,
private val filterSelectableTagsInteractor: FilterSelectableTagsInteractor,
private val clearDataInteractor: ClearDataInteractor,
private val backupInteractor: BackupInteractor,
) {

suspend fun changeData(
Expand Down Expand Up @@ -114,4 +118,14 @@ class DateEditChangeInteractor @Inject constructor(
addRecordMediator.doAfterAdd(newTypeId)
}
}

suspend fun deleteAllRecords() {
recordInteractor.removeAll()
backupInteractor.doAfterRestore()
}

suspend fun deleteAllData() {
clearDataInteractor.execute()
backupInteractor.doAfterRestore()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DataEditFragment :
Binding::inflate

override var insetConfiguration: InsetConfiguration =
InsetConfiguration.ApplyToView { binding.root }
InsetConfiguration.ApplyToView { binding.containerDataEdit }

private val viewModel: DataEditViewModel by viewModels()

Expand Down Expand Up @@ -84,6 +84,8 @@ class DataEditFragment :
checkboxDataEditDeleteRecords.setOnClick(viewModel::onDeleteRecordsClick)
etDataEditChangeComment.doAfterTextChanged { viewModel.onCommentChange(it.toString()) }
btnDataEditChange.setOnClick(throttle(viewModel::onChangeClick))
btnDataEditDeleteRecords.setOnClick(throttle(viewModel::onDeleteAllRecordsClick))
btnDataEditDeleteData.setOnClick(throttle(viewModel::onDeleteDataClick))
}

override fun initViewModel(): Unit = with(viewModel) {
Expand All @@ -95,6 +97,7 @@ class DataEditFragment :
removeTagsState.observe(::setRemoveTagState)
deleteRecordsState.observe(::setDeleteRecordsState)
changeButtonState.observe(::setChangeButtonState)
disableButtons.observe { disableButtons() }
keyboardVisibility.observe { visible ->
if (visible) {
showKeyboard(etDataEditChangeComment)
Expand Down Expand Up @@ -249,4 +252,10 @@ class DataEditFragment :
btnDataEditChange.isEnabled = state.enabled
btnDataEditChange.backgroundTintList = ColorStateList.valueOf(state.backgroundTint)
}

private fun disableButtons() = with(binding) {
btnDataEditChange.isEnabled = false
btnDataEditDeleteRecords.isEnabled = false
btnDataEditDeleteData.isEnabled = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.util.simpletimetracker.core.base.SingleLiveEvent
import com.example.util.simpletimetracker.core.extension.set
import com.example.util.simpletimetracker.core.extension.toParams
import com.example.util.simpletimetracker.core.repo.DataEditRepo
import com.example.util.simpletimetracker.core.repo.ResourceRepo
import com.example.util.simpletimetracker.domain.extension.getTypeIds
import com.example.util.simpletimetracker.domain.extension.orZero
import com.example.util.simpletimetracker.domain.interactor.RecordTagInteractor
import com.example.util.simpletimetracker.domain.interactor.RecordTypeToTagInteractor
import com.example.util.simpletimetracker.domain.model.RecordsFilter
import com.example.util.simpletimetracker.feature_data_edit.R
Expand Down Expand Up @@ -42,7 +42,6 @@ class DataEditViewModel @Inject constructor(
private val dataEditRepo: DataEditRepo,
private val dataEditViewDataInteractor: DateEditViewDataInteractor,
private val dataEditChangeInteractor: DateEditChangeInteractor,
private val recordTagInteractor: RecordTagInteractor,
private val recordTypeToTagInteractor: RecordTypeToTagInteractor,
) : ViewModel() {

Expand Down Expand Up @@ -73,6 +72,7 @@ class DataEditViewModel @Inject constructor(
initial
}
}
val disableButtons: LiveData<Unit> = SingleLiveEvent<Unit>()
val keyboardVisibility: LiveData<Boolean> = MutableLiveData(false)

private var filters: List<RecordsFilter> = emptyList()
Expand Down Expand Up @@ -212,19 +212,22 @@ class DataEditViewModel @Inject constructor(
}

fun onChangeClick() {
router.navigate(
StandardDialogParams(
tag = ALERT_DIALOG_TAG,
message = resourceRepo.getString(R.string.archive_deletion_alert),
btnPositive = resourceRepo.getString(R.string.data_edit_button_change),
btnNegative = resourceRepo.getString(R.string.cancel),
),
)
showAlert(tag = CHANGE_ALERT_DIALOG_TAG)
}

fun onDeleteAllRecordsClick() {
showAlert(tag = DELETE_RECORDS_ALERT_DIALOG_TAG)
}

fun onDeleteDataClick() {
showAlert(tag = DELETE_DATA_ALERT_DIALOG_TAG)
}

fun onPositiveDialogClick(tag: String?) {
if (tag == ALERT_DIALOG_TAG) {
onChangeConfirmed()
when (tag) {
CHANGE_ALERT_DIALOG_TAG -> onChangeConfirmed()
DELETE_RECORDS_ALERT_DIALOG_TAG -> onDeleteRecordsConfirmed()
DELETE_DATA_ALERT_DIALOG_TAG -> onDeleteDataConfirmed()
}
}

Expand All @@ -240,19 +243,48 @@ class DataEditViewModel @Inject constructor(
updateSelectedRecordsCountViewData()
}

private fun onChangeConfirmed() = viewModelScope.launch {
changeButtonState.set(dataEditViewDataInteractor.getChangeButtonState(false))
dataEditRepo.inProgress.set(true)

dataEditChangeInteractor.changeData(
typeState = typeState,
commentState = commentState,
addTagState = addTagState,
removeTagState = removeTagState,
deleteRecordsState = deleteState,
filters = filters,
private fun showAlert(tag: String) {
router.navigate(
StandardDialogParams(
tag = tag,
message = resourceRepo.getString(R.string.archive_deletion_alert),
btnPositive = resourceRepo.getString(R.string.ok),
btnNegative = resourceRepo.getString(R.string.cancel),
),
)
}

private fun onChangeConfirmed() {
doDataEditWork {
dataEditChangeInteractor.changeData(
typeState = typeState,
commentState = commentState,
addTagState = addTagState,
removeTagState = removeTagState,
deleteRecordsState = deleteState,
filters = filters,
)
}
}

private fun onDeleteRecordsConfirmed() {
doDataEditWork {
dataEditChangeInteractor.deleteAllRecords()
}
}

private fun onDeleteDataConfirmed() {
doDataEditWork {
dataEditChangeInteractor.deleteAllData()
}
}

private fun doDataEditWork(
work: suspend () -> Unit,
) = viewModelScope.launch {
disableButtons.set(Unit)
dataEditRepo.inProgress.set(true)
work.invoke()
dataEditRepo.inProgress.set(false)
showMessage(R.string.data_edit_success_message)
delay(100) // wait for dialog to close.
Expand Down Expand Up @@ -361,9 +393,11 @@ class DataEditViewModel @Inject constructor(
}

companion object {
private const val FILTER_TAG = "data_edit_filter_tag"
private const val ADD_TAGS_TAG = "data_edit_add_tags_tag"
private const val REMOVE_TAGS_TAG = "data_edit_remove_tags_tag"
private const val ALERT_DIALOG_TAG = "alert_dialog_tag"
private const val FILTER_TAG = "DATA_EDIT_FILTER_TAG"
private const val ADD_TAGS_TAG = "DATA_EDIT_ADD_TAGS_TAG"
private const val REMOVE_TAGS_TAG = "DATA_EDIT_REMOVE_TAGS_TAG"
private const val CHANGE_ALERT_DIALOG_TAG = "DATA_EDIT_CHANGE_ALERT_DIALOG_TAG"
private const val DELETE_RECORDS_ALERT_DIALOG_TAG = "DATA_EDIT_DELETE_RECORDS_ALERT_DIALOG_TAG"
private const val DELETE_DATA_ALERT_DIALOG_TAG = "DATA_EDIT_DELETE_DATA_ALERT_DIALOG_TAG"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@
android:textColor="?colorSecondary"
app:layout_constraintTop_toBottomOf="@id/tvDataEditTitle" />

<com.example.util.simpletimetracker.feature_views.DividerView
<com.example.util.simpletimetracker.feature_views.DividerFullView
android:id="@+id/dividerDataEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/tvDataEditHint" />

<androidx.core.widget.NestedScrollView
android:id="@+id/nsvDataEdit"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/btnDataEditChange"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/dividerDataEdit">

<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/containerDataEdit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
Expand All @@ -52,6 +54,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_marginTop="6dp"
android:padding="16dp"
android:text="@string/data_edit_select_records"
app:icon="@drawable/ic_list_24px"
Expand Down Expand Up @@ -323,16 +326,45 @@

</androidx.cardview.widget.CardView>

<com.google.android.material.button.MaterialButton
android:id="@+id/btnDataEditChange"
style="@style/AppButtonActive"
android:layout_width="match_parent"
android:layout_marginHorizontal="8dp"
android:text="@string/data_edit_button_change" />

<com.example.util.simpletimetracker.feature_views.DividerView
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<com.google.android.material.button.MaterialButton
android:id="@+id/btnDataEditDeleteRecords"
style="@style/AppButtonActive"
android:layout_width="match_parent"
android:layout_height="@dimen/button_height_short"
android:layout_marginHorizontal="8dp"
android:layout_marginVertical="6dp"
android:background="@drawable/bg_rounded_gradient"
android:foreground="?selectableItemBackground"
android:text="@string/data_edit_button_delete_records"
app:backgroundTint="@null"
tools:ignore="UnusedAttribute" />

<com.google.android.material.button.MaterialButton
android:id="@+id/btnDataEditDeleteData"
style="@style/AppButtonActive"
android:layout_width="match_parent"
android:layout_height="@dimen/button_height_short"
android:layout_marginHorizontal="8dp"
android:layout_marginVertical="6dp"
android:background="@drawable/bg_rounded_gradient"
android:foreground="?selectableItemBackground"
android:text="@string/data_edit_button_delete_data"
app:backgroundTint="@null"
tools:ignore="UnusedAttribute" />

</androidx.appcompat.widget.LinearLayoutCompat>

</androidx.core.widget.NestedScrollView>

<com.google.android.material.button.MaterialButton
android:id="@+id/btnDataEditChange"
style="@style/AppButtonActive"
android:layout_width="match_parent"
android:layout_marginHorizontal="8dp"
android:text="@string/data_edit_button_change"
app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.util.simpletimetracker.feature_views

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.example.util.simpletimetracker.feature_views.databinding.DividerFullViewBinding

class DividerFullView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
) : ConstraintLayout(
context,
attrs,
defStyleAttr,
) {

init {
DividerFullViewBinding.inflate(LayoutInflater.from(context), this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="?appNegativeColor"
android:startColor="?colorAccent" />
<corners android:radius="@dimen/button_corner_radius"/>
</shape>
16 changes: 16 additions & 0 deletions features/feature_views/src/main/res/layout/divider_full_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="8dp"
android:background="?appDividerColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</merge>
1 change: 1 addition & 0 deletions features/feature_views/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<dimen name="color_icon_recycler_margin">8dp</dimen>

<dimen name="button_height">58dp</dimen>
<dimen name="button_height_short">46dp</dimen>
<dimen name="button_corner_radius">8dp</dimen>

<dimen name="widget_universal_corner_radius">16dp</dimen>
Expand Down
2 changes: 2 additions & 0 deletions resources/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@
<string name="data_edit_change_add_tag">إضافة وسوم</string>
<string name="data_edit_change_remove_tag">حذف الوسوم</string>
<string name="data_edit_button_change">تغيير</string>
<string name="data_edit_button_delete_records">حذف كافة السجلات</string>
<string name="data_edit_button_delete_data">احذف كافة البيانات</string>
<string name="data_edit_success_message">تم تغيير البيانات</string>

<!-- Records filter -->
Expand Down
2 changes: 2 additions & 0 deletions resources/src/main/res/values-ca/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ Exemple:<br/>
<string name="data_edit_change_add_tag">Afegeix etiquetes</string>
<string name="data_edit_change_remove_tag">Elimina les etiquetes</string>
<string name="data_edit_button_change">Canviar</string>
<string name="data_edit_button_delete_records">Suprimeix tots els registres</string>
<string name="data_edit_button_delete_data">Suprimeix totes les dades</string>
<string name="data_edit_success_message">Dades canviades</string>

<!-- Records filter -->
Expand Down
Loading

0 comments on commit 8e9362d

Please sign in to comment.