Skip to content

Commit

Permalink
Synchronous BlurView loading. Mildly scuffed sometimes?
Browse files Browse the repository at this point in the history
  • Loading branch information
meiron03 committed Jan 4, 2024
1 parent e370a22 commit 9917e57
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) :
holder.itemView.blurView
.setOverlayColor(ColorUtils.setAlphaComponent(accentColor, 150))

dataModel.notifyNewsBlurLoaded()
}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package com.pennapps.labs.pennmobile.classes
interface HomepageDataModel {
fun getSize() : Int
fun getCell(position: Int) : HomeCell
fun notifyPostBlurLoaded()
fun notifyNewsBlurLoaded()
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,13 +26,20 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {

private val homepageCells = mutableListOf<HomeCell>()
private val cellMutex = Mutex()
private val _blurViewsLoaded = MutableLiveData<Boolean>(false)
val blurViewsLoaded: LiveData<Boolean>
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) {
Expand Down Expand Up @@ -147,6 +157,8 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
postCell.info?.post = post[0]

addCell(postCell, POST_POS)
} else {
setPostBlurView(true)
}

latch.countDown()
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 9917e57

Please sign in to comment.