From 0eee69bacb5d15319e2e10922ff279f4cd5d12d5 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sun, 1 Oct 2023 19:26:11 -0700 Subject: [PATCH] Refactor --- .../memories/service/AccountService.kt | 80 ++++++++++--------- .../gallery/memories/service/TimelineQuery.kt | 21 ++++- 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/gallery/memories/service/AccountService.kt b/app/src/main/java/gallery/memories/service/AccountService.kt index 945394f1..d00d3b61 100644 --- a/app/src/main/java/gallery/memories/service/AccountService.kt +++ b/app/src/main/java/gallery/memories/service/AccountService.kt @@ -5,6 +5,7 @@ import android.net.Uri import android.util.Base64 import android.util.Log import android.widget.Toast +import androidx.media3.common.util.UnstableApi import gallery.memories.MainActivity import gallery.memories.R import io.github.g00fy2.versioncompare.Version @@ -16,6 +17,7 @@ import okhttp3.Response import org.json.JSONObject import java.net.SocketTimeoutException +@UnstableApi class AccountService(private val mCtx: MainActivity) { companion object { val TAG = AccountService::class.java.simpleName @@ -140,53 +142,53 @@ class AccountService(private val mCtx: MainActivity) { } fun checkCredentialsAndVersion() { - memoriesUrl.let { base -> - val request = Request.Builder() - .url(base + "api/describe") - .get() - .header("Authorization", authHeader ?: "") - .build() + if (memoriesUrl == null) return - val response: Response - try { - response = OkHttpClient().newCall(request).execute() - } catch (e: Exception) { - Log.w(TAG, "checkCredentialsAndVersion: ", e) - return - } + val request = Request.Builder() + .url(memoriesUrl + "api/describe") + .get() + .header("Authorization", authHeader ?: "") + .build() - val body = response.body?.string() - response.body?.close() + val response: Response + try { + response = OkHttpClient().newCall(request).execute() + } catch (e: Exception) { + Log.w(TAG, "checkCredentialsAndVersion: ", e) + return + } - // Check status code - if (response.code == 401) { - return loggedOut() - } + val body = response.body?.string() + response.body?.close() - // Could not connect to memories - if (response.code == 404) { - return toast(mCtx.getString(R.string.err_no_ver)) - } + // Check status code + if (response.code == 401) { + return loggedOut() + } - // Check body - if (body == null || response.code != 200) { - toast(mCtx.getString(R.string.err_no_describe)) - return - } + // Could not connect to memories + if (response.code == 404) { + return toast(mCtx.getString(R.string.err_no_ver)) + } - val json = JSONObject(body) - val version = json.getString("version") - val uid = json.get("uid") + // Check body + if (body == null || response.code != 200) { + toast(mCtx.getString(R.string.err_no_describe)) + return + } - // Check UID exists - if (uid.equals(null) && authHeader != null) { - return loggedOut() - } + val json = JSONObject(body) + val version = json.getString("version") + val uid = json.get("uid") - // Check minimum version - if (Version(version) < Version(mCtx.getString(R.string.min_server_version))) { - return toast(mCtx.getString(R.string.err_no_ver)) - } + // Check UID exists + if (uid.equals(null) && authHeader != null) { + return loggedOut() + } + + // Check minimum version + if (Version(version) < Version(mCtx.getString(R.string.min_server_version))) { + return toast(mCtx.getString(R.string.err_no_ver)) } } diff --git a/app/src/main/java/gallery/memories/service/TimelineQuery.kt b/app/src/main/java/gallery/memories/service/TimelineQuery.kt index 31025a1b..4d612b70 100644 --- a/app/src/main/java/gallery/memories/service/TimelineQuery.kt +++ b/app/src/main/java/gallery/memories/service/TimelineQuery.kt @@ -26,7 +26,8 @@ import java.io.IOException import java.time.Instant import java.util.concurrent.CountDownLatch -@UnstableApi class TimelineQuery(private val mCtx: MainActivity) { +@UnstableApi +class TimelineQuery(private val mCtx: MainActivity) { private val TAG = TimelineQuery::class.java.simpleName private val mConfigService = ConfigService(mCtx) @@ -228,7 +229,7 @@ import java.util.concurrent.CountDownLatch } // Delete from database - mPhotoDao.deleteFileIds(sysImgs.map { it.fileId}) + mPhotoDao.deleteFileIds(sysImgs.map { it.fileId }) } finally { synchronized(this) { deleting = false } } @@ -254,13 +255,25 @@ import java.util.concurrent.CountDownLatch var updates = 0 // Iterate all images from system store - for (image in SystemImage.cursor(mCtx, SystemImage.IMAGE_URI, selection, selectionArgs, null)) { + for (image in SystemImage.cursor( + mCtx, + SystemImage.IMAGE_URI, + selection, + selectionArgs, + null + )) { insertItemDb(image) updates++ } // Iterate all videos from system store - for (video in SystemImage.cursor(mCtx, SystemImage.VIDEO_URI, selection, selectionArgs, null)) { + for (video in SystemImage.cursor( + mCtx, + SystemImage.VIDEO_URI, + selection, + selectionArgs, + null + )) { insertItemDb(video) updates++ }