From 38c7acc7d612085694fcc68d6e814dd440df6ab3 Mon Sep 17 00:00:00 2001 From: meiron03 Date: Sat, 14 Oct 2023 15:50:55 -0400 Subject: [PATCH] Jointly load all cells in home fragment --- .../pennapps/labs/pennmobile/HomeFragment.kt | 176 ++++++++++++++---- .../pennapps/labs/pennmobile/MainActivity.kt | 1 - .../pennmobile/api/OAuth2NetworkManager.kt | 4 - 3 files changed, 135 insertions(+), 46 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 b0af7d39..ffafc6b5 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt @@ -81,6 +81,9 @@ class HomeFragment : Fragment() { super.onViewCreated(view, savedInstanceState) getHomePage() } + + + @RequiresApi(Build.VERSION_CODES.M) private fun getHomePage() { @@ -108,6 +111,9 @@ class HomeFragment : Fragment() { homepageCells.add(HomeCell()) } + // number of cells loaded + var loaded = 0 + val studentLife = MainActivity.studentLifeInstance OAuth2NetworkManager(mActivity).getAccessToken { val sp = sharedPreferences @@ -117,6 +123,9 @@ class HomeFragment : Fragment() { val bearerToken = "Bearer " + sp.getString(getString(R.string.access_token), "").toString() Log.i("HomeFragment", bearerToken) if (bearerToken != "Bearer ") { + val totalCells = 6 + + val idHash = getSha256Hash(deviceID) studentLife.browsePolls(bearerToken, idHash).subscribe({ poll -> if (poll.size == 0) { @@ -126,10 +135,14 @@ class HomeFragment : Fragment() { val pollCell = PollCell(poll[0]) pollCell.poll.options.forEach { pollCell.poll.totalVotes += it.voteCount } homepageCells[0] = pollCell - home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) - loadingPanel?.visibility = View.GONE - internetConnectionHome?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }, { throwable -> Log.e("Poll", "Error retrieving polls", throwable) @@ -142,16 +155,28 @@ class HomeFragment : Fragment() { newsCell.info?.article = article newsCell.type = "news" homepageCells[3] = newsCell - home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + + loaded++ + + if (loaded == totalCells) { + home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }, { throwable -> mActivity.runOnUiThread { Log.e("Home", "Could not load news", throwable) throwable.printStackTrace() - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + + loaded++ + + if (loaded == totalCells) { + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }) @@ -175,17 +200,28 @@ class HomeFragment : Fragment() { diningCellInfo.venues = venues diningCell.info = diningCellInfo homepageCells[4] = diningCell - home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) - loadingPanel?.visibility = View.GONE - internetConnectionHome?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + + loaded++ + + if (loaded == totalCells) { + home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }, { throwable -> mActivity.runOnUiThread { Log.e("Home", "Could not load Dining", throwable) throwable.printStackTrace() - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + + loaded++ + + if (loaded == totalCells) { + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }) @@ -199,16 +235,26 @@ class HomeFragment : Fragment() { gsrBookingCell.type = "gsr_booking" gsrBookingCell.buildings = arrayListOf("Huntsman Hall", "Weigle") homepageCells[5] = gsrBookingCell - home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }, { throwable -> mActivity.runOnUiThread { Log.e("Home", "Could not load calendar", throwable) throwable.printStackTrace() - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }) @@ -223,17 +269,26 @@ class HomeFragment : Fragment() { } laundryCell.info = laundryCellInfo homepageCells[6] = laundryCell - home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) - loadingPanel?.visibility = View.GONE - internetConnectionHome?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }, { throwable -> mActivity.runOnUiThread { Log.e("Home", "Could not load laundry", throwable) throwable.printStackTrace() - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }) @@ -245,9 +300,14 @@ class HomeFragment : Fragment() { postCell.type = "post" postCell.info?.post = post[0] homepageCells[2] = postCell - home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } } @@ -255,28 +315,45 @@ class HomeFragment : Fragment() { mActivity.runOnUiThread { Log.e("Home", "Could not load posts", throwable) throwable.printStackTrace() - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }) } else { + val totalCells = 2 + studentLife.calendar.subscribe({ events -> mActivity.runOnUiThread { val calendar = HomeCell() calendar.type = "calendar" calendar.events = events homepageCells.add(0, calendar) - home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }, { throwable -> mActivity.runOnUiThread { Log.e("Home", "Could not load Home page", throwable) throwable.printStackTrace() - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }) @@ -291,16 +368,26 @@ class HomeFragment : Fragment() { gsrBookingCell.type = "gsr_booking" gsrBookingCell.buildings = arrayListOf("Huntsman Hall", "Weigle") homepageCells.add(homepageCells.size, gsrBookingCell) - home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }, { throwable -> mActivity.runOnUiThread { Log.e("Home", "Could not load Home page", throwable) throwable.printStackTrace() - loadingPanel?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + loaded++ + + if (loaded == totalCells) { + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }) } @@ -376,4 +463,11 @@ class HomeFragment : Fragment() { } } + enum class Cells { + POLLS, NEWS, DINING, CALENDAR, LAUNDRY, POSTS + } + + + + } \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt index 7736004d..3fadcef2 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt @@ -53,7 +53,6 @@ import retrofit.RestAdapter import retrofit.android.AndroidLog import retrofit.client.OkClient import retrofit.converter.GsonConverter -import java.util.Calendar import java.util.concurrent.TimeUnit class MainActivity : AppCompatActivity() { diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/OAuth2NetworkManager.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/OAuth2NetworkManager.kt index 55cca64e..72aa78ae 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/OAuth2NetworkManager.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/OAuth2NetworkManager.kt @@ -8,11 +8,7 @@ import com.pennapps.labs.pennmobile.BuildConfig import com.pennapps.labs.pennmobile.MainActivity import com.pennapps.labs.pennmobile.R import com.pennapps.labs.pennmobile.classes.AccessTokenResponse -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.sync.withLock import retrofit.Callback import retrofit.RetrofitError import retrofit.client.Response