Skip to content

Commit 9e4e9df

Browse files
committed
Stability fix.
1 parent aa0f4b1 commit 9e4e9df

File tree

2 files changed

+87
-19
lines changed

2 files changed

+87
-19
lines changed

Main/src/main/java/com/redelf/commons/extensions.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import android.widget.Toast
2323
import com.google.android.gms.auth.api.signin.GoogleSignIn
2424
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
2525
import com.google.firebase.crashlytics.FirebaseCrashlytics
26+
import com.redelf.commons.execution.Execution
2627
import com.redelf.commons.execution.Executor
2728
import com.redelf.commons.persistance.PropertiesHash
2829
import timber.log.Timber
@@ -597,7 +598,7 @@ fun exec(
597598
timeout: Long = 30L,
598599
timeUnit: TimeUnit = TimeUnit.SECONDS,
599600
logTag: String = "Bool exec ::",
600-
executor: Executor? = null,
601+
executor: Execution? = null,
601602
debug: Boolean = false
602603

603604
): Boolean {
@@ -626,25 +627,25 @@ fun <T> doExec(
626627
timeout: Long = 30L,
627628
timeUnit: TimeUnit = TimeUnit.SECONDS,
628629
logTag: String = "Do exec ::",
629-
executor: Executor? = null,
630+
executor: Execution? = null,
630631
debug: Boolean = false
631632

632633
): T? {
633634

634635
var success: T? = null
635636
var future: Future<T>? = null
636637

637-
executor?.let {
638+
try {
638639

639-
future = it.execute(callable)
640-
}
640+
executor?.let {
641641

642-
if (executor == null) {
642+
future = it.execute(callable)
643+
}
643644

644-
future = Executor.MAIN.execute(callable)
645-
}
645+
if (executor == null) {
646646

647-
try {
647+
future = Executor.MAIN.execute(callable)
648+
}
648649

649650
if (debug) {
650651

Main/src/main/java/com/redelf/commons/persistance/Data.kt

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@ import android.content.Context
44
import android.text.TextUtils
55
import com.redelf.commons.doExec
66
import com.redelf.commons.exec
7+
import com.redelf.commons.execution.Execution
78
import com.redelf.commons.execution.Executor
9+
import com.redelf.commons.execution.TaskExecutor
810
import com.redelf.commons.persistance.Facade.EmptyFacade
11+
import com.redelf.commons.recordException
12+
import timber.log.Timber
913
import java.util.concurrent.Callable
14+
import java.util.concurrent.Future
15+
import java.util.concurrent.RejectedExecutionException
1016

1117
/**
1218
* Secure, simple key-value storage for Android.
1319
*/
1420
object Data {
1521

1622
private var facade: Facade? = EmptyFacade()
23+
private val executor = TaskExecutor.instantiateSingle()
1724

1825
fun init(
1926

@@ -58,7 +65,9 @@ object Data {
5865
*/
5966
fun <T> put(key: String?, value: T): Boolean {
6067

61-
return facade?.put(key, value) ?: false
68+
val callable = Callable { facade?.put(key, value) ?: false }
69+
70+
return exec(callable, executor = getExecutor())
6271
}
6372

6473
/**
@@ -71,7 +80,9 @@ object Data {
7180
*/
7281
operator fun <T> get(key: String?): T? {
7382

74-
return facade?.get(key)
83+
val callable = Callable<T?> { facade?.get(key) }
84+
85+
return doExec(callable, executor = getExecutor(), logTag = "Do exec :: Get :: $key")
7586
}
7687

7788
/**
@@ -83,7 +94,9 @@ object Data {
8394
*/
8495
operator fun <T> get(key: String?, defaultValue: T): T {
8596

86-
return facade?.get(key, defaultValue) ?: defaultValue
97+
val callable = Callable<T?> { facade?.get(key, defaultValue) }
98+
99+
return doExec(callable, executor = getExecutor()) ?: defaultValue
87100
}
88101

89102
/**
@@ -93,7 +106,9 @@ object Data {
93106
*/
94107
fun count(): Long {
95108

96-
return facade?.count() ?: 0
109+
val callable = Callable<Long> { facade?.count() }
110+
111+
return doExec(callable, executor = getExecutor()) ?: -1
97112
}
98113

99114
/**
@@ -104,12 +119,16 @@ object Data {
104119
*/
105120
fun deleteAll(): Boolean {
106121

107-
return facade?.deleteAll() ?: false
122+
val callable = Callable { facade?.deleteAll() ?: false }
123+
124+
return exec(callable, executor = getExecutor())
108125
}
109126

110127
fun deleteKeysWithPrefix(value: String): Boolean {
111128

112-
return facade?.deleteKeysWithPrefix(value) ?: true
129+
val callable = Callable { facade?.deleteKeysWithPrefix(value) ?: true }
130+
131+
return exec(callable, executor = getExecutor())
113132
}
114133

115134
/**
@@ -120,7 +139,9 @@ object Data {
120139
*/
121140
fun delete(key: String?): Boolean {
122141

123-
return facade?.delete(key) ?: false
142+
val callable = Callable { facade?.delete(key) ?: false }
143+
144+
return exec(callable, executor = getExecutor())
124145
}
125146

126147
/**
@@ -131,7 +152,9 @@ object Data {
131152
*/
132153
operator fun contains(key: String?): Boolean {
133154

134-
return facade?.contains(key) ?: false
155+
val callable = Callable { facade?.contains(key) ?: false }
156+
157+
return exec(callable, executor = getExecutor())
135158
}
136159

137160
val isBuilt: Boolean
@@ -142,11 +165,55 @@ object Data {
142165
*/
143166
get(): Boolean {
144167

145-
return facade?.isBuilt ?: false
168+
val callable = Callable { facade?.isBuilt ?: false }
169+
170+
return exec(callable, executor = getExecutor())
146171
}
147172

148173
fun destroy() {
149174

150-
facade?.destroy()
175+
getExecutor().execute {
176+
177+
facade?.destroy()
178+
}
179+
}
180+
181+
private fun getExecutor() = object : Execution {
182+
183+
@Throws(RejectedExecutionException::class)
184+
override fun <T> execute(callable: Callable<T>): Future<T> {
185+
186+
return executor.submit(callable)
187+
}
188+
189+
override fun execute(action: Runnable, delayInMillis: Long) {
190+
191+
executor.execute {
192+
193+
try {
194+
195+
Thread.sleep(delayInMillis)
196+
197+
action.run()
198+
199+
} catch (e: InterruptedException) {
200+
201+
Timber.e(e)
202+
}
203+
}
204+
}
205+
206+
override fun execute(what: Runnable) {
207+
208+
try {
209+
210+
executor.execute(what)
211+
212+
} catch (e: RejectedExecutionException) {
213+
214+
recordException(e)
215+
}
216+
}
217+
151218
}
152219
}

0 commit comments

Comments
 (0)