Skip to content

Commit

Permalink
Merge pull request #500 from HackIllinois/leah/version-flow-fix
Browse files Browse the repository at this point in the history
Fix Version Checking & Loading Animation Flow
  • Loading branch information
leahlud authored Oct 22, 2023
2 parents b6be731 + 52b8f10 commit a427f8b
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 82 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
android:screenOrientation="portrait" />

<activity
android:name=".view.onboarding.OnboardingActivity"
android:name=".view.OnboardingActivity"
android:screenOrientation="portrait" />

<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ data class UserEventPair(val userToken: String, val eventId: String)
data class MeetingCheckInResponse(var status: String)

data class AttendeeCheckInResponse(
val newPoints: Int,
val totalPoints: Int,
val status: String
var newPoints: Int,
var totalPoints: Int,
var status: String
)

data class StaffCheckInResponse(
val newPoints: Int,
val totalPoints: Int,
val status: String,
val rsvpData: RSVPData
var newPoints: Int,
var totalPoints: Int,
var status: String,
var rsvpData: RSVPData
)

data class RSVPData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.hackillinois.android.App
import org.hackillinois.android.database.entity.*
import org.json.JSONObject
import retrofit2.HttpException

class EventRepository {
private val eventDao = App.database.eventDao()
Expand Down Expand Up @@ -74,10 +76,16 @@ class EventRepository {
withContext(Dispatchers.IO) {
try {
val body = MeetingEventId(eventId)
apiResponse = App.getAPI().staffMeetingCheckIn(body) // 200: status = "Success"
App.getAPI().staffMeetingCheckIn(body)
apiResponse.status = "Success! Your meeting attendance has been recorded!"
} catch (e: Exception) {
apiResponse.status = e.message.toString()
Log.d("STAFF MEETING CHECK IN", "${e.message}")
var error = "Unknown error"
if (e is HttpException) {
val jsonObject = JSONObject("" + e.response()?.errorBody()?.string())
error = jsonObject.optString("error", "Unknown error")
}
apiResponse.status = "Scan failed: $error"
Log.d("STAFF MEETING CHECK IN ERROR", apiResponse.status)
}
}
return apiResponse
Expand All @@ -92,6 +100,7 @@ class EventRepository {
apiResponse = App.getAPI().staffEventCheckIn(userTokenEventIdPair)
} catch (e: Exception) {
Log.e("STAFF EVENT CHECK IN", e.toString())
apiResponse.status = "Something isn't quite right."
}
}
return apiResponse
Expand Down
36 changes: 12 additions & 24 deletions app/src/main/java/org/hackillinois/android/view/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,21 @@ class LoginActivity : AppCompatActivity() {

private fun showFailedToLogin(message: String?) {
if (message != null) {
Snackbar.make(
findViewById(android.R.id.content),
message.toString(),
Snackbar.LENGTH_SHORT,
).show()
Snackbar.make(findViewById(android.R.id.content), message.toString(), Snackbar.LENGTH_SHORT,).show()
} else {
Snackbar.make(
findViewById(android.R.id.content),
"Failed to login. Please try again.",
Snackbar.LENGTH_SHORT,
).show()
val failMessage = applicationContext.getString(R.string.login_fail_message)
Snackbar.make(findViewById(android.R.id.content), failMessage, Snackbar.LENGTH_SHORT,).show()
}
}

private fun showFailedToLoginStaff() {
Snackbar.make(
findViewById(android.R.id.content),
"You must have a valid staff account to log in.",
Snackbar.LENGTH_SHORT,
).show()
val staffMessage = applicationContext.getString(R.string.login_fail_staff_message)
Snackbar.make(findViewById(android.R.id.content), staffMessage, Snackbar.LENGTH_SHORT).show()
}

private fun showFailedToLoginAttendee() {
Snackbar.make(
findViewById(android.R.id.content),
"You must RSVP to log in.",
Snackbar.LENGTH_SHORT,
).show()
val attendeeMessage = applicationContext.getString(R.string.login_fail_attendee_message)
Snackbar.make(findViewById(android.R.id.content), attendeeMessage, Snackbar.LENGTH_SHORT).show()
}

private fun launchMainActivity() {
Expand All @@ -154,13 +141,14 @@ class LoginActivity : AppCompatActivity() {
}

private fun setOAuthProvider(provider: String) {
val editor = applicationContext.getSharedPreferences(applicationContext.getString(R.string.authorization_pref_file_key), Context.MODE_PRIVATE).edit()
editor.putString("provider", provider)
editor.apply()
val prefString = applicationContext.getString(R.string.authorization_pref_file_key)
val editor = applicationContext.getSharedPreferences(prefString, Context.MODE_PRIVATE).edit()
editor.putString("provider", provider).apply()
}

private fun getOAuthProvider(): String {
return applicationContext.getSharedPreferences(applicationContext.getString(R.string.authorization_pref_file_key), Context.MODE_PRIVATE).getString("provider", "")
val prefString = applicationContext.getString(R.string.authorization_pref_file_key)
return applicationContext.getSharedPreferences(prefString, Context.MODE_PRIVATE).getString("provider", "")
?: ""
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/org/hackillinois/android/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.hackillinois.android.common.JWTUtilities
import org.hackillinois.android.notifications.FirebaseTokenManager
import org.hackillinois.android.view.home.HomeFragment
import org.hackillinois.android.view.leaderboard.LeaderboardFragment
import org.hackillinois.android.view.onboarding.OnboardingActivity
import org.hackillinois.android.view.profile.ProfileFragment
import org.hackillinois.android.view.scanner.ScannerFragment
import org.hackillinois.android.view.scanner.StaffScannerFragment
Expand Down Expand Up @@ -172,12 +171,13 @@ class MainActivity : AppCompatActivity() {

thread {
FavoritesManager.clearFavorites(this)
val prefString = applicationContext.getString(R.string.authorization_pref_file_key)
applicationContext.getSharedPreferences(prefString, Context.MODE_PRIVATE).edit().remove("provider").apply()
App.database.clearAllTables()
App.getAPI("")

runOnUiThread {
// val loginIntent = Intent(this, LoginActivity::class.java)
val loginIntent = Intent(this, OnboardingActivity::class.java)
val loginIntent = Intent(this, LoginActivity::class.java)
loginIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(loginIntent)
finish()
Expand All @@ -190,7 +190,7 @@ class MainActivity : AppCompatActivity() {
}

private fun isStaff(): Boolean {
return applicationContext.getSharedPreferences(applicationContext.getString(R.string.authorization_pref_file_key), Context.MODE_PRIVATE).getString("provider", "")
?: "" == "google"
val prefString = applicationContext.getString(R.string.authorization_pref_file_key)
return applicationContext.getSharedPreferences(prefString, Context.MODE_PRIVATE).getString("provider", "") ?: "" == "google"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.hackillinois.android.view.onboarding
package org.hackillinois.android.view

import android.content.Intent
import android.os.Bundle
Expand All @@ -10,7 +10,7 @@ import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.android.synthetic.main.activity_onboarding.*
import org.hackillinois.android.R
import org.hackillinois.android.view.LoginActivity
import org.hackillinois.android.view.onboarding.OnboardingPageFragment

class OnboardingActivity : FragmentActivity() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.hackillinois.android.view

import android.animation.Animator
import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import kotlinx.android.synthetic.main.activity_splash_screen.*
import kotlinx.coroutines.async
Expand All @@ -15,14 +18,14 @@ import org.hackillinois.android.App
import org.hackillinois.android.BuildConfig
import org.hackillinois.android.R
import org.hackillinois.android.common.JWTUtilities
import org.hackillinois.android.view.onboarding.OnboardingActivity
import java.util.concurrent.CountDownLatch
import kotlin.concurrent.thread

class SplashScreenActivity : AppCompatActivity() {

private val countDownLatch = CountDownLatch(2)
private val countDownLatch = CountDownLatch(3)
private var needsToLogin = true
private var needsToUpdate = false

@Volatile private var hasClickedOrAnimFinish = false

Expand All @@ -37,20 +40,22 @@ class SplashScreenActivity : AppCompatActivity() {

// launch Coroutine to execute asynchronous calls
var androidVersion = BuildConfig.VERSION_NAME
playAnimation()
lifecycleScope.launch {
try {
val api = App.getAPI()
val apiResponse = async { api.versionCode() }.await()
val apiVersion = apiResponse.version
// check if user needs to update their app
if (androidVersion < apiVersion) {
needsToUpdate = true
showUpdatePopUp()
} else {
// app is up-to-date, so start animation and check if they need to log in
playAnimation()
countDownLatch.countDown()
val jwt = JWTUtilities.readJWT(applicationContext)
if (jwt != JWTUtilities.DEFAULT_JWT) {
Log.d("JWT", jwt)
Log.d("JWT SplashScreen", jwt)
val api = App.getAPI(jwt)
async { api.user() }.await()
needsToLogin = false
Expand All @@ -67,13 +72,15 @@ class SplashScreenActivity : AppCompatActivity() {
}

thread {
/* countDownLatch needs 2 things to finish:
1. Async api call is completed or fails
2. Loading animation finishes or user taps it
/* countDownLatch needs 3 things to finish:
1. Version is >= API's stored version (GET /version/android/)
2. Async api call (GET /user/) is completed or fails
3. Loading animation finishes or user taps it
*/
countDownLatch.await()
// once countDownLatch is fulfilled, run logic for log in on the UI Thread
runOnUiThread {
splashAnimationView.pauseAnimation()
if (needsToLogin) {
launchOnboardingActivity()
} else {
Expand All @@ -83,6 +90,13 @@ class SplashScreenActivity : AppCompatActivity() {
}
}

override fun onResume() {
super.onResume()
if (needsToUpdate) {
showUpdatePopUp()
}
}

private fun playAnimation() {
// make animation view visible and start playing it
splashAnimationView.visibility = View.VISIBLE
Expand Down Expand Up @@ -112,12 +126,20 @@ class SplashScreenActivity : AppCompatActivity() {
}

private fun showUpdatePopUp() {
splashAnimationView.pauseAnimation()
splashAnimationView.visibility = View.INVISIBLE
val builder = AlertDialog.Builder(this)
.setTitle(R.string.update_app_title)
.setMessage(R.string.update_app_message)
.setCancelable(false)
.setNegativeButton("Go to Play Store") { dialog, id ->
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=org.hackillinois.android.release&pcampaignid=web_share"))
startActivity(intent)
}
val alertDialog = builder.create()
val buttonColor = ContextCompat.getColor(this, R.color.seaSaltGreen)
alertDialog.show()
alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(buttonColor)
}

private fun launchMainActivity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ class ProfileFragment : Fragment() {
viewModel.attendee.observe(this) { updateDietaryRestrictions(it) }
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
if (!hasLoggedIn() or (hasLoggedIn() and isStaff())) {
val view = inflater.inflate(R.layout.fragment_profile_not_logged_in, container, false)
val logoutButton = view.findViewById<Button>(R.id.logout_button)
Expand Down Expand Up @@ -126,16 +122,14 @@ class ProfileFragment : Fragment() {
return view
}

// should refresh QR code
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// if user isn't a staff and has logged in (i.e. attendee), show profile
if (!isStaff() && hasLoggedIn()) {
viewModel = ViewModelProvider(this).get(ProfileViewModel::class.java)
viewModel.qr.observe(
viewLifecycleOwner,
Observer { it ->
// Update UI with new value
Observer {
updateQrView(it)
},
)
Expand Down Expand Up @@ -221,9 +215,7 @@ class ProfileFragment : Fragment() {

private fun isStaff(): Boolean {
val context = requireActivity().applicationContext
return context.getSharedPreferences(
context.getString(R.string.authorization_pref_file_key),
Context.MODE_PRIVATE,
).getString("provider", "") ?: "" == "google"
val prefString = context.getString(R.string.authorization_pref_file_key)
return context.getSharedPreferences(prefString, Context.MODE_PRIVATE).getString("provider", "") ?: "" == "google"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,25 +218,8 @@ class ScannerFragment : Fragment() {
}

private fun displayStaffScanResult(lastScanStatus: ScanStatus?) = lastScanStatus?.let {
val responseString = when (it.userMessage) {
"Success" -> {
if (isMeetingAttendance) {
"Success! Your attendance has been recorded!"
} else {
"Success! The attendant has the following dietary restrictions: ${it.dietary}"
}
}
"InvalidEventId" -> "The event code doesn't seem to be correct. Try selecting the event again or select another event"
"BadUserToken" -> "The QR code may have expired or might be invalid. Please refresh the QR code and try again"
"AlreadyCheckedIn" -> "Looks like the attendant is already checked in."
else -> {
if (isMeetingAttendance) {
"Scan Failed. ${it.userMessage}"
} else {
"Something isn't quite right."
}
}
}
val responseString = it.userMessage

// make dialog from response
if (activity != null) {
if (alertDialog == null) {
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
<string name="favorites_pref_file_key">org.hackillinois.android.favorites</string>
<string name="authorization_pref_file_key">org.hackillinois.android.authorization</string>

<!-- Dialogs -->
<string name="update_app_title">Update your app</string>
<!-- Dialogs / Snackbars -->
<string name="login_fail_message">Failed to login. Please try again.</string>
<string name="login_fail_staff_message">You must have a valid staff account to log in.</string>
<string name="login_fail_attendee_message">You must RSVP to log in.</string>
<string name="update_app_title">Update Required</string>
<string name="update_app_message">To enjoy the latest features, please download the newest version of the HackIllinois app.</string>

<!-- Onboarding Fragment -->
Expand Down

0 comments on commit a427f8b

Please sign in to comment.