properly handle grouping by last_modified and date_taken monthly

This commit is contained in:
tibbi 2019-06-18 23:43:06 +02:00
parent 3e2c66e294
commit 48f097f789
4 changed files with 53 additions and 18 deletions

View file

@ -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

View file

@ -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 {
""
}

View file

@ -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()

View file

@ -29,20 +29,36 @@
android:text="@string/do_not_group_files"/>
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/grouping_dialog_radio_last_modified"
android:id="@+id/grouping_dialog_radio_last_modified_daily"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin"
android:text="@string/by_last_modified"/>
android:text="@string/by_last_modified_daily"/>
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/grouping_dialog_radio_date_taken"
android:id="@+id/grouping_dialog_radio_last_modified_monthly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin"
android:text="@string/by_date_taken"/>
android:text="@string/by_last_modified_monthly"/>
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/grouping_dialog_radio_date_taken_daily"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin"
android:text="@string/by_date_taken_daily"/>
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/grouping_dialog_radio_date_taken_monthly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin"
android:text="@string/by_date_taken_monthly"/>
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/grouping_dialog_radio_file_type"