From 9917e5788a42fe0b4704e7d516f83d318483be89 Mon Sep 17 00:00:00 2001 From: meiron03 Date: Thu, 4 Jan 2024 15:45:27 -0500 Subject: [PATCH] Synchronous BlurView loading. Mildly scuffed sometimes? --- .../pennapps/labs/pennmobile/HomeFragment.kt | 9 +++- .../labs/pennmobile/adapters/HomeAdapter2.kt | 3 +- .../pennmobile/classes/HomepageDataModel.kt | 2 + .../pennmobile/classes/HomepageViewModel.kt | 49 ++++++++++++++++++- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt index 51fa0a70..3361be08 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt @@ -7,6 +7,7 @@ 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 @@ -88,6 +89,12 @@ class HomeFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) getHomePage() + homepageViewModel.blurViewsLoaded.observe(viewLifecycleOwner) { loaded -> + if (loaded) { + binding.homeCellsRv.visibility = View.VISIBLE + loadingPanel?.visibility = View.GONE + } + } } private fun getOnline() : Boolean { @@ -150,7 +157,7 @@ class HomeFragment : Fragment() { homepageViewModel.populateHomePageCells(studentLife, bearerToken, deviceID) { mActivity.runOnUiThread { binding.homeCellsRv.adapter = HomeAdapter2(homepageViewModel) - loadingPanel?.visibility = View.GONE + binding.homeCellsRv.visibility = View.INVISIBLE binding.internetConnectionHome.visibility = View.GONE binding.homeRefreshLayout.isRefreshing = false } diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/HomeAdapter2.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/HomeAdapter2.kt index 111ad21d..c8317c4e 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/HomeAdapter2.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/HomeAdapter2.kt @@ -245,6 +245,7 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) : holder.itemView.blurView .setOverlayColor(ColorUtils.setAlphaComponent(accentColor, 150)) + dataModel.notifyNewsBlurLoaded() } @@ -368,7 +369,6 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) : holder.itemView.home_card_rv.adapter = HomeGsrBuildingAdapter(ArrayList(buildings)) } - @OptIn(DelicateCoroutinesApi::class) private fun bindPostCell(holder: ViewHolder, cell: HomeCell) { val info = cell.info val post = cell.info?.post @@ -407,6 +407,7 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) : holder.itemView.post_card_container.background = bitmapDrawable holder.itemView.postBlurView .setOverlayColor(ColorUtils.setAlphaComponent(accentColor, 150)) + dataModel.notifyPostBlurLoaded() } } /** Sets up blur view on post card */ diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageDataModel.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageDataModel.kt index 3361390b..4cf81871 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageDataModel.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageDataModel.kt @@ -3,4 +3,6 @@ package com.pennapps.labs.pennmobile.classes interface HomepageDataModel { fun getSize() : Int fun getCell(position: Int) : HomeCell + fun notifyPostBlurLoaded() + fun notifyNewsBlurLoaded() } \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt index 78939001..857b604e 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt @@ -1,5 +1,8 @@ package com.pennapps.labs.pennmobile.classes +import android.util.Log +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.pennapps.labs.pennmobile.api.StudentLife import com.pennapps.labs.pennmobile.utils.Utils.getSha256Hash @@ -23,13 +26,20 @@ class HomepageViewModel : HomepageDataModel, ViewModel() { private val homepageCells = mutableListOf() private val cellMutex = Mutex() + private val _blurViewsLoaded = MutableLiveData(false) + val blurViewsLoaded: LiveData + get() = _blurViewsLoaded + + private var postBlurViewLoaded = false + private var newsBlurViewLoaded = false + private val postBlurMutex = Mutex() + private val newsBlurMutex = Mutex() init { for (i in 1..NUM_CELLS) { homepageCells.add(HomeCell()) } } - @Synchronized fun updateHomePageCells(studentLife: StudentLife, bearerToken: String, deviceID: String, update: (Int) -> Unit, callback: () -> Unit) { @@ -147,6 +157,8 @@ class HomepageViewModel : HomepageDataModel, ViewModel() { postCell.info?.post = post[0] addCell(postCell, POST_POS) + } else { + setPostBlurView(true) } latch.countDown() @@ -185,6 +197,41 @@ class HomepageViewModel : HomepageDataModel, ViewModel() { }) } + private fun setPostBlurView(status: Boolean) = runBlocking { + postBlurMutex.withLock { + postBlurViewLoaded = status + } + updateBlurViewStatus() + } + private fun setNewsBlurView(status: Boolean) = runBlocking { + newsBlurMutex.withLock { + newsBlurViewLoaded = status + } + updateBlurViewStatus() + } + private fun updateBlurViewStatus() = runBlocking { + postBlurMutex.lock() + newsBlurMutex.lock() + Log.i("HomeBlurViewLoadStatus", "Called updateBlurViewStatus") + Log.i("HomeBlurViewLoadStatus", "News: $newsBlurViewLoaded") + Log.i("HomeBlurViewLoadStatus", "Posts: $postBlurViewLoaded") + if (newsBlurViewLoaded && postBlurViewLoaded) { + _blurViewsLoaded.postValue(true) + } else { + _blurViewsLoaded.postValue(false) + } + postBlurMutex.unlock() + newsBlurMutex.unlock() + } + + override fun notifyPostBlurLoaded() { + setPostBlurView(true) + } + + override fun notifyNewsBlurLoaded() { + setNewsBlurView(true) + } + override fun getSize(): Int { return homepageCells.size }