diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/values/StaticValuePart.java b/app/src/main/java/com/codigoparallevar/minicards/parts/values/StaticValuePart.java index 3f42251..1590eca 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/values/StaticValuePart.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/values/StaticValuePart.java @@ -31,6 +31,7 @@ import com.codigoparallevar.minicards.types.functional.Tuple3; import com.codigoparallevar.minicards.types.wireData.WireDataType; import com.codigoparallevar.minicards.ui_helpers.GetAsync; import com.codigoparallevar.minicards.utils.Serializations; +import com.programaker.api.ProgramakerApi; import com.programaker.api.data.ProgramakerCustomBlockArgument; import com.programaker.api.data.ProgramakerCustomBlockArgumentValue; @@ -290,12 +291,15 @@ public class StaticValuePart implements Part { break; } + ProgramakerApi api = _grid.getApi(); Dialog loadingDialog = createLoadingDialog(ctx); - loadingDialog.show(); + if (!api.hasCachedAllowedValues(blockArg)) { + loadingDialog.show(); + } new GetAsync>().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Tuple3<>( - () -> _grid.getApi().fetchAllowedValues(blockArg), + () -> api.fetchAllowedValues(blockArg), results -> { Log.i(LogTag, "Result: " + results); loadingDialog.cancel(); @@ -330,7 +334,7 @@ public class StaticValuePart implements Part { private Dialog createItemDialog(Context ctx, CharSequence[] itemNames, Consumer onSelected) { 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); progressBar.setIndeterminate(true); diff --git a/app/src/main/java/com/programaker/api/ProgramakerApi.kt b/app/src/main/java/com/programaker/api/ProgramakerApi.kt index 61b68cd..4780e2f 100644 --- a/app/src/main/java/com/programaker/api/ProgramakerApi.kt +++ b/app/src/main/java/com/programaker/api/ProgramakerApi.kt @@ -22,6 +22,7 @@ import java.net.URLConnection class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") { + private val enumCallbackCache: MutableMap> = HashMap() private val LogTag: String = "ProgramakerApi" private var userId: String? = null @@ -137,7 +138,13 @@ class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") } fun fetchAllowedValues(blockArg: ProgramakerCustomBlockArgument): List? { - 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) @@ -156,6 +163,7 @@ class ProgramakerApi(private val ApiRoot: String="https://programaker.com/api") Log.e(LogTag, ex.toString(), ex) return null } + cacheValues(url, 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) { + enumCallbackCache[url] = results + } + + private fun cachedAllowedValues(url: String): List? { + 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 parseJson(content: InputStream, resultClass: Type): T { val reader = InputStreamReader(content) val gson = Gson()