Add simple error handling
This commit is contained in:
parent
4b67b29f20
commit
971e8e7a33
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
Binary file not shown.
Before Width: | Height: | Size: 140 KiB |
@ -3,12 +3,14 @@ package com.example.allyoucantweet.ui
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.animation.AlphaAnimation
|
||||
import android.view.animation.Animation
|
||||
import android.view.animation.DecelerateInterpolator
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.example.allyoucantweet.R
|
||||
import com.example.allyoucantweet.databinding.ActivityMainBinding
|
||||
import com.example.allyoucantweet.network.service.AYCTService
|
||||
import com.example.allyoucantweet.utils.empty
|
||||
import com.example.allyoucantweet.utils.gone
|
||||
import com.example.allyoucantweet.utils.show
|
||||
import com.google.gson.GsonBuilder
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
@ -35,27 +37,45 @@ class MainActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(binding.root)
|
||||
|
||||
binding.retry.setOnClickListener {
|
||||
it.gone()
|
||||
binding.hello.gone()
|
||||
getHelloMessage()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
CoroutineScope(IO).launch {
|
||||
try {
|
||||
val hello = service.getHello()
|
||||
Log.d("AYCT", "hello: $hello")
|
||||
runOnUiThread {
|
||||
binding.hello.apply {
|
||||
text = hello
|
||||
val fadeIn: Animation = AlphaAnimation(0f, 1f).apply {
|
||||
interpolator = DecelerateInterpolator()
|
||||
duration = 1500
|
||||
}
|
||||
animation = fadeIn
|
||||
}
|
||||
getHelloMessage()
|
||||
}
|
||||
|
||||
private fun getHelloMessage() = CoroutineScope(IO).launch {
|
||||
runOnUiThread {
|
||||
binding.progressCircular.show()
|
||||
binding.retry.gone()
|
||||
}
|
||||
var message = String.empty()
|
||||
try {
|
||||
message = service.getHello()
|
||||
} catch (e: Exception) {
|
||||
message = getString(R.string.sth_went_wrong)
|
||||
runOnUiThread { binding.retry.show() }
|
||||
Log.d("AYCT", "error message: ${e.message}")
|
||||
} finally {
|
||||
runOnUiThread {
|
||||
binding.hello.show()
|
||||
binding.progressCircular.gone()
|
||||
binding.hello.apply {
|
||||
text = message
|
||||
animation = this@MainActivity.getAnimation()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.d("AYCT", "error message: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getAnimation() = AlphaAnimation(0f, 1f).apply {
|
||||
interpolator = DecelerateInterpolator()
|
||||
duration = 1500
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.example.allyoucantweet.utils
|
||||
|
||||
import android.view.View
|
||||
|
||||
fun View.show() = this.apply {
|
||||
visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
fun View.gone() = this.apply {
|
||||
visibility = View.GONE
|
||||
}
|
||||
|
||||
fun String.Companion.empty() = ""
|
5
app/src/main/res/drawable/ic_sync.xml
Normal file
5
app/src/main/res/drawable/ic_sync.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:tint="#FFFFFF"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M12,4L12,1L8,5l4,4L12,6c3.31,0 6,2.69 6,6 0,1.01 -0.25,1.97 -0.7,2.8l1.46,1.46C19.54,15.03 20,13.57 20,12c0,-4.42 -3.58,-8 -8,-8zM12,18c-3.31,0 -6,-2.69 -6,-6 0,-1.01 0.25,-1.97 0.7,-2.8L5.24,7.74C4.46,8.97 4,10.43 4,12c0,4.42 3.58,8 8,8v3l4,-4 -4,-4v3z"/>
|
||||
</vector>
|
@ -5,27 +5,53 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/brand"
|
||||
android:padding="@dimen/app_margin"
|
||||
tools:context=".ui.MainActivity">
|
||||
|
||||
<com.google.android.material.imageview.ShapeableImageView
|
||||
<ImageView
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="200dp"
|
||||
android:contentDescription="@string/app_name"
|
||||
android:src="@mipmap/ic_launcher_foreground"
|
||||
app:layout_constraintBottom_toBottomOf="@id/hello"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progress_circular"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/retry"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/retry"
|
||||
android:visibility="gone"
|
||||
app:icon="@drawable/ic_sync"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/hello" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/hello"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAlignment="center"
|
||||
android:textColor="@color/brand_text"
|
||||
android:textSize="30sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:text="Hello There" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -6,7 +6,7 @@
|
||||
<item name="colorPrimaryVariant">@color/brand</item>
|
||||
<item name="colorOnPrimary">@color/black</item>
|
||||
<!-- Secondary brand color. -->
|
||||
<item name="colorSecondary">@color/teal_200</item>
|
||||
<item name="colorSecondary">@color/brand_text</item>
|
||||
<item name="colorSecondaryVariant">@color/teal_200</item>
|
||||
<item name="colorOnSecondary">@color/black</item>
|
||||
<!-- Status bar color. -->
|
||||
|
4
app/src/main/res/values/dimens.xml
Normal file
4
app/src/main/res/values/dimens.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<dimen name="app_margin">16dp</dimen>
|
||||
</resources>
|
@ -1,4 +1,6 @@
|
||||
<resources>
|
||||
<string name="app_name">AllYouCanTweet</string>
|
||||
<string name="base_url" translatable="false">https://young-spire-63034.herokuapp.com/</string>
|
||||
<string name="retry">retry</string>
|
||||
<string name="sth_went_wrong">Ups… Something went wrong!</string>
|
||||
</resources>
|
@ -6,7 +6,7 @@
|
||||
<item name="colorPrimaryVariant">@color/brand</item>
|
||||
<item name="colorOnPrimary">@color/white</item>
|
||||
<!-- Secondary brand color. -->
|
||||
<item name="colorSecondary">@color/teal_200</item>
|
||||
<item name="colorSecondary">@color/brand_text</item>
|
||||
<item name="colorSecondaryVariant">@color/teal_700</item>
|
||||
<item name="colorOnSecondary">@color/black</item>
|
||||
<!-- Status bar color. -->
|
||||
|
Loading…
Reference in New Issue
Block a user