Skip to content

Commit

Permalink
Jointly load all cells in home fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
meiron03 committed Oct 14, 2023
1 parent 0a110a5 commit 38c7acc
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 46 deletions.
176 changes: 135 additions & 41 deletions PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class HomeFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
getHomePage()
}



@RequiresApi(Build.VERSION_CODES.M)
private fun getHomePage() {

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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
}
}
})

Expand All @@ -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
}
}
})

Expand All @@ -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
}
}
})

Expand All @@ -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
}
}
})

Expand All @@ -245,38 +300,60 @@ 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
}
}
}

}, { throwable ->
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
}
}
})

Expand All @@ -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
}
}
})
}
Expand Down Expand Up @@ -376,4 +463,11 @@ class HomeFragment : Fragment() {
}
}

enum class Cells {
POLLS, NEWS, DINING, CALENDAR, LAUNDRY, POSTS
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38c7acc

Please sign in to comment.