From 78267e95d857c3bdda44de936bb6c241cbe44621 Mon Sep 17 00:00:00 2001 From: HighPriest Date: Sun, 25 Feb 2024 19:57:31 +0100 Subject: [PATCH 1/2] WIP: Add storage for daily goal & ability to set this daily goal WIP Issues: - After setting the time for a label, opening the dialog keeps the old time value. Only updates when the view is closed & re-opened. Probably caused by how the dialog is created (a lambda), causing the default value to be set only on creation of the view. - The set time doesn't show up anywhere --- .../apps/adrcotfas/goodtime/database/Label.kt | 3 ++ .../adrcotfas/goodtime/database/LabelDao.kt | 3 ++ .../goodtime/labels/AddEditLabelActivity.kt | 26 ++++++++++---- .../goodtime/labels/AddEditLabelsAdapter.kt | 20 +++++++---- .../goodtime/main/LabelsViewModel.kt | 6 ++++ .../goodtime/settings/PreferenceHelper.kt | 6 ++++ .../adrcotfas/goodtime/statistics/Utils.kt | 6 +++- .../statistics/main/SelectLabelDialog.kt | 1 + .../ui/common/TimePickerDialogBuilder.kt | 9 +++++ .../layout/activity_add_edit_labels_row.xml | 35 +++++++++++++++---- app/src/main/res/values/strings.xml | 2 ++ 11 files changed, 97 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/apps/adrcotfas/goodtime/database/Label.kt b/app/src/main/java/com/apps/adrcotfas/goodtime/database/Label.kt index 176a584a..4d328011 100644 --- a/app/src/main/java/com/apps/adrcotfas/goodtime/database/Label.kt +++ b/app/src/main/java/com/apps/adrcotfas/goodtime/database/Label.kt @@ -20,6 +20,9 @@ class Label( @ColumnInfo(defaultValue = "") var title: String, + @ColumnInfo(defaultValue = "0") + var timeDailyGoal: Int, + @ColumnInfo(defaultValue = "0") var colorId: Int) { diff --git a/app/src/main/java/com/apps/adrcotfas/goodtime/database/LabelDao.kt b/app/src/main/java/com/apps/adrcotfas/goodtime/database/LabelDao.kt index ed6609dc..8c42f286 100644 --- a/app/src/main/java/com/apps/adrcotfas/goodtime/database/LabelDao.kt +++ b/app/src/main/java/com/apps/adrcotfas/goodtime/database/LabelDao.kt @@ -38,6 +38,9 @@ interface LabelDao { @Query("update Label SET colorId = :colorId WHERE title = :title") suspend fun editLabelColor(title: String, colorId: Int) + @Query("update Label SET timeDailyGoal = :timeDailyGoal WHERE title = :title") + suspend fun editLabelGoal(title: String, timeDailyGoal: Int) + @Query("update Label SET `order` = :order WHERE title = :title") suspend fun editLabelOrder(title: String, order: Int) diff --git a/app/src/main/java/com/apps/adrcotfas/goodtime/labels/AddEditLabelActivity.kt b/app/src/main/java/com/apps/adrcotfas/goodtime/labels/AddEditLabelActivity.kt index 7d651371..890f1066 100644 --- a/app/src/main/java/com/apps/adrcotfas/goodtime/labels/AddEditLabelActivity.kt +++ b/app/src/main/java/com/apps/adrcotfas/goodtime/labels/AddEditLabelActivity.kt @@ -170,7 +170,7 @@ class AddEditLabelActivity : AppCompatActivity(), OnEditLabelListener { viewModel.editLabelName(label, newLabel) val crtLabel = preferenceHelper.currentSessionLabel if (crtLabel.title == label) { - preferenceHelper.currentSessionLabel = Label(newLabel, crtLabel.colorId) + preferenceHelper.currentSessionLabel = Label(newLabel, crtLabel.colorId, crtLabel.timeDailyGoal) } } @@ -178,7 +178,15 @@ class AddEditLabelActivity : AppCompatActivity(), OnEditLabelListener { viewModel.editLabelColor(label, newColor) val crtLabel = preferenceHelper.currentSessionLabel if (crtLabel.title != "" && crtLabel.title == label) { - preferenceHelper.currentSessionLabel = Label(label, newColor) + preferenceHelper.currentSessionLabel = Label(label, newColor, crtLabel.timeDailyGoal) + } + } + + override fun onEditTimeGoal(label: String, newTimeGoal: Int) { + viewModel.editLabelGoal(label, newTimeGoal) + val crtLabel = preferenceHelper.currentSessionLabel + if (crtLabel.title != "" && crtLabel.title == label) { + preferenceHelper.currentSessionLabel = Label(label, crtLabel.colorId, newTimeGoal) } } @@ -188,7 +196,9 @@ class AddEditLabelActivity : AppCompatActivity(), OnEditLabelListener { if (label.archived && crtLabel.title != "" && crtLabel.title == label.title) { currentSessionManager.currentSession.setLabel("") preferenceHelper.currentSessionLabel = Label( - "", ThemeHelper.getColor( + "", + 0, + ThemeHelper.getColor( this, ThemeHelper.COLOR_INDEX_UNLABELED ) @@ -245,7 +255,9 @@ class AddEditLabelActivity : AppCompatActivity(), OnEditLabelListener { // the label attached to the current session was deleted if (label.title == preferenceHelper.currentSessionLabel.title) { preferenceHelper.currentSessionLabel = Label( - "", ThemeHelper.getColor( + "", + 0, + ThemeHelper.getColor( this, ThemeHelper.COLOR_INDEX_UNLABELED ) @@ -276,14 +288,16 @@ class AddEditLabelActivity : AppCompatActivity(), OnEditLabelListener { private fun addLabel() { labelToAdd = - Label(addLabelView.text.toString().trim { it <= ' ' }, labelToAdd.colorId) + Label(addLabelView.text.toString().trim { it <= ' ' }, labelToAdd.timeDailyGoal, labelToAdd.colorId) if (labelIsGoodToAdd(this, labels, labelToAdd.title, "")) { labels.add(labelToAdd) adapter.notifyItemInserted(labels.size) recyclerView.scrollToPosition(labels.size - 1) viewModel.addLabel(labelToAdd) labelToAdd = Label( - "", ThemeHelper.getColor( + "", + 0, + ThemeHelper.getColor( this, ThemeHelper.COLOR_INDEX_UNLABELED ) diff --git a/app/src/main/java/com/apps/adrcotfas/goodtime/labels/AddEditLabelsAdapter.kt b/app/src/main/java/com/apps/adrcotfas/goodtime/labels/AddEditLabelsAdapter.kt index facf008f..4a3cc0ca 100644 --- a/app/src/main/java/com/apps/adrcotfas/goodtime/labels/AddEditLabelsAdapter.kt +++ b/app/src/main/java/com/apps/adrcotfas/goodtime/labels/AddEditLabelsAdapter.kt @@ -13,6 +13,7 @@ package com.apps.adrcotfas.goodtime.labels import android.annotation.SuppressLint +import android.app.TimePickerDialog import android.content.Context import com.apps.adrcotfas.goodtime.labels.AddEditLabelActivity.Companion.labelIsGoodToAdd import com.apps.adrcotfas.goodtime.main.ItemTouchHelperAdapter @@ -40,6 +41,7 @@ class AddEditLabelsAdapter( interface OnEditLabelListener { fun onEditColor(label: String, newColor: Int) + fun onEditTimeGoal(label: String, newTimeGoal: Int) fun onEditLabel(label: String, newLabel: String) fun onDeleteLabel(label: Label, position: Int) fun onLabelRearranged() @@ -102,11 +104,13 @@ class AddEditLabelsAdapter( val labelIcon: ImageView = itemView.findViewById(R.id.label_icon) val imageLeft: ImageView = itemView.findViewById(R.id.image_left) private val imageRight: ImageView = itemView.findViewById(R.id.image_right) + val labelTimeSet: ImageView = itemView.findViewById(R.id.label_time_set) private val row: RelativeLayout = itemView.findViewById(R.id.dialog_edit_label_row) val scrollIconContainer: FrameLayout = itemView.findViewById(R.id.scroll_icon_container) private val imageLeftContainer: FrameLayout = itemView.findViewById(R.id.image_left_container) private val labelIconContainer: FrameLayout = itemView.findViewById(R.id.label_icon_container) private val imageRightContainer: FrameLayout = itemView.findViewById(R.id.image_right_container) + private val labelTimeSetContainer: FrameLayout = itemView.findViewById(R.id.label_time_set_container) private val imageDeleteContainer: FrameLayout = itemView.findViewById(R.id.image_delete_container) override fun onItemSelected() { @@ -125,18 +129,13 @@ class AddEditLabelsAdapter( text.onFocusChangeListener = OnFocusChangeListener { _: View?, hasFocus: Boolean -> // shrink the textView when we're in edit mode and the delete button appears - val params = text.layoutParams as RelativeLayout.LayoutParams - params.addRule( - RelativeLayout.START_OF, - if (hasFocus) R.id.image_delete_container else R.id.image_right_container - ) - text.layoutParams = params val position = bindingAdapterPosition val crtLabel = labels[position] labelIcon.setColorFilter(ThemeHelper.getColor(context.get()!!, crtLabel.colorId)) imageLeftContainer.visibility = if (hasFocus) View.VISIBLE else View.INVISIBLE labelIconContainer.visibility = if (hasFocus) View.INVISIBLE else View.VISIBLE imageDeleteContainer.visibility = if (hasFocus) View.VISIBLE else View.INVISIBLE + labelTimeSetContainer.visibility = if (hasFocus) View.INVISIBLE else View.VISIBLE imageRight.setImageDrawable( ContextCompat.getDrawable( context.get()!!, if (hasFocus) R.drawable.ic_done else R.drawable.ic_edit @@ -224,6 +223,15 @@ class AddEditLabelsAdapter( dialog.show() } + // changing the time goal of a label + labelTimeSetContainer.setOnClickListener { + val crtLabel = labels[bindingAdapterPosition] + val dialog = TimePickerDialog(context.get()!!, { _, hour, minute -> + callback.onEditTimeGoal(crtLabel.title, hour * 60 + minute) + }, crtLabel.timeDailyGoal / 60, crtLabel.timeDailyGoal % 60, true) + dialog.show() + } + // the edit button imageRightContainer.setOnClickListener { ThemeHelper.requestFocusEditText( diff --git a/app/src/main/java/com/apps/adrcotfas/goodtime/main/LabelsViewModel.kt b/app/src/main/java/com/apps/adrcotfas/goodtime/main/LabelsViewModel.kt index b13d45a2..185d51a1 100644 --- a/app/src/main/java/com/apps/adrcotfas/goodtime/main/LabelsViewModel.kt +++ b/app/src/main/java/com/apps/adrcotfas/goodtime/main/LabelsViewModel.kt @@ -72,6 +72,12 @@ class LabelsViewModel @Inject constructor( } } + fun editLabelGoal(label: String, timeDailyGoal: Int) { + viewModelScope.launch(Dispatchers.IO) { + dao.editLabelGoal(label, timeDailyGoal) + } + } + fun editLabelOrder(label: String, newOrder: Int) { viewModelScope.launch(Dispatchers.IO) { dao.editLabelOrder(label, newOrder) diff --git a/app/src/main/java/com/apps/adrcotfas/goodtime/settings/PreferenceHelper.kt b/app/src/main/java/com/apps/adrcotfas/goodtime/settings/PreferenceHelper.kt index 56e74d03..556ace33 100644 --- a/app/src/main/java/com/apps/adrcotfas/goodtime/settings/PreferenceHelper.kt +++ b/app/src/main/java/com/apps/adrcotfas/goodtime/settings/PreferenceHelper.kt @@ -67,6 +67,7 @@ class PreferenceHelper(val context: Context) { private const val WORK_STREAK = "pref_WORK_STREAK" private const val LAST_WORK_FINISHED_AT = "pref_last_work_finished_at" private const val CURRENT_SESSION_LABEL = "pref_current_session_label" + private const val CURRENT_SESSION_GOAL = "pref_current_session_goal" private const val CURRENT_SESSION_COLOR = "pref_current_session_color" private const val INTRO_SNACKBAR_STEP = "pref_intro_snackbar_step" private const val INTRO_ARCHIVE_LABEL = "pref_intro_archive_label" @@ -192,6 +193,9 @@ class PreferenceHelper(val context: Context) { .getString( CURRENT_SESSION_LABEL, "" )!!, + preferences.getInt( + CURRENT_SESSION_GOAL, 0 + ), preferences.getInt( CURRENT_SESSION_COLOR, 0 ) @@ -199,6 +203,8 @@ class PreferenceHelper(val context: Context) { set(label) { preferences.edit() .putString(CURRENT_SESSION_LABEL, label.title).apply() + preferences.edit() + .putInt(CURRENT_SESSION_GOAL, label.timeDailyGoal).apply() preferences.edit() .putInt(CURRENT_SESSION_COLOR, label.colorId).apply() } diff --git a/app/src/main/java/com/apps/adrcotfas/goodtime/statistics/Utils.kt b/app/src/main/java/com/apps/adrcotfas/goodtime/statistics/Utils.kt index f6baae36..29b1e1e9 100644 --- a/app/src/main/java/com/apps/adrcotfas/goodtime/statistics/Utils.kt +++ b/app/src/main/java/com/apps/adrcotfas/goodtime/statistics/Utils.kt @@ -21,13 +21,16 @@ object Utils { fun getInstanceTotalLabel(context: Context): Label { return Label( context.getString(R.string.label_all), + 0, // TODO: Check if this 0 is a correct default value, or should I get something here ThemeHelper.COLOR_INDEX_ALL_LABELS ) } fun getInstanceUnlabeledLabel(context: Context): Label { return Label( - "unlabeled", ThemeHelper.getColor( + "unlabeled", + 0, + ThemeHelper.getColor( context, ThemeHelper.COLOR_INDEX_UNLABELED ) @@ -38,6 +41,7 @@ object Utils { val randomColor = ThemeHelper.getPalette(context).indices.random() return Label( "", + 0, randomColor ) } diff --git a/app/src/main/java/com/apps/adrcotfas/goodtime/statistics/main/SelectLabelDialog.kt b/app/src/main/java/com/apps/adrcotfas/goodtime/statistics/main/SelectLabelDialog.kt index 32738364..f68eb8fa 100644 --- a/app/src/main/java/com/apps/adrcotfas/goodtime/statistics/main/SelectLabelDialog.kt +++ b/app/src/main/java/com/apps/adrcotfas/goodtime/statistics/main/SelectLabelDialog.kt @@ -148,6 +148,7 @@ class SelectLabelDialog : DialogFragment() { notifyLabelSelected( Label( mLabel, + 0, // TODO: Check if this 0 placeholder is placed correctly, or should I somehow get value of the "Chip" ThemeHelper.getIndexOfColor(requireContext(), color) ) ) diff --git a/app/src/main/java/com/apps/adrcotfas/goodtime/ui/common/TimePickerDialogBuilder.kt b/app/src/main/java/com/apps/adrcotfas/goodtime/ui/common/TimePickerDialogBuilder.kt index 47639c05..541e1183 100644 --- a/app/src/main/java/com/apps/adrcotfas/goodtime/ui/common/TimePickerDialogBuilder.kt +++ b/app/src/main/java/com/apps/adrcotfas/goodtime/ui/common/TimePickerDialogBuilder.kt @@ -18,4 +18,13 @@ class TimePickerDialogBuilder(context: Context) { .setInputMode(MaterialTimePicker.INPUT_MODE_KEYBOARD) .build() } + + fun buildDialog(hours: Int, minutes: Int): MaterialTimePicker { + return MaterialTimePicker.Builder() + .setHour(hours) + .setMinute(minutes) + .setTimeFormat(TimeFormat.CLOCK_24H) + .setInputMode(MaterialTimePicker.INPUT_MODE_CLOCK) + .build() + } } diff --git a/app/src/main/res/layout/activity_add_edit_labels_row.xml b/app/src/main/res/layout/activity_add_edit_labels_row.xml index afba8b1f..f90687ea 100644 --- a/app/src/main/res/layout/activity_add_edit_labels_row.xml +++ b/app/src/main/res/layout/activity_add_edit_labels_row.xml @@ -80,16 +80,37 @@ android:hint="@string/label_name" android:layout_centerVertical="true" android:layout_toEndOf="@id/image_left_container" - - android:layout_toStartOf="@id/image_right_container" - + android:layout_toStartOf="@id/label_time_set_container" android:layout_width="match_parent" - android:layout_height="match_parent" - - android:paddingBottom="12dp" + android:layout_height="wrap_content" android:paddingTop="12dp" + android:paddingBottom="12dp" + style="@style/AddEditLabelRowEdit" /> - style="@style/AddEditLabelRowEdit"/> + + + + + Label name Label already exists Select color + Pick daily time goal Delete label? Deleting this label will remove it from all finished sessions. The sessions will not be removed. Select label @@ -102,6 +103,7 @@ "Stay focused, get rid of procrastination and improve your productivity." Close + Goal Time Delete Edit Upgrade From fca3075dbb970e2e7ac2553e092aefce3df5b3fa Mon Sep 17 00:00:00 2001 From: HighPriest Date: Tue, 27 Feb 2024 23:11:33 +0100 Subject: [PATCH 2/2] WIP: Modify layouts to hold chips with labels and time left --- app/src/main/res/layout/activity_main.xml | 8 +++++ .../layout/activity_main_goal_labels_row.xml | 36 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 app/src/main/res/layout/activity_main_goal_labels_row.xml diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3a06e497..b87a174e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,6 +7,14 @@ android:layout_height="match_parent" android:fitsSystemWindows="true"> + + + + + + + + + + + + + + + \ No newline at end of file