From 29ca68105d0dbfb33e87241410056b97d9d1eaa0 Mon Sep 17 00:00:00 2001 From: Rick van Schijndel Date: Mon, 23 Jul 2018 21:32:37 +0200 Subject: [PATCH] Move response codes to their own object Now they can be referenced from different places --- .../java/eu/rickvanschijndel/solargraph/ResponseCode.kt | 6 ++++++ .../solargraph/activities/GraphActivity.kt | 8 +++----- .../solargraph/activities/LoginActivity.kt | 5 +++-- 3 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/eu/rickvanschijndel/solargraph/ResponseCode.kt diff --git a/app/src/main/java/eu/rickvanschijndel/solargraph/ResponseCode.kt b/app/src/main/java/eu/rickvanschijndel/solargraph/ResponseCode.kt new file mode 100644 index 0000000..c5ba534 --- /dev/null +++ b/app/src/main/java/eu/rickvanschijndel/solargraph/ResponseCode.kt @@ -0,0 +1,6 @@ +package eu.rickvanschijndel.solargraph + +object ResponseCode { + const val OK = 200 + const val UNAUTHORIZED = 401 +} diff --git a/app/src/main/java/eu/rickvanschijndel/solargraph/activities/GraphActivity.kt b/app/src/main/java/eu/rickvanschijndel/solargraph/activities/GraphActivity.kt index 864fcd4..168b500 100644 --- a/app/src/main/java/eu/rickvanschijndel/solargraph/activities/GraphActivity.kt +++ b/app/src/main/java/eu/rickvanschijndel/solargraph/activities/GraphActivity.kt @@ -16,6 +16,7 @@ import com.github.mikephil.charting.data.LineDataSet import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.listener.OnChartValueSelectedListener import eu.rickvanschijndel.solargraph.R +import eu.rickvanschijndel.solargraph.ResponseCode import eu.rickvanschijndel.solargraph.models.ProductionResponse import eu.rickvanschijndel.solargraph.rest.ApiImpl import io.reactivex.Single @@ -27,7 +28,6 @@ import retrofit2.Response import java.text.SimpleDateFormat import java.util.Locale import java.util.Date - import kotlinx.android.synthetic.main.activity_graph.network_info import kotlinx.android.synthetic.main.activity_graph.today_power import kotlinx.android.synthetic.main.activity_graph.monthly_power @@ -40,8 +40,6 @@ class GraphActivity : AppCompatActivity() { companion object { private const val TAG = "GraphActivity" - private const val RESPONSE_OK = 200 - private const val RESPONSE_UNAUTHORIZED = 401 } override fun onCreate(savedInstanceState: Bundle?) { @@ -98,11 +96,11 @@ class GraphActivity : AppCompatActivity() { ?.subscribe(object: SingleObserver> { override fun onSuccess(response: Response) { when(response.code()) { - RESPONSE_OK -> { + ResponseCode.OK -> { val body = response.body()!! onDataRetrieved(body) } - RESPONSE_UNAUTHORIZED -> { + ResponseCode.UNAUTHORIZED -> { removeUsernameAndPassword() restartApplication() } diff --git a/app/src/main/java/eu/rickvanschijndel/solargraph/activities/LoginActivity.kt b/app/src/main/java/eu/rickvanschijndel/solargraph/activities/LoginActivity.kt index 72ad22a..4fa55e2 100644 --- a/app/src/main/java/eu/rickvanschijndel/solargraph/activities/LoginActivity.kt +++ b/app/src/main/java/eu/rickvanschijndel/solargraph/activities/LoginActivity.kt @@ -9,6 +9,7 @@ import android.os.Bundle import android.preference.PreferenceManager import android.support.design.widget.Snackbar import eu.rickvanschijndel.solargraph.R +import eu.rickvanschijndel.solargraph.ResponseCode import eu.rickvanschijndel.solargraph.models.SiteResponse import eu.rickvanschijndel.solargraph.rest.ApiImpl import io.reactivex.SingleObserver @@ -69,14 +70,14 @@ class LoginActivity : AppCompatActivity() { .subscribe(object: SingleObserver>> { override fun onSuccess(response: Response>) { when(response.code()) { - 200 -> { + ResponseCode.OK -> { saveUsernameAndPassword() val graphActivity = Intent(applicationContext, GraphActivity::class.java) startActivity(graphActivity) finish() } - 401 -> { + ResponseCode.UNAUTHORIZED -> { notifyLoginFailure(response.message()) return }