Skip to content

Commit

Permalink
Move response codes to their own object
Browse files Browse the repository at this point in the history
Now they can be referenced from different places
  • Loading branch information
Mindavi committed Jul 23, 2018
1 parent da81f9c commit 29ca681
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package eu.rickvanschijndel.solargraph

object ResponseCode {
const val OK = 200
const val UNAUTHORIZED = 401
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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?) {
Expand Down Expand Up @@ -98,11 +96,11 @@ class GraphActivity : AppCompatActivity() {
?.subscribe(object: SingleObserver<Response<ProductionResponse>> {
override fun onSuccess(response: Response<ProductionResponse>) {
when(response.code()) {
RESPONSE_OK -> {
ResponseCode.OK -> {
val body = response.body()!!
onDataRetrieved(body)
}
RESPONSE_UNAUTHORIZED -> {
ResponseCode.UNAUTHORIZED -> {
removeUsernameAndPassword()
restartApplication()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,14 +70,14 @@ class LoginActivity : AppCompatActivity() {
.subscribe(object: SingleObserver<Response<Collection<SiteResponse>>> {
override fun onSuccess(response: Response<Collection<SiteResponse>>) {
when(response.code()) {
200 -> {
ResponseCode.OK -> {
saveUsernameAndPassword()
val graphActivity = Intent(applicationContext, GraphActivity::class.java)
startActivity(graphActivity)
finish()
}

401 -> {
ResponseCode.UNAUTHORIZED -> {
notifyLoginFailure(response.message())
return
}
Expand Down

0 comments on commit 29ca681

Please sign in to comment.