Skip to content

Commit

Permalink
Took care of timing issues when logging out
Browse files Browse the repository at this point in the history
  • Loading branch information
namboy94 committed Oct 5, 2018
1 parent 19d8daa commit a0475f0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ along with bundesliga-tippspiel-android. If not, see <http://www.gnu.org/licens
package net.namibsun.hktipp.activities

import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
Expand All @@ -32,6 +34,22 @@ import org.jetbrains.anko.doAsync
*/
abstract class BaseActivity : AppCompatActivity() {

/**
* The shared preferences used for storing stuff like API keys
*/
lateinit var sharedPreferences: SharedPreferences

/**
* Initializes the Shared preferences
* @param savedInstanceState: The bundle provided by a previous activity
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.sharedPreferences = this.getSharedPreferences(
"SHARED_PREFS", Context.MODE_PRIVATE
)
}

/**
* Shows an error dialog.
* @param titleResource: The resource of the error message title
Expand Down Expand Up @@ -114,15 +132,17 @@ abstract class AuthorizedActivity : BaseActivity() {
Log.i("Activity", "Logging out")

val apiConnection = ApiConnection.loadStored(this)

val editor = this.sharedPreferences.edit()
editor.remove("api_key")
editor.apply()

if (apiConnection != null) {
this.doAsync {
apiConnection.logout(this@AuthorizedActivity)
this@AuthorizedActivity.runOnUiThread {
this@AuthorizedActivity.startActivity(LoginActivity::class.java, true)
}
}
} else {
this.startActivity(LoginActivity::class.java, true)
}

this.startActivity(LoginActivity::class.java, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class BetActivity : AuthorizedActivity() {
}
popup.show()
}
this.updateData()
// onResume will be called, so we don't need to call updateData here
}

/**
Expand All @@ -106,10 +106,8 @@ class BetActivity : AuthorizedActivity() {
this.findViewById<Button>(R.id.bets_prev_button).setOnClickListener { }

Log.d("BetActivity", "Clearing old views")
if (this.matchDay != -1) { // We don't have to clear if no data was fetched before
this.betViews[this.matchDay] = mutableListOf()
this.renderBetViews()
}
this.betViews[this.matchDay] = mutableListOf()
this.renderBetViews()
}

/**
Expand Down Expand Up @@ -238,7 +236,9 @@ class BetActivity : AuthorizedActivity() {
val list = this.findViewById<LinearLayout>(R.id.bets_list)
val bets = this.betViews[this.matchDay]

title.text = this.resources.getString(R.string.bets_matchday_title, this.matchDay)
if (this.matchDay != -1) {
title.text = this.resources.getString(R.string.bets_matchday_title, this.matchDay)
}

if (bets != null) {
list.removeAllViews()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class LoginActivity : BaseActivity() {
this.findViewById<View>(R.id.login_screen_button).setOnClickListener { this.login() }
this.findViewById<View>(R.id.login_screen_logo).setOnClickListener { this.login() }
this.findViewById<View>(R.id.login_screen_register_button).setOnClickListener {
// TODO Register Activity
val uri = Uri.parse("https://hk-tippspiel.com/register")
val intent = Intent(Intent.ACTION_VIEW, uri)
this.startActivity(intent)
Expand Down
17 changes: 7 additions & 10 deletions app/src/main/kotlin/net/namibsun/hktipp/api/ApiConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ along with bundesliga-tippspiel-android. If not, see <http://www.gnu.org/licens
*/

package net.namibsun.hktipp.api
import android.content.Context
import android.util.Base64
import android.util.Log
import net.namibsun.hktipp.activities.BaseActivity
import okhttp3.Headers
import okhttp3.MediaType
import okhttp3.OkHttpClient
Expand Down Expand Up @@ -57,15 +57,13 @@ class ApiConnection(
* Logs out by deleting the API key
* @param context: If provided, deletes the API key information from the shared preferences
*/
fun logout(context: Context? = null) {
fun logout(context: BaseActivity? = null) {
this.delete("api_key", mapOf("api_key" to this.apiKey))

if (context != null) {
val prefs = context.getSharedPreferences("SHARED_PREFS", Context.MODE_PRIVATE)
val editor = prefs.edit()
val editor = context.sharedPreferences.edit()
editor.remove("server_url")
editor.remove("api_key")
editor.remove("user")
editor.remove("expiration")
editor.apply()
}
Expand All @@ -75,9 +73,8 @@ class ApiConnection(
* Stores the API Connection info in the shared preferences
* @param context: The context from which to load the shared preferences
*/
fun store(context: Context) {
val prefs = context.getSharedPreferences("SHARED_PREFS", Context.MODE_PRIVATE)
val editor = prefs.edit()
fun store(context: BaseActivity) {
val editor = context.sharedPreferences.edit()
editor.putString("server_url", this.serverUrl)
editor.putString("api_key", this.apiKey)
editor.putString("user", this.user.toJson().toString())
Expand All @@ -95,8 +92,8 @@ class ApiConnection(
* @param context: The context in from which to get the shared preferences
* @return The loaded ApiConnection OR null if no valid connection was found
*/
fun loadStored(context: Context): ApiConnection? {
val prefs = context.getSharedPreferences("SHARED_PREFS", Context.MODE_PRIVATE)
fun loadStored(context: BaseActivity): ApiConnection? {
val prefs = context.sharedPreferences
val serverUrl = prefs.getString("server_url", null)
val apiKey = prefs.getString("api_key", null)
val expiration = prefs.getInt("expiration", -1)
Expand Down
30 changes: 20 additions & 10 deletions app/src/main/kotlin/net/namibsun/hktipp/views/MatchBetView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,29 @@ class MatchBetView(context: Context, bet: Bet)
this.findViewById<TextView>(R.id.single_match_bet_away).text = "${bet.awayScore}"

val pointsView = this.findViewById<TextView>(R.id.single_match_bet_points)
pointsView.text = "${bet.points}"
val pointsDisplay = if (bet.match.finished) {
"${bet.points}"
} else {
"-"
}
pointsView.text = pointsDisplay

ContextCompat.getDrawable(context, R.drawable.goal_owngoal)

val background = when (bet.points) {
0 -> R.drawable.bet_points_0
3 -> R.drawable.bet_points_1
7 -> R.drawable.bet_points_2
10 -> R.drawable.bet_points_3
12 -> R.drawable.bet_points_4
15 -> R.drawable.bet_points_5
else -> R.drawable.bet_points_before
val background = if (bet.match.finished) {
when (bet.points) {
0 -> R.drawable.bet_points_0
3 -> R.drawable.bet_points_1
7 -> R.drawable.bet_points_2
10 -> R.drawable.bet_points_3
12 -> R.drawable.bet_points_4
15 -> R.drawable.bet_points_5
else -> R.drawable.bet_points_before
}
} else {
R.drawable.bet_points_before
}

pointsView.background = ContextCompat.getDrawable(context, background)
}
}
}

0 comments on commit a0475f0

Please sign in to comment.