Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting to optionally apply date format to note view #218

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import com.google.android.material.slider.Slider
import com.google.android.material.textfield.TextInputLayout.END_ICON_PASSWORD_TOGGLE
import com.philkes.notallyx.R
import com.philkes.notallyx.databinding.ChoiceItemBinding
import com.philkes.notallyx.databinding.NotesSortDialogBinding
import com.philkes.notallyx.databinding.DialogDateFormatBinding
import com.philkes.notallyx.databinding.DialogNotesSortBinding
import com.philkes.notallyx.databinding.DialogPreferenceBooleanBinding
import com.philkes.notallyx.databinding.DialogTextInputBinding
import com.philkes.notallyx.databinding.PreferenceBinding
import com.philkes.notallyx.databinding.PreferenceBooleanDialogBinding
import com.philkes.notallyx.databinding.PreferenceSeekbarBinding
import com.philkes.notallyx.databinding.TextInputDialogBinding
import com.philkes.notallyx.presentation.addCancelButton
import com.philkes.notallyx.presentation.checkedTag
import com.philkes.notallyx.presentation.showToast
Expand All @@ -25,6 +26,7 @@ import com.philkes.notallyx.presentation.viewmodel.BaseNoteModel
import com.philkes.notallyx.presentation.viewmodel.preference.BiometricLock
import com.philkes.notallyx.presentation.viewmodel.preference.BooleanPreference
import com.philkes.notallyx.presentation.viewmodel.preference.Constants.PASSWORD_EMPTY
import com.philkes.notallyx.presentation.viewmodel.preference.DateFormat
import com.philkes.notallyx.presentation.viewmodel.preference.EnumPreference
import com.philkes.notallyx.presentation.viewmodel.preference.IntPreference
import com.philkes.notallyx.presentation.viewmodel.preference.NotallyXPreferences.Companion.EMPTY_PATH
Expand Down Expand Up @@ -129,7 +131,7 @@ fun PreferenceBinding.setup(
Value.text = value.getText(context)

root.setOnClickListener {
val layout = NotesSortDialogBinding.inflate(layoutInflater, null, false)
val layout = DialogNotesSortBinding.inflate(layoutInflater, null, false)
NotesSortBy.entries.forEachIndexed { idx, notesSortBy ->
ChoiceItemBinding.inflate(layoutInflater).root.apply {
id = idx
Expand Down Expand Up @@ -174,6 +176,48 @@ fun PreferenceBinding.setup(
}
}

fun PreferenceBinding.setup(
dateFormatPreference: EnumPreference<DateFormat>,
dateFormatValue: DateFormat,
applyToNoteViewValue: Boolean,
context: Context,
layoutInflater: LayoutInflater,
onSave: (dateFormat: DateFormat, applyToEditMode: Boolean) -> Unit,
) {
Title.setText(dateFormatPreference.titleResId!!)

Value.text = dateFormatValue.getText(context)

root.setOnClickListener {
val layout = DialogDateFormatBinding.inflate(layoutInflater, null, false)
DateFormat.entries.forEachIndexed { idx, dateFormat ->
ChoiceItemBinding.inflate(layoutInflater).root.apply {
id = idx
text = dateFormat.getText(context)
tag = dateFormat
layout.DateFormatRadioGroup.addView(this)
if (dateFormat == dateFormatValue) {
layout.DateFormatRadioGroup.check(this.id)
}
}
}

layout.ApplyToNoteView.isChecked = applyToNoteViewValue

MaterialAlertDialogBuilder(context)
.setTitle(dateFormatPreference.titleResId)
.setView(layout.root)
.setPositiveButton(R.string.save) { dialog, _ ->
dialog.cancel()
val dateFormat = layout.DateFormatRadioGroup.checkedTag() as DateFormat
val applyToNoteView = layout.ApplyToNoteView.isChecked
onSave(dateFormat, applyToNoteView)
}
.addCancelButton()
.show()
}
}

fun PreferenceBinding.setup(
preference: BooleanPreference,
value: Boolean,
Expand All @@ -189,7 +233,7 @@ fun PreferenceBinding.setup(
Value.text = if (value) enabledText else disabledText
root.setOnClickListener {
val layout =
PreferenceBooleanDialogBinding.inflate(layoutInflater, null, false).apply {
DialogPreferenceBooleanBinding.inflate(layoutInflater, null, false).apply {
Title.setText(preference.titleResId)
messageResId?.let { Message.setText(it) }
if (value) {
Expand Down Expand Up @@ -231,7 +275,7 @@ fun PreferenceBinding.setupBackupPassword(
Value.text =
if (password != PASSWORD_EMPTY) password else context.getText(R.string.tap_to_set_up)
root.setOnClickListener {
val layout = TextInputDialogBinding.inflate(layoutInflater, null, false)
val layout = DialogTextInputBinding.inflate(layoutInflater, null, false)
layout.InputText.apply {
if (password != PASSWORD_EMPTY) {
setText(password)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import com.philkes.notallyx.NotallyXApplication
import com.philkes.notallyx.R
import com.philkes.notallyx.data.imports.FOLDER_MIMETYPE
import com.philkes.notallyx.data.imports.ImportSource
import com.philkes.notallyx.databinding.DialogTextInputBinding
import com.philkes.notallyx.databinding.FragmentSettingsBinding
import com.philkes.notallyx.databinding.TextInputDialogBinding
import com.philkes.notallyx.presentation.addCancelButton
import com.philkes.notallyx.presentation.getUriForFile
import com.philkes.notallyx.presentation.setupImportProgressDialog
Expand Down Expand Up @@ -150,7 +150,7 @@ class SettingsFragment : Fragment() {
}

"application/zip" -> {
val layout = TextInputDialogBinding.inflate(layoutInflater, null, false)
val layout = DialogTextInputBinding.inflate(layoutInflater, null, false)
val password = model.preferences.backupPassword.value
layout.InputText.apply {
if (password != PASSWORD_EMPTY) {
Expand Down Expand Up @@ -190,9 +190,17 @@ class SettingsFragment : Fragment() {
}
}

dateFormat.observe(viewLifecycleOwner) { value ->
binding.DateFormat.setup(dateFormat, value, requireContext()) { newValue ->
model.savePreference(dateFormat, newValue)
dateFormat.merge(applyDateFormatInNoteView).observe(viewLifecycleOwner) {
(dateFormatValue, applyDateFormatInEditNoteValue) ->
binding.DateFormat.setup(
dateFormat,
dateFormatValue,
applyDateFormatInEditNoteValue,
requireContext(),
layoutInflater,
) { newDateFormatValue, newApplyDateFormatInEditNote ->
model.savePreference(dateFormat, newDateFormatValue)
model.savePreference(applyDateFormatInNoteView, newApplyDateFormatInEditNote)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import com.philkes.notallyx.presentation.view.note.audio.AudioAdapter
import com.philkes.notallyx.presentation.view.note.preview.PreviewFileAdapter
import com.philkes.notallyx.presentation.view.note.preview.PreviewImageAdapter
import com.philkes.notallyx.presentation.viewmodel.NotallyModel
import com.philkes.notallyx.presentation.viewmodel.preference.DateFormat
import com.philkes.notallyx.presentation.viewmodel.preference.NotesSortBy
import com.philkes.notallyx.presentation.widget.WidgetProvider
import com.philkes.notallyx.utils.FileError
Expand Down Expand Up @@ -468,7 +469,11 @@ abstract class EditActivity(private val type: Type) :
NotesSortBy.MODIFIED_DATE -> Pair(model.modifiedTimestamp, R.string.modified_date)
else -> Pair(null, null)
}
binding.Date.displayFormattedTimestamp(date, preferences.dateFormat.value, datePrefixResId)
val dateFormat =
if (preferences.applyDateFormatInNoteView.value) {
preferences.dateFormat.value
} else DateFormat.ABSOLUTE
binding.Date.displayFormattedTimestamp(date, dateFormat, datePrefixResId)
binding.EnterTitle.setText(model.title)
Operations.bindLabels(binding.LabelGroup, model.labels, model.textSize, paddingTop = true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.philkes.notallyx.R
import com.philkes.notallyx.data.model.findWebUrls
import com.philkes.notallyx.data.model.isNoteUrl
import com.philkes.notallyx.data.model.isWebUrl
import com.philkes.notallyx.databinding.TextInputDialog2Binding
import com.philkes.notallyx.databinding.DialogTextInput2Binding
import com.philkes.notallyx.presentation.clone
import com.philkes.notallyx.presentation.createTextWatcherWithHistory
import com.philkes.notallyx.presentation.removeSelectionFromSpans
Expand Down Expand Up @@ -341,7 +341,7 @@ class StylableEditTextWithHistory(context: Context, attrs: AttributeSet) :
) {
val isNoteUrl = urlBefore.isNoteUrl()
val layout =
TextInputDialog2Binding.inflate(LayoutInflater.from(context)).apply {
DialogTextInput2Binding.inflate(LayoutInflater.from(context)).apply {
InputText1.setText(displayTextBefore)
InputTextLayout1.setHint(R.string.display_text)
InputText2.setText(urlBefore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class NotallyXPreferences private constructor(private val app: Application) {
createEnumPreference(preferences, "textSize", TextSize.MEDIUM, R.string.text_size)
val dateFormat =
createEnumPreference(preferences, "dateFormat", DateFormat.RELATIVE, R.string.date_format)
val applyDateFormatInNoteView =
BooleanPreference("applyDateFormatInNoteView", preferences, true)

val notesView = createEnumPreference(preferences, "view", NotesView.LIST, R.string.view)
val notesSorting = NotesSortPreference(preferences)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.philkes.notallyx.data.model.toPreservedByteArray
import com.philkes.notallyx.data.model.toPreservedString
import com.philkes.notallyx.presentation.view.misc.NotNullLiveData
import com.philkes.notallyx.utils.createObserverSkipFirst
import com.philkes.notallyx.utils.merge
import java.security.SecureRandom
import java.util.Date
import java.util.Locale
Expand Down Expand Up @@ -60,6 +61,10 @@ abstract class BasePreference<T>(
getData().observe(lifecycleOwner, observer)
}

fun <C> merge(other: BasePreference<C>): MediatorLiveData<Pair<T, C>> {
return getData().merge(other.getData())
}

fun observeForever(observer: Observer<T>) {
getData().observeForever(observer)
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/philkes/notallyx/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ fun <T, C> NotNullLiveData<T>.mergeSkipFirst(
}
}

fun <T, C> NotNullLiveData<T>.merge(liveData: NotNullLiveData<C>): MediatorLiveData<Pair<T, C>> {
return MediatorLiveData<Pair<T, C>>().apply {
addSource(this@merge) { value1 -> value = Pair(value1, liveData.value) }
addSource(liveData) { value2 -> value = Pair([email protected], value2) }
}
}

fun ClipboardManager.getLatestText(): CharSequence? {
return primaryClip?.let { if (it.itemCount > 0) it.getItemAt(0)!!.text else null }
}
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/res/layout/dialog_date_format.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="8dp"
android:paddingStart="26dp"
android:paddingEnd="26dp"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/date_format_hint"
style="@style/MaterialAlertDialog.Material3.Body.Text"
android:paddingBottom="8dp"
/>

<RadioGroup
android:id="@+id/DateFormatRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>

<com.google.android.material.checkbox.MaterialCheckBox
android:id="@+id/ApplyToNoteView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/date_format_apply_in_note_view"
/>


</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@
<string name="follow_system">Follow system</string>

<string name="date_format">Date format</string>
<string name="date_format_hint">Applies selected date format in Notes Overview</string>
<string name="date_format_apply_in_note_view">Also apply in Note View</string>
<string name="none">None</string>

<string name="text_size">Text size</string>
Expand Down