Skip to content

Commit

Permalink
Introduce onError callback method
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Jan 1, 2021
1 parent c9c2663 commit 1a03584
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AdobeColorTool(

interface AdobeImportListener {
fun onComplete(colors: Map<String, List<AdobeColor>>)
fun onError(e: Exception)
fun onCancel() {}
}

Expand Down Expand Up @@ -92,9 +93,15 @@ class AdobeColorTool(
override fun onSuccess(result: Result?) {
result?.data?.data?.let {
activity.contentResolver.openInputStream(it)?.let { data ->
val bytes = getBytes(data)
bytes.toColorList()?.let { list -> listener.onComplete(list) }
listener.onCancel()
try {
val bytes = getBytes(data)
bytes.toColorList()?.let { list ->
listener.onComplete(list)
return
}
listener.onError(Exception("Wrong file type"))
return
} catch (e: Exception) { listener.onError(e) }
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class MainActivity : AppCompatActivity() {
Toast.makeText(this@MainActivity, R.string.import_completed, Toast.LENGTH_SHORT).show()
}

override fun onError(e: Exception) {
Toast.makeText(this@MainActivity, R.string.import_failed, Toast.LENGTH_SHORT).show()
}

override fun onCancel() {
Toast.makeText(this@MainActivity, R.string.import_cancelled, Toast.LENGTH_SHORT).show()
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<string name="import_colors">Import Colors</string>
<string name="import_completed">Import completed</string>
<string name="import_cancelled">Import cancelled</string>
<string name="import_failed">Import failed due to an error</string>
</resources>

0 comments on commit 1a03584

Please sign in to comment.