Skip to content

Commit

Permalink
fix(android): fix crash when connection fails in hot mode (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
n8chur committed Sep 5, 2019
1 parent fd4294e commit ab9cd5b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 10 additions & 4 deletions packages/targets/sources/android/bindings/Typograph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import android.widget.TextView
import java.io.BufferedInputStream
import java.io.FileOutputStream
import android.os.StrictMode
import android.util.Log
import android.util.TypedValue

{{> androidDataClassStart }}

val typeface: Typeface
val typeface: Typeface?
get() {
if (foreignFontCache.containsKey(font.name)) {
return foreignFontCache.get(font.name)!!
Expand Down Expand Up @@ -123,7 +124,12 @@ private fun hotLoadFont(font: Font) {
val tempFile = CoreFile.createTempFile(tempPrefix, tempSuffix)
val url = font.file.url
val connection = url.openConnection()
connection.connect()
try {
connection.connect()
} catch (e: Exception) {
Log.e("DIEZ", e.toString())
return
}
val input = BufferedInputStream(url.openStream(), 8192)
val output = FileOutputStream(tempFile)
val data = ByteArray(1024)
Expand All @@ -142,10 +148,10 @@ private fun hotLoadFont(font: Font) {
StrictMode.setThreadPolicy(oldThreadPolicy)
}

private fun getTypeface(font: Font): Typeface {
private fun getTypeface(font: Font): Typeface? {
if (Environment.isHot) {
hotLoadFont(font)
return foreignFontCache.get(font.name)!!
return foreignFontCache.get(font.name)
}

val typeface = Environment.resources.getFont(font.file.resourceId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.widget.TextView
import java.io.BufferedInputStream
import java.io.FileOutputStream
import android.os.StrictMode
import android.util.Log
import android.util.TypedValue

data class Typograph(
Expand All @@ -16,7 +17,7 @@ data class Typograph(
) {
companion object {}

val typeface: Typeface
val typeface: Typeface?
get() {
if (foreignFontCache.containsKey(font.name)) {
return foreignFontCache.get(font.name)!!
Expand Down Expand Up @@ -129,7 +130,12 @@ private fun hotLoadFont(font: Font) {
val tempFile = CoreFile.createTempFile(tempPrefix, tempSuffix)
val url = font.file.url
val connection = url.openConnection()
connection.connect()
try {
connection.connect()
} catch (e: Exception) {
Log.e("DIEZ", e.toString())
return
}
val input = BufferedInputStream(url.openStream(), 8192)
val output = FileOutputStream(tempFile)
val data = ByteArray(1024)
Expand All @@ -148,10 +154,10 @@ private fun hotLoadFont(font: Font) {
StrictMode.setThreadPolicy(oldThreadPolicy)
}

private fun getTypeface(font: Font): Typeface {
private fun getTypeface(font: Font): Typeface? {
if (Environment.isHot) {
hotLoadFont(font)
return foreignFontCache.get(font.name)!!
return foreignFontCache.get(font.name)
}

val typeface = Environment.resources.getFont(font.file.resourceId)
Expand Down

0 comments on commit ab9cd5b

Please sign in to comment.