From d66cc690827da68e1762b06028490b31a3eb19d0 Mon Sep 17 00:00:00 2001 From: Romman Sabbir Date: Thu, 5 May 2022 17:36:51 +0600 Subject: [PATCH] - Fixed issue #2 - APIs updated - New API added to remove list of cache from Cache Dir --- .../rommansabbir/storex/BaseStoreXInstance.kt | 4 ++++ .../java/com/rommansabbir/storex/StoreX.kt | 23 ++++--------------- .../com/rommansabbir/storex/StoreXInstance.kt | 16 +++++++++---- .../execptions/DuplicateKeyFoundException.kt | 2 +- .../DuplicateStoreXConfigException.kt | 2 +- .../InvalidEncryptionKeyException.kt | 2 +- .../execptions/NoConfigFoundException.kt | 2 +- .../execptions/NoStoreAbleObjectFound.kt | 2 +- .../execptions/NotInitializedException.kt | 2 +- 9 files changed, 26 insertions(+), 29 deletions(-) diff --git a/StoreX/src/main/java/com/rommansabbir/storex/BaseStoreXInstance.kt b/StoreX/src/main/java/com/rommansabbir/storex/BaseStoreXInstance.kt index 7ad2d01..e2ce933 100644 --- a/StoreX/src/main/java/com/rommansabbir/storex/BaseStoreXInstance.kt +++ b/StoreX/src/main/java/com/rommansabbir/storex/BaseStoreXInstance.kt @@ -79,6 +79,10 @@ abstract class BaseStoreXInstance( } } + internal fun clearCacheFromCacheDir(key: String) { + application.cacheDir?.listFiles()?.find { it.name == key }?.delete() + } + override fun registerListener(listener: SharedPreferences.OnSharedPreferenceChangeListener) { this.mSharedPreferences.registerOnSharedPreferenceChangeListener(listener) } diff --git a/StoreX/src/main/java/com/rommansabbir/storex/StoreX.kt b/StoreX/src/main/java/com/rommansabbir/storex/StoreX.kt index 7fa3bcc..d6d50bb 100644 --- a/StoreX/src/main/java/com/rommansabbir/storex/StoreX.kt +++ b/StoreX/src/main/java/com/rommansabbir/storex/StoreX.kt @@ -5,20 +5,10 @@ import com.rommansabbir.storex.callbacks.SaveCallback import kotlinx.coroutines.CoroutineScope interface StoreX { - @Deprecated( - "Use new method with Coroutine Support", - replaceWith = ReplaceWith("StoreX.put(scope : CoroutineScope, key: String, value: StoreAbleObject)") - ) - @Throws(RuntimeException::class) fun put(key: String, value: StoreAbleObject): Boolean - @Throws(RuntimeException::class) fun put(scope: CoroutineScope, key: String, value: StoreAbleObject) - @Deprecated( - "Use new method with Coroutine Support", - replaceWith = ReplaceWith("StoreX.put(scope: CoroutineScope, key: String, value: StoreAbleObject, callback: SaveCallback)") - ) fun put(key: String, value: StoreAbleObject, callback: SaveCallback) fun put( @@ -28,13 +18,8 @@ interface StoreX { callback: SaveCallback ) - @Throws(RuntimeException::class) fun get(key: String, objectType: Class): T - @Deprecated( - "Use new method with Coroutine Support", - replaceWith = ReplaceWith("StoreX.get(scope: CoroutineScope, key: String, objectType: Class, callback: GetCallback)") - ) fun get(key: String, objectType: Class, callback: GetCallback) fun get( @@ -44,17 +29,17 @@ interface StoreX { callback: GetCallback ) - @Throws(RuntimeException::class) fun addSubscriber(subscriber: Subscriber) - @Throws(RuntimeException::class) - fun addSubscriber(subscribers: ArrayList) + fun addSubscriber(subscribers: List) fun removeSubscriber(subscriber: Subscriber) - fun removeSubscriber(subscribers: ArrayList) + fun removeSubscriber(subscribers: List) fun remove(key: String) + fun removeFromCacheDir(key: List): Boolean + fun removeAll() } \ No newline at end of file diff --git a/StoreX/src/main/java/com/rommansabbir/storex/StoreXInstance.kt b/StoreX/src/main/java/com/rommansabbir/storex/StoreXInstance.kt index ded7fb7..9aa1ca5 100644 --- a/StoreX/src/main/java/com/rommansabbir/storex/StoreXInstance.kt +++ b/StoreX/src/main/java/com/rommansabbir/storex/StoreXInstance.kt @@ -13,7 +13,6 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import java.util.* internal class StoreXInstance( @@ -254,7 +253,7 @@ internal class StoreXInstance( StoreXCore.addSubscriber(subscriber) } - override fun addSubscriber(subscribers: ArrayList) { + override fun addSubscriber(subscribers: List) { subscribers.forEach { addSubscriber(it) } @@ -264,14 +263,23 @@ internal class StoreXInstance( StoreXCore.removeSubscriber(subscriber) } - override fun removeSubscriber(subscribers: ArrayList) { + override fun removeSubscriber(subscribers: List) { subscribers.forEach { removeSubscriber(it) } } override fun remove(key: String) { - clearCacheByKey(key) + if (writeOrGetAsFileUsingCacheDirectory) { + clearCacheFromCacheDir(key) + } else { + clearCacheByKey(key) + } + } + + override fun removeFromCacheDir(key: List): Boolean { + key.forEach { clearCacheFromCacheDir(it) } + return true } override fun removeAll() { diff --git a/StoreX/src/main/java/com/rommansabbir/storex/execptions/DuplicateKeyFoundException.kt b/StoreX/src/main/java/com/rommansabbir/storex/execptions/DuplicateKeyFoundException.kt index 37eca46..b37f12d 100644 --- a/StoreX/src/main/java/com/rommansabbir/storex/execptions/DuplicateKeyFoundException.kt +++ b/StoreX/src/main/java/com/rommansabbir/storex/execptions/DuplicateKeyFoundException.kt @@ -1,4 +1,4 @@ package com.rommansabbir.storex.execptions class DuplicateKeyFoundException(@JvmField override val message: String = "This subscriber is already present in the StoreX subscribers list. Make sure your SUBSCRIBER_ID is unique or else remove the current subscriber and add new one") : - RuntimeException(message) \ No newline at end of file + Exception(message) \ No newline at end of file diff --git a/StoreX/src/main/java/com/rommansabbir/storex/execptions/DuplicateStoreXConfigException.kt b/StoreX/src/main/java/com/rommansabbir/storex/execptions/DuplicateStoreXConfigException.kt index a49f23a..b2db93b 100644 --- a/StoreX/src/main/java/com/rommansabbir/storex/execptions/DuplicateStoreXConfigException.kt +++ b/StoreX/src/main/java/com/rommansabbir/storex/execptions/DuplicateStoreXConfigException.kt @@ -1,4 +1,4 @@ package com.rommansabbir.storex.execptions class DuplicateStoreXConfigException(@JvmField override val message: String = "Duplicate StoreXConfig found.") : - RuntimeException(message) \ No newline at end of file + Exception(message) \ No newline at end of file diff --git a/StoreX/src/main/java/com/rommansabbir/storex/execptions/InvalidEncryptionKeyException.kt b/StoreX/src/main/java/com/rommansabbir/storex/execptions/InvalidEncryptionKeyException.kt index fb4dd40..50293e6 100644 --- a/StoreX/src/main/java/com/rommansabbir/storex/execptions/InvalidEncryptionKeyException.kt +++ b/StoreX/src/main/java/com/rommansabbir/storex/execptions/InvalidEncryptionKeyException.kt @@ -1,4 +1,4 @@ package com.rommansabbir.storex.execptions class InvalidEncryptionKeyException(@JvmField override val message: String = "Invalid encryption key") : - RuntimeException(message) \ No newline at end of file + Exception(message) \ No newline at end of file diff --git a/StoreX/src/main/java/com/rommansabbir/storex/execptions/NoConfigFoundException.kt b/StoreX/src/main/java/com/rommansabbir/storex/execptions/NoConfigFoundException.kt index 87c75e5..29a4254 100644 --- a/StoreX/src/main/java/com/rommansabbir/storex/execptions/NoConfigFoundException.kt +++ b/StoreX/src/main/java/com/rommansabbir/storex/execptions/NoConfigFoundException.kt @@ -1,4 +1,4 @@ package com.rommansabbir.storex.execptions class NoConfigFoundException(@JvmField override val message: String = "No valid instance found for this StoreXConfig") : - RuntimeException(message) \ No newline at end of file + Exception(message) \ No newline at end of file diff --git a/StoreX/src/main/java/com/rommansabbir/storex/execptions/NoStoreAbleObjectFound.kt b/StoreX/src/main/java/com/rommansabbir/storex/execptions/NoStoreAbleObjectFound.kt index 284869f..b041a4c 100644 --- a/StoreX/src/main/java/com/rommansabbir/storex/execptions/NoStoreAbleObjectFound.kt +++ b/StoreX/src/main/java/com/rommansabbir/storex/execptions/NoStoreAbleObjectFound.kt @@ -1,4 +1,4 @@ package com.rommansabbir.storex.execptions class NoStoreAbleObjectFound(@JvmField override val message: String = "No StoreAbleObject found associated with this key") : - RuntimeException(message) \ No newline at end of file + Exception(message) \ No newline at end of file diff --git a/StoreX/src/main/java/com/rommansabbir/storex/execptions/NotInitializedException.kt b/StoreX/src/main/java/com/rommansabbir/storex/execptions/NotInitializedException.kt index 1f22c76..e58db0e 100644 --- a/StoreX/src/main/java/com/rommansabbir/storex/execptions/NotInitializedException.kt +++ b/StoreX/src/main/java/com/rommansabbir/storex/execptions/NotInitializedException.kt @@ -1,4 +1,4 @@ package com.rommansabbir.storex.execptions class NotInitializedException(@JvmField override val message: String = "StoreX is not initialized properly") : - RuntimeException(message) \ No newline at end of file + Exception(message) \ No newline at end of file