Skip to content

Commit

Permalink
Perhaps we should actually use Object Oriented Programming... Create …
Browse files Browse the repository at this point in the history
…separate subclasses for all homepage cells
  • Loading branch information
meiron03 committed Jan 5, 2024
1 parent 9917e57 commit 9f4f485
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ class CampusExpressLoginFragment : Fragment() {

return stateString
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class HomeFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
homepageViewModel.resetBlurViews()
getHomePage()
homepageViewModel.blurViewsLoaded.observe(viewLifecycleOwner) { loaded ->
if (loaded) {
Expand Down Expand Up @@ -212,4 +213,4 @@ class HomeFragment : Fragment() {
//TODO: Account Settings
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,4 @@ fun ViewGroup.showSneakerToast(message: String, doOnRetry: (() -> Unit)?, sneake
messageView.text = message

sneaker.sneakCustom(view).setCornerRadius(12, 16).setMessage(message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ import com.pennapps.labs.pennmobile.*
import com.pennapps.labs.pennmobile.DiningFragment.Companion.getMenus
import com.pennapps.labs.pennmobile.api.OAuth2NetworkManager
import com.pennapps.labs.pennmobile.api.StudentLife
import com.pennapps.labs.pennmobile.classes.CalendarCell
import com.pennapps.labs.pennmobile.classes.CalendarEvent
import com.pennapps.labs.pennmobile.classes.DiningCell
import com.pennapps.labs.pennmobile.classes.DiningHall
import com.pennapps.labs.pennmobile.classes.HomeCell
import com.pennapps.labs.pennmobile.classes.HomepageDataModel
import com.pennapps.labs.pennmobile.classes.LaundryCell
import com.pennapps.labs.pennmobile.classes.NewsCell
import com.pennapps.labs.pennmobile.classes.PollCell
import com.pennapps.labs.pennmobile.classes.PostCell
import com.pennapps.labs.pennmobile.components.sneaker.Utils.convertToDp
import com.pennapps.labs.pennmobile.utils.Utils
import eightbitlab.com.blurview.RenderScriptBlur
Expand Down Expand Up @@ -118,15 +123,12 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) :
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val cell = dataModel.getCell(position)
when (cell.type) {
"reservations" -> bindHomeReservationsCell(holder, cell)
"dining" -> bindDiningCell(holder, cell)
"calendar" -> bindCalendarCell(holder, cell)
"news" -> bindNewsCell(holder, cell)
"laundry" -> bindLaundryCell(holder, cell)
"gsr_booking" -> bindGsrBookingCell(holder, cell)
"post" -> bindPostCell(holder, cell)
"feature" -> bindFeatureCell(holder, cell)
"poll" -> bindPollCell(holder, cell, position)
"dining" -> bindDiningCell(holder, cell as DiningCell)
"calendar" -> bindCalendarCell(holder, cell as CalendarCell)
"news" -> bindNewsCell(holder, cell as NewsCell)
"laundry" -> bindLaundryCell(holder, cell as LaundryCell)
"post" -> bindPostCell(holder, cell as PostCell)
"poll" -> bindPollCell(holder, cell as PollCell, position)
else -> Log.i("HomeAdapter", "Unsupported type of data at position $position")
}
}
Expand All @@ -141,10 +143,6 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) :

override fun getItemViewType(position: Int): Int {
val cell = dataModel.getCell(position)
if (cell.info?.isTest == true) {
Log.i("HomeAdapter", "Test Portal post")
return NOT_SUPPORTED
}
return when (cell.type) {
"reservations" -> RESERVATIONS
"dining" -> DINING
Expand All @@ -159,17 +157,7 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) :
}
}

private fun bindHomeReservationsCell(holder: ViewHolder, cell: HomeCell) {
val reservations = cell.reservations ?: ArrayList()
holder.itemView.home_card_title.text = "Upcoming Reservations"
holder.itemView.home_card_subtitle.text = "GSR RESERVATIONS"

holder.itemView.home_card_rv.layoutManager = LinearLayoutManager(mContext,
LinearLayoutManager.VERTICAL, false)
holder.itemView.home_card_rv.adapter = GsrReservationsAdapter(ArrayList(reservations))
}

private fun bindDiningCell(holder: ViewHolder, cell: HomeCell) {
private fun bindDiningCell(holder: ViewHolder, cell: DiningCell) {
holder.itemView.home_card_title.text = "Favorites"
holder.itemView.home_card_subtitle.text = "DINING HALLS"
holder.itemView.dining_prefs_btn.visibility = View.VISIBLE
Expand All @@ -191,7 +179,7 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) :
.subscribe { diningHalls ->
mActivity.runOnUiThread {
val favorites: ArrayList<DiningHall> = arrayListOf()
val favoritesIdList: List<Int>? = cell.info?.venues
val favoritesIdList: List<Int>? = cell.venues
diningHalls.forEach {
if (favoritesIdList?.contains(it.id) == true) {
favorites.add(it)
Expand All @@ -205,8 +193,8 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) :
}
}

private fun bindNewsCell(holder: ViewHolder, cell: HomeCell) {
val article = cell.info?.article
private fun bindNewsCell(holder: ViewHolder, cell: NewsCell) {
val article = cell.article
holder.itemView.home_news_title.text = article?.title
holder.itemView.home_news_subtitle.text = article?.subtitle
holder.itemView.home_news_timestamp.text = article?.timestamp?.trim()
Expand Down Expand Up @@ -308,7 +296,7 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) :
}
}

private fun bindCalendarCell(holder: ViewHolder, cell: HomeCell) {
private fun bindCalendarCell(holder: ViewHolder, cell: CalendarCell) {
val events = cell.events ?: ArrayList()

var i = events.size - 1
Expand All @@ -330,13 +318,8 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) :
holder.itemView.home_card_rv.adapter = UniversityEventAdapter(eventList)
}

private fun bindCoursesCell(holder: ViewHolder, cell: HomeCell) {
holder.itemView.home_card_title.text = "Today's schedule"
holder.itemView.home_card_subtitle.text = "COURSE SCHEDULE"
}

private fun bindLaundryCell(holder: ViewHolder, cell: HomeCell) {
val roomID = cell.info?.roomId ?: 0
private fun bindLaundryCell(holder: ViewHolder, cell: LaundryCell) {
val roomID = cell.roomId
holder.itemView.home_card_subtitle.text = "LAUNDRY"
holder.itemView.home_card_rv.layoutManager = LinearLayoutManager(mContext,
LinearLayoutManager.VERTICAL, false)
Expand All @@ -358,36 +341,24 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) :
}, { throwable -> mActivity.runOnUiThread { throwable.printStackTrace() } })
}

private fun bindGsrBookingCell(holder: ViewHolder, cell: HomeCell) {
val buildings = cell.buildings ?: ArrayList()

holder.itemView.home_card_title.text = "Book a GSR"
holder.itemView.home_card_subtitle.text = "GROUP STUDY ROOMS"

holder.itemView.home_card_rv.layoutManager = LinearLayoutManager(mContext,
LinearLayoutManager.VERTICAL, false)
holder.itemView.home_card_rv.adapter = HomeGsrBuildingAdapter(ArrayList(buildings))
}

private fun bindPostCell(holder: ViewHolder, cell: HomeCell) {
val info = cell.info
val post = cell.info?.post
holder.itemView.home_post_title.text = post?.title
holder.itemView.home_post_subtitle.text = post?.subtitle
private fun bindPostCell(holder: ViewHolder, cell: PostCell) {
val post = cell.post
holder.itemView.home_post_title.text = post.title
holder.itemView.home_post_subtitle.text = post.subtitle
holder.itemView.home_post_source.text = "Penn Labs" //post?.clubCode?.capitalize()
val time = post?.startDate?.substring(5, 7) + " / " +
post?.startDate?.substring(8, 10) + " - " +
post?.expireDate?.substring(5, 7) + " / " +
post?.expireDate?.substring(8, 10)
val time = post.startDate?.substring(5, 7) + " / " +
post.startDate?.substring(8, 10) + " - " +
post.expireDate?.substring(5, 7) + " / " +
post.expireDate?.substring(8, 10)
holder.itemView.home_post_timestamp.text = time
Glide.with(mContext).load(post?.imageUrl)
Glide.with(mContext).load(post.imageUrl)
.fitCenter()
.centerCrop()
.into(holder.itemView.home_post_iv)
/** Adds dynamically generated accent color from the fetched image to the news card */
var accentColor: Int = getColor(mContext, R.color.black)
mActivity.lifecycleScope.launch(Dispatchers.Default) {
val bitmap = Glide.with(mContext).load(post?.imageUrl).submit().get().toBitmap()
val bitmap = Glide.with(mContext).load(post.imageUrl).submit().get().toBitmap()
// Create palette from bitmap
fun createPaletteSync(bitmap: Bitmap): Palette = Palette.from(bitmap).generate()
val vibrantSwatch: Palette.Swatch? = createPaletteSync(bitmap).darkVibrantSwatch
Expand Down Expand Up @@ -443,25 +414,8 @@ class HomeAdapter2(private val dataModel: HomepageDataModel) :
}
}

// Returns an announcement for a Penn Mobile feature, such as Spring Fling
private fun bindFeatureCell(holder: ViewHolder, cell: HomeCell) {
val info = cell.info
holder.itemView.home_post_title?.text = info?.title
holder.itemView.home_post_subtitle?.text = info?.description
holder.itemView.home_post_source?.text = info?.source
holder.itemView.home_post_timestamp?.text = info?.timestamp
if (info?.imageUrl != null) {
Glide.with(mContext).load(info.imageUrl).fitCenter().centerCrop().into(holder.itemView.home_post_iv)
}

// For now, we only use Feature cards for Spring Fling so we show the Fling Fragment
holder.itemView.home_post_card.setOnClickListener {
mActivity.fragmentTransact(FlingFragment(), false)
}
}

private fun bindPollCell(holder: ViewHolder, cell: HomeCell, position: Int) {
val poll = (cell as PollCell).poll
private fun bindPollCell(holder: ViewHolder, cell: PollCell, position: Int) {
val poll = cell.poll
holder.itemView.home_card_title?.text = poll.question
holder.itemView.home_card_subtitle_2?.text = "${poll.totalVotes} Votes"
if(poll.clubCode != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pennapps.labs.pennmobile.classes

data class CalendarCell(val events: List<CalendarEvent>) : HomeCell2() {
init {
type = "calendar"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pennapps.labs.pennmobile.classes

data class DiningCell(val venues: List<Int>) : HomeCell2() {
init {
type = "dining"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pennapps.labs.pennmobile.classes

open class HomeCell2 {
var type : String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.pennapps.labs.pennmobile.classes

interface HomepageDataModel {
fun getSize() : Int
fun getCell(position: Int) : HomeCell
fun getCell(position: Int) : HomeCell2
fun notifyPostBlurLoaded()
fun notifyNewsBlurLoaded()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
private const val LAUNDRY_POS = 5
}

private val homepageCells = mutableListOf<HomeCell>()
private val homepageCells = mutableListOf<HomeCell2>()
private val cellMutex = Mutex()
private val _blurViewsLoaded = MutableLiveData<Boolean>(false)
private val _blurViewsLoaded = MutableLiveData(false)
val blurViewsLoaded: LiveData<Boolean>
get() = _blurViewsLoaded

Expand All @@ -37,9 +37,15 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {

init {
for (i in 1..NUM_CELLS) {
homepageCells.add(HomeCell())
homepageCells.add(HomeCell2())
}
}

fun resetBlurViews() {
setPostBlurView(false)
setNewsBlurView(false)
}

@Synchronized
fun updateHomePageCells(studentLife: StudentLife, bearerToken: String, deviceID: String,
update: (Int) -> Unit, callback: () -> Unit) {
Expand Down Expand Up @@ -77,7 +83,7 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
callback.invoke()
}

private fun addCell(cell: HomeCell, pos: Int) = runBlocking {
private fun addCell(cell: HomeCell2, pos: Int) = runBlocking {
cellMutex.withLock {
homepageCells[pos] = cell
}
Expand Down Expand Up @@ -107,7 +113,10 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
newsCell.info?.article = article
newsCell.type = "news"

addCell(newsCell, NEWS_POS)
val newsCell2 = NewsCell(article)
Log.i("HC2", "${newsCell2.type}")

addCell(newsCell2, NEWS_POS)
latch.countDown()
}, { throwable ->
throwable.printStackTrace()
Expand All @@ -121,7 +130,10 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
calendar.type = "calendar"
calendar.events = events

addCell(calendar, CALENDAR_POS)
val calendarCell = CalendarCell(events)
Log.i("HC2", "${calendarCell.type}")

addCell(calendarCell, CALENDAR_POS)
latch.countDown()
}, { throwable ->
throwable.printStackTrace()
Expand All @@ -140,7 +152,10 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
}
laundryCell.info = laundryCellInfo

addCell(laundryCell, LAUNDRY_POS)
val laundryCell2 = if (preferences.isNullOrEmpty()) LaundryCell(0) else LaundryCell(preferences[0])
Log.i("HC2", "${laundryCell2.type}")

addCell(laundryCell2, LAUNDRY_POS)
latch.countDown()
}, { throwable ->
throwable.printStackTrace()
Expand All @@ -156,7 +171,10 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
postCell.type = "post"
postCell.info?.post = post[0]

addCell(postCell, POST_POS)
val postCell2 = PostCell(post[0])
Log.i("HC2", "${postCell2.type}")

addCell(postCell2, POST_POS)
} else {
setPostBlurView(true)
}
Expand Down Expand Up @@ -189,7 +207,10 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
diningCellInfo.venues = venues
diningCell.info = diningCellInfo

addCell(diningCell, DINING_POS)
val diningCell2 = DiningCell(venues)
Log.i("HC2", "${diningCell2.type}")

addCell(diningCell2, DINING_POS)
latch.countDown()
}, { throwable ->
throwable.printStackTrace()
Expand All @@ -201,13 +222,11 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
postBlurMutex.withLock {
postBlurViewLoaded = status
}
updateBlurViewStatus()
}
private fun setNewsBlurView(status: Boolean) = runBlocking {
newsBlurMutex.withLock {
newsBlurViewLoaded = status
}
updateBlurViewStatus()
}
private fun updateBlurViewStatus() = runBlocking {
postBlurMutex.lock()
Expand All @@ -226,17 +245,19 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {

override fun notifyPostBlurLoaded() {
setPostBlurView(true)
updateBlurViewStatus()
}

override fun notifyNewsBlurLoaded() {
setNewsBlurView(true)
updateBlurViewStatus()
}

override fun getSize(): Int {
return homepageCells.size
}

override fun getCell(position: Int): HomeCell {
override fun getCell(position: Int): HomeCell2 {
return homepageCells[position]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pennapps.labs.pennmobile.classes

data class LaundryCell(val roomId: Int) : HomeCell2() {
init {
type = "laundry"
}
}
Loading

0 comments on commit 9f4f485

Please sign in to comment.