Skip to content

Commit

Permalink
Cleanup + documentation. Also added some explanation as to how waitin…
Browse files Browse the repository at this point in the history
…g for the blur views is done.

Home page loading/reloading should be smoother (and more modular).
  • Loading branch information
meiron03 committed Jan 6, 2024
1 parent 67be5b9 commit c645054
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 848 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -19,7 +18,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.analytics.FirebaseAnalytics
import com.pennapps.labs.pennmobile.adapters.HomeAdapter2
import com.pennapps.labs.pennmobile.adapters.HomeAdapter
import com.pennapps.labs.pennmobile.api.OAuth2NetworkManager
import com.pennapps.labs.pennmobile.classes.HomepageViewModel
import com.pennapps.labs.pennmobile.components.collapsingtoolbar.ToolbarBehavior
Expand Down Expand Up @@ -74,7 +73,7 @@ class HomeFragment : Fragment() {
.setColorSchemeResources(R.color.color_accent, R.color.color_primary)
view.home_refresh_layout
.setOnRefreshListener {
refreshHomePage()
getHomePage()
}

initAppBar(view)
Expand All @@ -88,6 +87,29 @@ class HomeFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

/* Mildly suspicious idea to hide the RecyclerView until blurviews (for news and posts) are
done processing. Processing the blurviews is slow and makes the app looks sloppy. Ideally
this is replaced by something less hacky (such as using a ListView instead) though perhaps
it is simpler to just remove the blur altogether.
This takes advantage of a RecyclerView idiosyncracy: when a RecyclerView resides inside a
nested scrollview, all of the elements are inflated:
https://stackoverflow.com/questions/44453846/recyclerview-inside-nestedscrollview-causes-recyclerview-to-inflate-all-elements
https://www.reddit.com/r/androiddev/comments/d8gi9v/recyclerview_inside_nestedscrollview_causes/
This is can be used to figure out when the blurviews are finished processing.
Since when the adapter is set in getHomePage, onBindViewHolder() is called for each cell.
Thus, for the news and post cells which use blur, when the blur is finished processing,
the adapter notifies homepageViewModel. When both blurs are processed, the blurViewsLoaded
liveData in the ViewModel is toggled to true which HomeFragment observes.
If in the future, the homepage is stuck on loading forever, this might be why. To remove
this functionality and stop waiting for the blur views to finish, just remove the observer
below and change getHomePage() so that when HomeAdapter is set, homeCellsRv.visibility is
set to View.VISIBLE instead of View.INVISIBLE and hide loadingPanel
*/
homepageViewModel.resetBlurViews()
getHomePage()
homepageViewModel.blurViewsLoaded.observe(viewLifecycleOwner) { loaded ->
Expand All @@ -114,7 +136,8 @@ class HomeFragment : Fragment() {
binding.homeCellsRv.setPadding(0, 0, 0, 0)
return true
}
private fun refreshHomePage() {

private fun getHomePage() {
mActivity.showBottomBar()


Expand All @@ -129,16 +152,17 @@ class HomeFragment : Fragment() {
val bearerToken = "Bearer " + sp.getString(getString(R.string.access_token), "").toString()

lifecycleScope.launch(Dispatchers.Default) {
// set adapter if it is null
if (binding.homeCellsRv.adapter == null) {
homepageViewModel.populateHomePageCells(studentLife, bearerToken, deviceID) {
mActivity.runOnUiThread {
binding.homeCellsRv.adapter = HomeAdapter2(homepageViewModel)
binding.homeCellsRv.adapter = HomeAdapter(homepageViewModel)
binding.homeCellsRv.visibility = View.INVISIBLE
binding.internetConnectionHome.visibility = View.GONE
binding.homeRefreshLayout.isRefreshing = false
}
}
} else {
} else { // otherwise, call updateHomePageCells which only updates the cells that are changed
homepageViewModel.updateHomePageCells(studentLife, bearerToken, deviceID, { pos ->
mActivity.runOnUiThread {
binding.homeCellsRv.adapter!!.notifyItemChanged(pos)
Expand All @@ -153,32 +177,6 @@ class HomeFragment : Fragment() {
}
}

private fun getHomePage() {
mActivity.showBottomBar()

if (!getOnline()) {
return
}

val studentLife = MainActivity.studentLifeInstance
mActivity.mNetworkManager.getAccessToken {
val sp = sharedPreferences
val deviceID = OAuth2NetworkManager(mActivity).getDeviceId()
val bearerToken =
"Bearer " + sp.getString(getString(R.string.access_token), "").toString()

lifecycleScope.launch(Dispatchers.Default) {
homepageViewModel.populateHomePageCells(studentLife, bearerToken, deviceID) {
mActivity.runOnUiThread {
binding.homeCellsRv.adapter = HomeAdapter2(homepageViewModel)
binding.homeCellsRv.visibility = View.INVISIBLE
binding.internetConnectionHome.visibility = View.GONE
binding.homeRefreshLayout.isRefreshing = false
}
}
}
}
}

private val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,8 @@ class MainActivity : AppCompatActivity() {
// gets laundry preferences (used only for testing)
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<Int?>?>() {}.type, LaundryPrefSerializer())
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<FlingEvent?>?>() {}.type, FlingEventSerializer())
// gets fitness
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<Gym?>?>() {}.type, GymSerializer())
// gets gsr reservations
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<GSRReservation?>?>() {}.type, GsrReservationSerializer())
// gets homepage
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<HomeCell?>?>() {}.type, HomePageSerializer())
// gets user
gsonBuilder.registerTypeAdapter(Account::class.java, UserSerializer())
// gets posts
Expand Down
Loading

0 comments on commit c645054

Please sign in to comment.