From 23b0f4b1025e78f9023f129360fe50988c75e504 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Mon, 21 Aug 2023 03:12:41 -0700 Subject: [PATCH] fields: refactor more --- app/src/main/java/gallery/memories/NativeX.kt | 4 +- .../java/gallery/memories/mapper/Fields.kt | 28 +++++++ .../gallery/memories/service/ConfigService.kt | 24 ++++++ .../gallery/memories/service/TimelineQuery.kt | 80 +++++++------------ 4 files changed, 81 insertions(+), 55 deletions(-) create mode 100644 app/src/main/java/gallery/memories/service/ConfigService.kt diff --git a/app/src/main/java/gallery/memories/NativeX.kt b/app/src/main/java/gallery/memories/NativeX.kt index d10421b5..8b6e4dc6 100644 --- a/app/src/main/java/gallery/memories/NativeX.kt +++ b/app/src/main/java/gallery/memories/NativeX.kt @@ -185,7 +185,7 @@ import java.net.URLDecoder @JavascriptInterface fun configSetLocalFolders(json: String?) { if (json == null) return; - query.configSetLocalFolders(json) + query.localFolders = JSONArray(json) } @Throws(Exception::class) @@ -210,7 +210,7 @@ import java.net.URLDecoder } else if (path.matches(API.SHARE_LOCAL)) { return makeResponse(dlService!!.shareLocal(parts[4].toLong())) } else if (path.matches(API.CONFIG_LOCAL_FOLDES)) { - return makeResponse(query.getLocalFoldersConfig()) + return makeResponse(query.localFolders) } else { throw Exception("Not Found") } diff --git a/app/src/main/java/gallery/memories/mapper/Fields.kt b/app/src/main/java/gallery/memories/mapper/Fields.kt index 6f1be773..e1121579 100644 --- a/app/src/main/java/gallery/memories/mapper/Fields.kt +++ b/app/src/main/java/gallery/memories/mapper/Fields.kt @@ -1,5 +1,7 @@ package gallery.memories.mapper +import androidx.exifinterface.media.ExifInterface + class Fields { object Day { const val DAYID = Photo.DAYID @@ -27,4 +29,30 @@ class Fields { object Perm { const val DELETE = "D" } + + object EXIF { + val MAP = mapOf( + ExifInterface.TAG_APERTURE_VALUE to "Aperture", + ExifInterface.TAG_FOCAL_LENGTH to "FocalLength", + ExifInterface.TAG_F_NUMBER to "FNumber", + ExifInterface.TAG_SHUTTER_SPEED_VALUE to "ShutterSpeed", + ExifInterface.TAG_EXPOSURE_TIME to "ExposureTime", + ExifInterface.TAG_ISO_SPEED to "ISO", + ExifInterface.TAG_DATETIME_ORIGINAL to "DateTimeOriginal", + ExifInterface.TAG_OFFSET_TIME_ORIGINAL to "OffsetTimeOriginal", + ExifInterface.TAG_GPS_LATITUDE to "GPSLatitude", + ExifInterface.TAG_GPS_LONGITUDE to "GPSLongitude", + ExifInterface.TAG_GPS_ALTITUDE to "GPSAltitude", + ExifInterface.TAG_MAKE to "Make", + ExifInterface.TAG_MODEL to "Model", + ExifInterface.TAG_ORIENTATION to "Orientation", + ExifInterface.TAG_IMAGE_DESCRIPTION to "Description" + ) + } + + object Bucket { + const val ID = "id" + const val NAME = "name" + const val ENABLED = "enabled" + } } \ No newline at end of file diff --git a/app/src/main/java/gallery/memories/service/ConfigService.kt b/app/src/main/java/gallery/memories/service/ConfigService.kt new file mode 100644 index 00000000..cebdb2f9 --- /dev/null +++ b/app/src/main/java/gallery/memories/service/ConfigService.kt @@ -0,0 +1,24 @@ +package gallery.memories.service + +import android.content.Context +import gallery.memories.R + +class ConfigService(private val mCtx: Context) { + companion object { + private var mEnabledBuckets: Set? = null + } + + var enabledBucketIds: Set + get() { + if (mEnabledBuckets != null) return mEnabledBuckets!! + mEnabledBuckets = mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0) + .getStringSet(mCtx.getString(R.string.preferences_enabled_local_folders), null) ?: setOf() + return mEnabledBuckets!! + } + set(value) { + mEnabledBuckets = value + mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0).edit() + .putStringSet(mCtx.getString(R.string.preferences_enabled_local_folders), value) + .apply() + } +} \ No newline at end of file diff --git a/app/src/main/java/gallery/memories/service/TimelineQuery.kt b/app/src/main/java/gallery/memories/service/TimelineQuery.kt index fe27898a..c3c1cdfa 100644 --- a/app/src/main/java/gallery/memories/service/TimelineQuery.kt +++ b/app/src/main/java/gallery/memories/service/TimelineQuery.kt @@ -26,6 +26,7 @@ import java.util.concurrent.CountDownLatch @UnstableApi class TimelineQuery(private val mCtx: MainActivity) { private val mDbService = DbService(mCtx).initialize() + private val mConfigService = ConfigService(mCtx) private val TAG = TimelineQuery::class.java.simpleName // Photo deletion events @@ -33,9 +34,6 @@ import java.util.concurrent.CountDownLatch var deleteIntentLauncher: ActivityResultLauncher var deleteCallback: ((ActivityResult?) -> Unit)? = null - // Caches - var mEnabledBuckets: Set? = null - // Observers var imageObserver: ContentObserver? = null var videoObserver: ContentObserver? = null @@ -110,7 +108,7 @@ import java.util.concurrent.CountDownLatch @Throws(JSONException::class) fun getByDayId(dayId: Long): JSONArray { // Get the photos for the day from DB - val dbPhotos = mDbService.getPhotosByDay(dayId, getEnabledBucketIds()) + val dbPhotos = mDbService.getPhotosByDay(dayId, mConfigService.enabledBucketIds) val fileIds = dbPhotos.map { it.localId }.toMutableList() if (fileIds.isEmpty()) return JSONArray() @@ -131,7 +129,7 @@ import java.util.concurrent.CountDownLatch @Throws(JSONException::class) fun getDays(): JSONArray { - return mDbService.getDays(getEnabledBucketIds()).map { day -> day.json }.let { JSONArray(it) } + return mDbService.getDays(mConfigService.enabledBucketIds).map { day -> day.json }.let { JSONArray(it) } } @Throws(Exception::class) @@ -155,23 +153,11 @@ import java.util.concurrent.CountDownLatch try { val exif = ExifInterface(image.dataPath) - obj.put(Fields.Photo.EXIF, JSONObject() - .put("Aperture", exif.getAttribute(ExifInterface.TAG_APERTURE_VALUE)) - .put("FocalLength", exif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH)) - .put("FNumber", exif.getAttribute(ExifInterface.TAG_F_NUMBER)) - .put("ShutterSpeed", exif.getAttribute(ExifInterface.TAG_SHUTTER_SPEED_VALUE)) - .put("ExposureTime", exif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME)) - .put("ISO", exif.getAttribute(ExifInterface.TAG_ISO_SPEED)) - .put("DateTimeOriginal", exif.getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL)) - .put("OffsetTimeOriginal", exif.getAttribute(ExifInterface.TAG_OFFSET_TIME_ORIGINAL)) - .put("GPSLatitude", exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE)) - .put("GPSLongitude", exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE)) - .put("GPSAltitude", exif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE)) - .put("Make", exif.getAttribute(ExifInterface.TAG_MAKE)) - .put("Model", exif.getAttribute(ExifInterface.TAG_MODEL)) - .put("Orientation", exif.getAttribute(ExifInterface.TAG_ORIENTATION)) - .put("Description", exif.getAttribute(ExifInterface.TAG_IMAGE_DESCRIPTION)) - ) + obj.put(Fields.Photo.EXIF, JSONObject().apply { + Fields.EXIF.MAP.forEach { (key, field) -> + put(field, exif.getAttribute(key)) + } + }) } catch (e: IOException) { Log.w(TAG, "Error reading EXIF data for $id") } @@ -300,36 +286,24 @@ import java.util.concurrent.CountDownLatch Log.v(TAG, "Inserted file to local DB: $fileId / $baseName") } - fun getEnabledBucketIds(): Set { - if (mEnabledBuckets != null) return mEnabledBuckets!! - mEnabledBuckets = mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0) - .getStringSet(mCtx.getString(R.string.preferences_enabled_local_folders), null) ?: setOf() - return mEnabledBuckets!! - } - - fun getLocalFoldersConfig(): JSONArray { - val enabledSet = getEnabledBucketIds() - - return mDbService.getBuckets().map { - JSONObject() - .put("id", it.key) - .put("name", it.value) - .put("enabled", enabledSet.contains(it.key)) - }.let { JSONArray(it) } - } - - fun configSetLocalFolders(json: String) { - val enabledSet = mutableSetOf() - val array = JSONArray(json) - for (i in 0 until array.length()) { - val obj = array.getJSONObject(i) - if (obj.getBoolean("enabled")) { - enabledSet.add(obj.getLong("id").toString()) - } + /** This is in timeline query because it calls the database service */ + var localFolders: JSONArray + get() { + return mDbService.getBuckets().map { + JSONObject() + .put(Fields.Bucket.ID, it.key) + .put(Fields.Bucket.NAME, it.value) + .put(Fields.Bucket.ENABLED, mConfigService.enabledBucketIds.contains(it.key)) + }.let { JSONArray(it) } + } + set(value) { + val enabledSet = mutableSetOf() + for (i in 0 until value.length()) { + val obj = value.getJSONObject(i) + if (obj.getBoolean(Fields.Bucket.ENABLED)) { + enabledSet.add(obj.getString(Fields.Bucket.ID)) + } + } + mConfigService.enabledBucketIds = enabledSet } - mEnabledBuckets = enabledSet - mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0).edit() - .putStringSet(mCtx.getString(R.string.preferences_enabled_local_folders), enabledSet) - .apply() - } } \ No newline at end of file