From d766100c297bc094491de150f24c04719ffa8f4e Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 18 Jan 2018 23:23:56 -0500 Subject: Enhancement/speed up (#650) * Revert back to m.facebook * Add initial speedup * Update theme * Fix link press for event status * Move web states to fb const * Fix images and email * Fix up flyweight for requests * Ensure frost request is synchronous * Prepare diff utils * Improve speed and fix blank overlay * Update comments * Add debugger and fix searchview * Theme discover pages. Resolves #654 * Fix duplicate reload * Fix image loading * Update changelog * Update tests * Rename test Update dependencies Update gitignore --- .../pitchedapps/frost/activities/ImageActivity.kt | 32 +++++++++++++--------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt index cdde8311..e563ff8a 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt @@ -88,15 +88,18 @@ class ImageActivity : KauBaseActivity() { val IMAGE_URL: String by lazy { intent.getStringExtra(ARG_IMAGE_URL).trim('"') } - val TEXT: String? by lazy { intent.getStringExtra(ARG_TEXT) } + private val TEXT: String? by lazy { intent.getStringExtra(ARG_TEXT) } // a unique image identifier based on the id (if it exists), and its hash - val IMAGE_HASH: String by lazy { "${Math.abs(FB_IMAGE_ID_MATCHER.find(IMAGE_URL)[1]?.hashCode() ?: 0)}_${Math.abs(IMAGE_URL.hashCode())}" } + private val IMAGE_HASH: String by lazy { + "${Math.abs(FB_IMAGE_ID_MATCHER.find(IMAGE_URL)[1]?.hashCode() ?: 0)}_${Math.abs(IMAGE_URL.hashCode())}" + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) intent?.extras ?: return finish() L.i { "Displaying image" } + L.v { "Displaying image $IMAGE_URL" } val layout = if (!TEXT.isNullOrBlank()) R.layout.activity_image else R.layout.activity_image_textless setContentView(layout) container.setBackgroundColor(Prefs.bgColor.withMinAlpha(222)) @@ -116,6 +119,8 @@ class ImageActivity : KauBaseActivity() { override fun onImageLoadError(e: Exception?) { errorRef = e e.logFrostAnswers("Image load error") + L.e { "Failed to load image $IMAGE_URL" } + tempFile?.delete() fabAction = FabStates.ERROR } }) @@ -125,9 +130,12 @@ class ImageActivity : KauBaseActivity() { doAsync({ L.e(it) { "Failed to load image $IMAGE_HASH" } errorRef = it + runOnUiThread { progress.fadeOut() } + tempFile?.delete() fabAction = FabStates.ERROR }) { loadImage { file -> + uiThread { progress.fadeOut() } if (file == null) { fabAction = FabStates.ERROR return@loadImage @@ -153,11 +161,7 @@ class ImageActivity : KauBaseActivity() { L.d { "Loading from local cache ${local.absolutePath}" } return callback(local) } - val response = Request.Builder() - .url(IMAGE_URL) - .get() - .call() - .execute() + val response = getImageResponse() if (!response.isSuccessful) { L.e { "Unsuccessful response for image" } @@ -198,14 +202,16 @@ class ImageActivity : KauBaseActivity() { return File.createTempFile(imageFileName, IMG_EXTENSION, frostDir) } + private fun getImageResponse() = Request.Builder() + .url(IMAGE_URL) + .get() + .call() + .execute() + @Throws(IOException::class) private fun downloadImageTo(file: File) { - val body = Request.Builder() - .url(IMAGE_URL) - .get() - .call() - .execute() - .body() ?: throw IOException("Failed to retrieve image body") + val body = getImageResponse().body() + ?: throw IOException("Failed to retrieve image body") body.byteStream().use { input -> file.outputStream().use { output -> input.copyTo(output) -- cgit v1.2.3