Skip to content

Commit

Permalink
Fix Guest Mode (#606)
Browse files Browse the repository at this point in the history
* Fix Guest Mode (it does some suspicious function calls that we should fix in the future but sounds like a future problem)

* Remove unused imports and cleanup code
  • Loading branch information
meiron03 committed Apr 6, 2024
1 parent cc7b8c3 commit 22d7ba9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,21 @@ class HomeFragment : Fragment() {
val deviceID = OAuth2NetworkManager(mActivity).getDeviceId()
val bearerToken = "Bearer " + sp.getString(getString(R.string.access_token), "").toString()

val isLoggedIn = !sp.getBoolean(mActivity.getString(R.string.guest_mode), false)

lifecycleScope.launch(Dispatchers.Default) {
// set adapter if it is null
if (binding.homeCellsRv.adapter == null) {
homepageViewModel.populateHomePageCells(studentLife, bearerToken, deviceID)
homepageViewModel.populateHomePageCells(studentLife, isLoggedIn, bearerToken, deviceID)
withContext(Dispatchers.Main) {
binding.homeCellsRv.adapter = HomeAdapter(homepageViewModel)
binding.homeCellsRv.visibility = View.INVISIBLE
binding.internetConnectionHome.visibility = View.GONE
binding.homeRefreshLayout.isRefreshing = false
}
} else { // otherwise, call updateHomePageCells which only updates the cells that are changed
val updatedIndices = homepageViewModel.updateHomePageCells(studentLife, bearerToken, deviceID)
val updatedIndices = homepageViewModel.updateHomePageCells(studentLife, isLoggedIn,
bearerToken, deviceID)
withContext(Dispatchers.Main) {
updatedIndices.forEach { pos ->
binding.homeCellsRv.adapter!!.notifyItemChanged(pos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class LoginFragment : Fragment() {
editor.remove(getString(R.string.pennkey))
editor.remove(getString(R.string.access_token))
editor.remove(getString(R.string.accountID))
editor.putString(getString(R.string.access_token), "")
editor.putString(getString(R.string.refresh_token), "")
editor.putString(getString(R.string.expires_in), "")
editor.putBoolean(getString(R.string.guest_mode), true)
editor.apply()
mActivity.startHomeFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.provider.Settings
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.google.firebase.crashlytics.FirebaseCrashlytics

import com.pennapps.labs.pennmobile.BuildConfig
import com.pennapps.labs.pennmobile.MainActivity
import com.pennapps.labs.pennmobile.R
Expand All @@ -29,6 +28,14 @@ class OAuth2NetworkManager(private var mActivity: MainActivity) {

@Synchronized
fun getAccessToken(function: () -> Unit) {
// if guest mode, then just do network request dangerously(TEMPORARY FIX, PLEASE DO SOMETHING
// ABOUT THIS IN FUTURE)
val guestMode = sp.getBoolean(mActivity.getString(R.string.guest_mode), false)
if (guestMode) {
function.invoke()
return
}

mActivity.lifecycleScope.launch {
val tokenMutex = mActivity.tokenMutex
tokenMutex.lock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
* Returns a list of updated cell positions.
*/
@Synchronized
fun updateHomePageCells(studentLife: StudentLife, bearerToken: String,
fun updateHomePageCells(studentLife: StudentLife, isLoggedIn: Boolean, bearerToken: String,
deviceID: String) : List<Int> {
val prevList = homepageCells.toList()
populateHomePageCells(studentLife, bearerToken, deviceID)
populateHomePageCells(studentLife, isLoggedIn, bearerToken, deviceID)

val updatedIndices = mutableListOf<Int>()

Expand All @@ -128,9 +128,8 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
* This function requires a correct (non-expired) bearerToken!!
*/
@Synchronized
fun populateHomePageCells(studentLife: StudentLife, bearerToken: String, deviceID: String) {
val isLoggedIn = bearerToken != "Bearer "

fun populateHomePageCells(studentLife: StudentLife,
isLoggedIn: Boolean, bearerToken: String, deviceID: String) {
if (isLoggedIn) {
val latch = CountDownLatch(NUM_CELLS_LOGGED_IN)
getPolls(studentLife, bearerToken, deviceID, latch)
Expand Down

0 comments on commit 22d7ba9

Please sign in to comment.