diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrFragment.kt index 38fe7a43..a447a054 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrFragment.kt @@ -2,6 +2,7 @@ package com.pennapps.labs.pennmobile import android.app.DatePickerDialog import android.app.TimePickerDialog +import android.content.Context import android.content.SharedPreferences import android.os.Bundle import android.util.Log @@ -242,27 +243,29 @@ class GsrFragment : Fragment() { private fun updateStatus() { mActivity.mNetworkManager.getAccessToken { - val bearerToken = - sharedPreferences.getString(getString(R.string.access_token), "").toString() + checkIfFragmentAttached { + val bearerToken = + sharedPreferences.getString(getString(R.string.access_token), "").toString() - if (bearerToken.isEmpty()) { - Toast.makeText(activity, "You are not logged in!", Toast.LENGTH_LONG).show() - } else { - try { - mStudentLife - .isWharton( - "Bearer $bearerToken", - )?.subscribe( - { status -> - isWharton = status.isWharton - }, - { - Log.e("GsrFragment", "Error getting Wharton status", it) - isWharton = false - }, - ) - } catch (e: Exception) { - e.printStackTrace() + if (bearerToken.isEmpty()) { + Toast.makeText(activity, "You are not logged in!", Toast.LENGTH_LONG).show() + } else { + try { + mStudentLife + .isWharton( + "Bearer $bearerToken", + )?.subscribe( + { status -> + isWharton = status.isWharton + }, + { + Log.e("GsrFragment", "Error getting Wharton status", it) + isWharton = false + }, + ) + } catch (e: Exception) { + e.printStackTrace() + } } } } @@ -681,4 +684,10 @@ class GsrFragment : Fragment() { mGSRS.add(newGSRObject) } } + + fun checkIfFragmentAttached(operation: Context.() -> Unit) { + if (isAdded && context != null) { + operation(requireContext()) + } + } } diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MenuFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MenuFragment.kt index 5fc59b81..4d3bc3c4 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MenuFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MenuFragment.kt @@ -88,9 +88,7 @@ class MenuFragment : Fragment() { super.onCreate(savedInstanceState) mDiningHall = arguments?.getParcelable("DiningHall") mActivity = activity as MainActivity - mActivity.toolbar.visibility = View.VISIBLE setHasOptionsMenu(true) - mActivity.hideBottomBar() } override fun onCreateView( @@ -105,9 +103,18 @@ class MenuFragment : Fragment() { pager.adapter = pageAdapter v.setBackgroundColor(Color.WHITE) mActivity.addTabs(pageAdapter as TabAdapter, pager, true) + mActivity.hideBottomBar() return v } + override fun onViewCreated( + view: View, + savedInstanceState: Bundle?, + ) { + super.onViewCreated(view, savedInstanceState) + mActivity.toolbar.visibility = View.VISIBLE + } + override fun onCreateOptionsMenu( menu: Menu, inflater: MenuInflater,