Skip to content

Commit

Permalink
Get rid of unnecessary callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
meiron03 committed Feb 8, 2024
1 parent ce424c8 commit b892c7f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import kotlinx.android.synthetic.main.include_main.*
import kotlinx.android.synthetic.main.loading_panel.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.*

class HomeFragment : Fragment() {
Expand Down Expand Up @@ -165,24 +166,21 @@ class HomeFragment : Fragment() {
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 = HomeAdapter(homepageViewModel)
binding.homeCellsRv.visibility = View.INVISIBLE
binding.internetConnectionHome.visibility = View.GONE
binding.homeRefreshLayout.isRefreshing = false
}
homepageViewModel.populateHomePageCells(studentLife, 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
homepageViewModel.updateHomePageCells(studentLife, bearerToken, deviceID, { pos ->
mActivity.runOnUiThread {
binding.homeCellsRv.adapter!!.notifyItemChanged(pos)
}
}, {
mActivity.runOnUiThread {
binding.homeRefreshLayout.isRefreshing = false
}
})
val updatedIndices = homepageViewModel.updateHomePageCells(studentLife, bearerToken, deviceID)
withContext(Dispatchers.Main) {
updatedIndices.forEach { pos ->
binding.homeCellsRv.adapter!!.notifyItemChanged(pos)
}
binding.homeRefreshLayout.isRefreshing = false
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,34 +96,34 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
}

/**
* Updates each homepage cell. If a cell changes, then update is called with the index of the
* cell that was changed (created with the intention to play nice with RecyclerView and is used
* in conjunction with NotifyItemChanged().
* Returns a list of updated cell positions.
*/
@Synchronized
fun updateHomePageCells(studentLife: StudentLife, bearerToken: String, deviceID: String,
update: (Int) -> Unit, callback: () -> Unit) {
fun updateHomePageCells(studentLife: StudentLife, bearerToken: String,
deviceID: String) : List<Int> {
val prevList = homepageCells.toList()
populateHomePageCells(studentLife, bearerToken, deviceID) {
for (i in 0 until NUM_CELLS) {
if (prevList[i] != homepageCells[i]) {
update(i)
Log.i("CellUpdates", "updated index ${i}")
} else {
Log.i("CellUpdates", "saved an update at index ${i}")
}
populateHomePageCells(studentLife, bearerToken, deviceID)

val updatedIndices = mutableListOf<Int>()

for (i in 0 until NUM_CELLS) {
if (prevList[i] != homepageCells[i]) {
updatedIndices.add(i);
Log.i("CellUpdates", "updated index ${i}")
} else {
Log.i("CellUpdates", "saved an update at index ${i}")
}
callback.invoke()
}

return updatedIndices
}

/**
* Makes the network requests that populates the Homepage Cells.
* This function requires a correct (non-expired) bearerToken!!
*/
@Synchronized
fun populateHomePageCells(studentLife: StudentLife, bearerToken: String, deviceID: String,
callback: () -> Unit) {
fun populateHomePageCells(studentLife: StudentLife, bearerToken: String, deviceID: String) {
val isLoggedIn = bearerToken != "Bearer "

if (isLoggedIn) {
Expand All @@ -143,7 +143,6 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
getNews(studentLife, latch)
latch.await()
}
callback.invoke()
}

/**
Expand Down

0 comments on commit b892c7f

Please sign in to comment.