Add simple memory cache for enum/callback values.

This commit is contained in:
Sergio Martínez Portela 2020-06-02 13:23:15 +02:00
parent 75421d4c95
commit 7cea4a293c
2 changed files with 39 additions and 4 deletions

View File

@ -31,6 +31,7 @@ import com.codigoparallevar.minicards.types.functional.Tuple3;
import com.codigoparallevar.minicards.types.wireData.WireDataType; import com.codigoparallevar.minicards.types.wireData.WireDataType;
import com.codigoparallevar.minicards.ui_helpers.GetAsync; import com.codigoparallevar.minicards.ui_helpers.GetAsync;
import com.codigoparallevar.minicards.utils.Serializations; import com.codigoparallevar.minicards.utils.Serializations;
import com.programaker.api.ProgramakerApi;
import com.programaker.api.data.ProgramakerCustomBlockArgument; import com.programaker.api.data.ProgramakerCustomBlockArgument;
import com.programaker.api.data.ProgramakerCustomBlockArgumentValue; import com.programaker.api.data.ProgramakerCustomBlockArgumentValue;
@ -290,12 +291,15 @@ public class StaticValuePart implements Part {
break; break;
} }
ProgramakerApi api = _grid.getApi();
Dialog loadingDialog = createLoadingDialog(ctx); Dialog loadingDialog = createLoadingDialog(ctx);
if (!api.hasCachedAllowedValues(blockArg)) {
loadingDialog.show(); loadingDialog.show();
}
new GetAsync<List<ProgramakerCustomBlockArgumentValue>>().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new GetAsync<List<ProgramakerCustomBlockArgumentValue>>().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
new Tuple3<>( new Tuple3<>(
() -> _grid.getApi().fetchAllowedValues(blockArg), () -> api.fetchAllowedValues(blockArg),
results -> { results -> {
Log.i(LogTag, "Result: " + results); Log.i(LogTag, "Result: " + results);
loadingDialog.cancel(); loadingDialog.cancel();
@ -330,7 +334,7 @@ public class StaticValuePart implements Part {
private Dialog createItemDialog(Context ctx, CharSequence[] itemNames, Consumer<Integer> onSelected) { private Dialog createItemDialog(Context ctx, CharSequence[] itemNames, Consumer<Integer> onSelected) {
final AlertDialog.Builder builder = new AlertDialog.Builder(ctx); final AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(R.string.loading_available_values); builder.setTitle(R.string.set_value);
final ProgressBar progressBar = new ProgressBar(ctx); final ProgressBar progressBar = new ProgressBar(ctx);
progressBar.setIndeterminate(true); progressBar.setIndeterminate(true);

View File

@ -22,6 +22,7 @@ import java.net.URLConnection
class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") { class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") {
private val enumCallbackCache: MutableMap<String, List<ProgramakerCustomBlockArgumentValue>> = HashMap()
private val LogTag: String = "ProgramakerApi" private val LogTag: String = "ProgramakerApi"
private var userId: String? = null private var userId: String? = null
@ -137,7 +138,13 @@ class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api")
} }
fun fetchAllowedValues(blockArg: ProgramakerCustomBlockArgument): List<ProgramakerCustomBlockArgumentValue>? { fun fetchAllowedValues(blockArg: ProgramakerCustomBlockArgument): List<ProgramakerCustomBlockArgumentValue>? {
val conn = URL(getArgumentValuesUrl(blockArg)).openConnection() as HttpURLConnection val url = getArgumentValuesUrl(blockArg)
val cachedValues = cachedAllowedValues(url)
if (cachedValues != null) {
return cachedValues
}
val conn = URL(url).openConnection() as HttpURLConnection
addAuthHeader(conn) addAuthHeader(conn)
@ -156,6 +163,7 @@ class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api")
Log.e(LogTag, ex.toString(), ex) Log.e(LogTag, ex.toString(), ex)
return null return null
} }
cacheValues(url, results)
return results return results
} }
@ -344,6 +352,29 @@ class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api")
} }
} }
// Simple result caching
private fun cacheValues(url: String, results: List<ProgramakerCustomBlockArgumentValue>) {
enumCallbackCache[url] = results
}
private fun cachedAllowedValues(url: String): List<ProgramakerCustomBlockArgumentValue>? {
if (enumCallbackCache.containsKey(url)) {
return enumCallbackCache[url]
}
return null
}
fun hasCachedAllowedValues(blockArg: ProgramakerCustomBlockArgument): Boolean {
if (userId == null) {
// This will be required to fully form the URL.
// Also, without it, it's not possible to have cached values. We can safely discard it
return false
}
val url = getArgumentValuesUrl(blockArg)
return enumCallbackCache.containsKey(url)
}
// Utils
private fun <T>parseJson(content: InputStream, resultClass: Type): T { private fun <T>parseJson(content: InputStream, resultClass: Type): T {
val reader = InputStreamReader(content) val reader = InputStreamReader(content)
val gson = Gson() val gson = Gson()