From 48f097f7894847f2e598d6444f9963824d62e7ae Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 18 Jun 2019 23:43:06 +0200 Subject: [PATCH] properly handle grouping by last_modified and date_taken monthly --- .../pro/dialogs/ChangeGroupingDialog.kt | 12 ++++++---- .../gallery/pro/helpers/MediaFetcher.kt | 23 ++++++++++++------ .../gallery/pro/models/Medium.kt | 12 +++++++--- .../res/layout/dialog_change_grouping.xml | 24 +++++++++++++++---- 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/ChangeGroupingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/ChangeGroupingDialog.kt index b477b8501..71da2f57d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/ChangeGroupingDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/ChangeGroupingDialog.kt @@ -41,8 +41,10 @@ class ChangeGroupingDialog(val activity: BaseSimpleActivity, val path: String = val groupBtn = when { currGrouping and GROUP_BY_NONE != 0 -> groupingRadio.grouping_dialog_radio_none - currGrouping and GROUP_BY_LAST_MODIFIED_DAILY != 0 -> groupingRadio.grouping_dialog_radio_last_modified - currGrouping and GROUP_BY_DATE_TAKEN_DAILY != 0 -> groupingRadio.grouping_dialog_radio_date_taken + currGrouping and GROUP_BY_LAST_MODIFIED_DAILY != 0 -> groupingRadio.grouping_dialog_radio_last_modified_daily + currGrouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 -> groupingRadio.grouping_dialog_radio_last_modified_monthly + currGrouping and GROUP_BY_DATE_TAKEN_DAILY != 0 -> groupingRadio.grouping_dialog_radio_date_taken_daily + currGrouping and GROUP_BY_DATE_TAKEN_MONTHLY != 0 -> groupingRadio.grouping_dialog_radio_date_taken_monthly currGrouping and GROUP_BY_FILE_TYPE != 0 -> groupingRadio.grouping_dialog_radio_file_type currGrouping and GROUP_BY_EXTENSION != 0 -> groupingRadio.grouping_dialog_radio_extension else -> groupingRadio.grouping_dialog_radio_folder @@ -64,8 +66,10 @@ class ChangeGroupingDialog(val activity: BaseSimpleActivity, val path: String = val groupingRadio = view.grouping_dialog_radio_grouping var grouping = when (groupingRadio.checkedRadioButtonId) { R.id.grouping_dialog_radio_none -> GROUP_BY_NONE - R.id.grouping_dialog_radio_last_modified -> GROUP_BY_LAST_MODIFIED_DAILY - R.id.grouping_dialog_radio_date_taken -> GROUP_BY_DATE_TAKEN_DAILY + R.id.grouping_dialog_radio_last_modified_daily -> GROUP_BY_LAST_MODIFIED_DAILY + R.id.grouping_dialog_radio_last_modified_monthly -> GROUP_BY_LAST_MODIFIED_MONTHLY + R.id.grouping_dialog_radio_date_taken_daily -> GROUP_BY_DATE_TAKEN_DAILY + R.id.grouping_dialog_radio_date_taken_monthly -> GROUP_BY_DATE_TAKEN_MONTHLY R.id.grouping_dialog_radio_file_type -> GROUP_BY_FILE_TYPE R.id.grouping_dialog_radio_extension -> GROUP_BY_EXTENSION else -> GROUP_BY_FOLDER diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index a7f867c7b..a67a88468 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -336,7 +336,8 @@ class MediaFetcher(val context: Context) { } val sortDescending = currentGrouping and GROUP_DESCENDING != 0 - val sorted = if (currentGrouping and GROUP_BY_DATE_TAKEN_DAILY != 0 || currentGrouping and GROUP_BY_LAST_MODIFIED_DAILY != 0) { + val sorted = if (currentGrouping and GROUP_BY_LAST_MODIFIED_DAILY != 0 || currentGrouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 || + currentGrouping and GROUP_BY_DATE_TAKEN_DAILY != 0 || currentGrouping and GROUP_BY_DATE_TAKEN_MONTHLY != 0) { mediumGroups.toSortedMap(if (sortDescending) compareByDescending { it.toLongOrNull() ?: 0L } else { @@ -351,8 +352,8 @@ class MediaFetcher(val context: Context) { mediumGroups[key] = value } - val today = formatDate(System.currentTimeMillis().toString()) - val yesterday = formatDate((System.currentTimeMillis() - DAY_SECONDS * 1000).toString()) + val today = formatDate(System.currentTimeMillis().toString(), true) + val yesterday = formatDate((System.currentTimeMillis() - DAY_SECONDS * 1000).toString(), true) for ((key, value) in mediumGroups) { val sectionKey = getFormattedKey(key, currentGrouping, today, yesterday) thumbnailItems.add(ThumbnailSection(sectionKey)) @@ -363,13 +364,20 @@ class MediaFetcher(val context: Context) { } private fun getFormattedKey(key: String, grouping: Int, today: String, yesterday: String): String { - return when { - grouping and GROUP_BY_LAST_MODIFIED_DAILY != 0 || grouping and GROUP_BY_DATE_TAKEN_DAILY != 0 -> getFinalDate(formatDate(key), today, yesterday) + var result = when { + grouping and GROUP_BY_LAST_MODIFIED_DAILY != 0 || grouping and GROUP_BY_DATE_TAKEN_DAILY != 0 -> getFinalDate(formatDate(key, true), today, yesterday) + grouping and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 || grouping and GROUP_BY_DATE_TAKEN_MONTHLY != 0 -> formatDate(key, false) grouping and GROUP_BY_FILE_TYPE != 0 -> getFileTypeString(key) grouping and GROUP_BY_EXTENSION != 0 -> key.toUpperCase() grouping and GROUP_BY_FOLDER != 0 -> context.humanizePath(key) else -> key } + + if (result.isEmpty()) { + result = context.getString(R.string.unknown) + } + + return result } private fun getFinalDate(date: String, today: String, yesterday: String): String { @@ -380,11 +388,12 @@ class MediaFetcher(val context: Context) { } } - private fun formatDate(timestamp: String): String { + private fun formatDate(timestamp: String, showDay: Boolean): String { return if (timestamp.areDigitsOnly()) { val cal = Calendar.getInstance(Locale.ENGLISH) cal.timeInMillis = timestamp.toLong() - DateFormat.format("dd MMM yyyy", cal).toString() + val format = if (showDay) "dd MMM yyyy" else "MMM yyyy" + DateFormat.format(format, cal).toString() } else { "" } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt index 6be0459e9..6044219d7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt @@ -56,8 +56,10 @@ data class Medium( fun getGroupingKey(groupBy: Int): String { return when { - groupBy and GROUP_BY_LAST_MODIFIED_DAILY != 0 -> getDayStartTS(modified) - groupBy and GROUP_BY_DATE_TAKEN_DAILY != 0 -> getDayStartTS(taken) + groupBy and GROUP_BY_LAST_MODIFIED_DAILY != 0 -> getDayStartTS(modified, false) + groupBy and GROUP_BY_LAST_MODIFIED_MONTHLY != 0 -> getDayStartTS(modified, true) + groupBy and GROUP_BY_DATE_TAKEN_DAILY != 0 -> getDayStartTS(taken, false) + groupBy and GROUP_BY_DATE_TAKEN_MONTHLY != 0 -> getDayStartTS(taken, true) groupBy and GROUP_BY_FILE_TYPE != 0 -> type.toString() groupBy and GROUP_BY_EXTENSION != 0 -> name.getFilenameExtension().toLowerCase() groupBy and GROUP_BY_FOLDER != 0 -> parentPath @@ -67,13 +69,17 @@ data class Medium( fun getIsInRecycleBin() = deletedTS != 0L - private fun getDayStartTS(ts: Long): String { + private fun getDayStartTS(ts: Long, resetDays: Boolean): String { val calendar = Calendar.getInstance(Locale.ENGLISH).apply { timeInMillis = ts set(Calendar.HOUR_OF_DAY, 0) set(Calendar.MINUTE, 0) set(Calendar.SECOND, 0) set(Calendar.MILLISECOND, 0) + + if (resetDays) { + set(Calendar.DAY_OF_MONTH, 1) + } } return calendar.timeInMillis.toString() diff --git a/app/src/main/res/layout/dialog_change_grouping.xml b/app/src/main/res/layout/dialog_change_grouping.xml index a51c19111..c64aad625 100644 --- a/app/src/main/res/layout/dialog_change_grouping.xml +++ b/app/src/main/res/layout/dialog_change_grouping.xml @@ -29,20 +29,36 @@ android:text="@string/do_not_group_files"/> + android:text="@string/by_last_modified_daily"/> + android:text="@string/by_last_modified_monthly"/> + + + +