Skip to content

Commit

Permalink
Fixing local media time offset and bug where media would show duplica…
Browse files Browse the repository at this point in the history
…ted in the feed when uploaded in some scenarios. Fixes #447. Fixes #446
  • Loading branch information
savvasdalkitsis committed Aug 29, 2023
1 parent e154998 commit ac11e23
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM localMediaItemDetails;
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,33 @@ internal class FeedUseCase @Inject constructor(
mediaBeingUploaded: Set<Long>,
): List<MediaCollection> {
val allLocalDays = allLocalMedia.groupBy { it.displayDayDate }
val combined = mergeLocalMediaIntoExistingRemoteDays(allRemoteDays, allLocalDays) +
remainingLocalDays(allRemoteDays, allLocalDays)
val mergedToRemote =
allRemoteDays.mergeLocalMediaIntoExistingRemoteDays(allLocalMedia)
val remainingLocalDays = allLocalDays.mediaNotIn(mergedToRemote)
val remoteAndLocalExistingDays = mergedToRemote
.addRemainingLocalMediaToExistingDays(remainingLocalDays)
val combined = remoteAndLocalExistingDays + allLocalDays
.mediaNotIn(remoteAndLocalExistingDays)
.toMediaCollections()
return combined
.sortedByDescending { it.unformattedDate }
.markDownloading(mediaBeingDownloaded)
.markUploading(mediaBeingUploaded)
.toList()
}

private fun Map<String?, List<MediaItem>>.mediaNotIn(
mergedToRemote: Sequence<MediaCollection>
): Map<String?, List<MediaItem>> {
val mergedToRemoteHashes = mergedToRemote
.flatMap { it.mediaItems }
.map { it.mediaHash }
return mapValues { (_, media) ->
media.filter { it.mediaHash !in mergedToRemoteHashes }
}
.filter { (_, media) -> media.isNotEmpty() }
}

private fun Sequence<MediaCollection>.markDownloading(
inProgress: Set<String>,
): Sequence<MediaCollection> = map { collection ->
Expand Down Expand Up @@ -186,29 +204,32 @@ internal class FeedUseCase @Inject constructor(
toMediaCollection()
}

private fun mergeLocalMediaIntoExistingRemoteDays(
remoteDays: Sequence<MediaCollection>,
localDays: Map<String?, List<MediaItem>>
) = remoteDays.map { remoteDay ->
val localMediaOnDay = localDays[remoteDay.displayTitle] ?: emptyList()
val existingRemoteMediaHashes = remoteDay.mediaItems.map { it.mediaHash }
val localMediaNotPresentRemotely = localMediaOnDay.filter {
it.mediaHash !in existingRemoteMediaHashes
}
val combinedMediaItems = remoteDay.mediaItems.map { remoteMediaItem ->
mergeLocalDuplicates(localDays.flatMap { it.value }, remoteMediaItem)
} + localMediaNotPresentRemotely
private fun Sequence<MediaCollection>.mergeLocalMediaIntoExistingRemoteDays(
localMedia: Sequence<MediaItem>
) = map { remoteDay ->
remoteDay.copy(
mediaItems = remoteDay.mediaItems.map { remoteMediaItem ->
mergeLocalDuplicates(localMedia, remoteMediaItem)
}
)
}

private fun Sequence<MediaCollection>.addRemainingLocalMediaToExistingDays(
remainingLocalDays: Map<String?, List<MediaItem>>
) = map { remoteDay ->
val localMediaOnDay = remainingLocalDays[remoteDay.displayTitle] ?: emptyList()
val combinedMediaItems = remoteDay.mediaItems + localMediaOnDay
remoteDay.copy(
mediaItems = combinedMediaItems.sortedByDescending { it.sortableDate }
)
}

private fun mergeLocalDuplicates(
allLocalMedia: List<MediaItem>,
localMedia: Sequence<MediaItem>,
remoteMediaItem: MediaItem
): MediaItem {
val localCopies =
allLocalMedia.filter { it.mediaHash == remoteMediaItem.mediaHash }.toSet()
localMedia.filter { it.mediaHash == remoteMediaItem.mediaHash }.toSet()
return when {
localCopies.isNotEmpty() ->
MediaItemGroup(
Expand All @@ -220,20 +241,15 @@ internal class FeedUseCase @Inject constructor(
}
}

private fun remainingLocalDays(
allRemoteDays: Sequence<MediaCollection>,
allLocalDays: Map<String?, List<MediaItem>>
) = allLocalDays
.filter { (day, _) -> day !in allRemoteDays.map { it.displayTitle } }
.map { (day, items) ->
MediaCollection(
id = "local_media_collection_$day",
mediaItems = items,
displayTitle = day ?: "-",
location = null,
unformattedDate = items.firstOrNull()?.sortableDate
)
}
private fun Map<String?, List<MediaItem>>.toMediaCollections() = map { (day, items) ->
MediaCollection(
id = "local_media_collection_$day",
mediaItems = items,
displayTitle = day ?: "-",
location = null,
unformattedDate = items.firstOrNull()?.sortableDate
)
}

private fun observeLocalMediaFeed(feedFetchType: FeedFetchType) =
mediaUseCase.observeLocalMedia()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class LocalMediaRepository @Inject constructor(
LocalMediaItemDetails(
id = item.id,
displayName = item.displayName,
dateTaken = item.dateTaken.toDateString(),
dateTaken = (exif.dateTime ?: item.dateTaken).toDateString(),
bucketId = item.bucketId,
bucketName = item.bucketName,
width = item.width ?: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data class ExifMetadata(
val shutterSpeed: Double?,
val isoSpeed: Int?,
val camera: String?,
val dateTime: Long?,
val focalLength: Double?,
val focalLength35Equivalent: Int?,
val width: Int?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/
package com.savvasdalkitsis.uhuruphotos.foundation.exif.implementation.usecase

import android.annotation.SuppressLint
import androidx.exifinterface.media.ExifInterface
import androidx.exifinterface.media.ExifInterface.TAG_APERTURE_VALUE
import androidx.exifinterface.media.ExifInterface.TAG_DIGITAL_ZOOM_RATIO
Expand Down Expand Up @@ -66,6 +67,7 @@ class ExifUseCase @Inject constructor(
}


@SuppressLint("RestrictedApi")
private fun ExifInterface.metadata() = ExifMetadata(
fStop = double(TAG_F_NUMBER)
?: ratio(TAG_APERTURE_VALUE)?.let { exp(it * ln(2.0) * 0.5) },
Expand All @@ -74,6 +76,7 @@ class ExifUseCase @Inject constructor(
},
isoSpeed = int(TAG_PHOTOGRAPHIC_SENSITIVITY),
camera = string(TAG_MODEL),
dateTime = dateTime,
focalLength = ratio(TAG_FOCAL_LENGTH),
focalLength35Equivalent = int(TAG_FOCAL_LENGTH_IN_35MM_FILM),
width = int(TAG_PIXEL_X_DIMENSION) ?: int(TAG_IMAGE_WIDTH),
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ kotlin.code.style=official
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
org.gradle.daemon=true
org.gradle.daemon=false
org.gradle.configuration-cache=true

0 comments on commit ac11e23

Please sign in to comment.