Skip to content

Commit

Permalink
- Fixed issue #2
Browse files Browse the repository at this point in the history
- APIs updated
- New API added to remove list of cache from Cache Dir
  • Loading branch information
rommansabbir committed May 5, 2022
1 parent 6c6b245 commit d66cc69
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
23 changes: 4 additions & 19 deletions StoreX/src/main/java/com/rommansabbir/storex/StoreX.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>)")
)
fun <T : StoreAbleObject> put(key: String, value: StoreAbleObject, callback: SaveCallback<T>)

fun <T : StoreAbleObject> put(
Expand All @@ -28,13 +18,8 @@ interface StoreX {
callback: SaveCallback<T>
)

@Throws(RuntimeException::class)
fun <T : StoreAbleObject> get(key: String, objectType: Class<T>): T

@Deprecated(
"Use new method with Coroutine Support",
replaceWith = ReplaceWith("StoreX.get(scope: CoroutineScope, key: String, objectType: Class<T>, callback: GetCallback<T>)")
)
fun <T : StoreAbleObject> get(key: String, objectType: Class<T>, callback: GetCallback<T>)

fun <T : StoreAbleObject> get(
Expand All @@ -44,17 +29,17 @@ interface StoreX {
callback: GetCallback<T>
)

@Throws(RuntimeException::class)
fun addSubscriber(subscriber: Subscriber)

@Throws(RuntimeException::class)
fun addSubscriber(subscribers: ArrayList<Subscriber>)
fun addSubscriber(subscribers: List<Subscriber>)

fun removeSubscriber(subscriber: Subscriber)

fun removeSubscriber(subscribers: ArrayList<Subscriber>)
fun removeSubscriber(subscribers: List<Subscriber>)

fun remove(key: String)

fun removeFromCacheDir(key: List<String>): Boolean

fun removeAll()
}
16 changes: 12 additions & 4 deletions StoreX/src/main/java/com/rommansabbir/storex/StoreXInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -254,7 +253,7 @@ internal class StoreXInstance(
StoreXCore.addSubscriber(subscriber)
}

override fun addSubscriber(subscribers: ArrayList<Subscriber>) {
override fun addSubscriber(subscribers: List<Subscriber>) {
subscribers.forEach {
addSubscriber(it)
}
Expand All @@ -264,14 +263,23 @@ internal class StoreXInstance(
StoreXCore.removeSubscriber(subscriber)
}

override fun removeSubscriber(subscribers: ArrayList<Subscriber>) {
override fun removeSubscriber(subscribers: List<Subscriber>) {
subscribers.forEach {
removeSubscriber(it)
}
}

override fun remove(key: String) {
clearCacheByKey(key)
if (writeOrGetAsFileUsingCacheDirectory) {
clearCacheFromCacheDir(key)
} else {
clearCacheByKey(key)
}
}

override fun removeFromCacheDir(key: List<String>): Boolean {
key.forEach { clearCacheFromCacheDir(it) }
return true
}

override fun removeAll() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Exception(message)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rommansabbir.storex.execptions

class DuplicateStoreXConfigException(@JvmField override val message: String = "Duplicate StoreXConfig found.") :
RuntimeException(message)
Exception(message)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rommansabbir.storex.execptions

class InvalidEncryptionKeyException(@JvmField override val message: String = "Invalid encryption key") :
RuntimeException(message)
Exception(message)
Original file line number Diff line number Diff line change
@@ -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)
Exception(message)
Original file line number Diff line number Diff line change
@@ -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)
Exception(message)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rommansabbir.storex.execptions

class NotInitializedException(@JvmField override val message: String = "StoreX is not initialized properly") :
RuntimeException(message)
Exception(message)

0 comments on commit d66cc69

Please sign in to comment.