Skip to content

Commit

Permalink
Add proper equality checks on update
Browse files Browse the repository at this point in the history
  • Loading branch information
meiron03 committed Jan 5, 2024
1 parent 1d1af00 commit 67be5b9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,29 @@ class Article {
@SerializedName("link")
@Expose
val articleUrl: String? = null
}

override fun equals(other: Any?) : Boolean {
return when(other) {
is Article -> {
this.imageUrl == other.imageUrl &&
this.source == other.source &&
this.title == other.title &&
this.subtitle == other.subtitle &&
this.timestamp == other.timestamp &&
this.articleUrl == other.articleUrl
} else -> false
}
}

override fun hashCode() : Int {
// lazy hash function but we don't use this method anyways
val urlHash = imageUrl.hashCode().toString()
val sourceHash = source.hashCode().toString()
val titleHash = title.hashCode().toString()
val subtitleHash = subtitle.hashCode().toString()
val timeHash = timestamp.hashCode().toString()
val articleHash = articleUrl.hashCode().toString()

return (urlHash + sourceHash + titleHash + subtitleHash + timeHash + articleHash).hashCode()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,19 @@ class CalendarEvent {
@SerializedName("date")
@Expose
var date: String? = null
}

override fun equals(other: Any?) : Boolean {
return when(other) {
is CalendarEvent -> {
this.name == other.name && this.date == other.date
} else -> false
}
}

override fun hashCode() : Int {
// lazy hash function but we don't use this method anyways
val nameHash = name.hashCode().toString()
val dateHash = date.hashCode().toString()
return (nameHash + dateHash).hashCode()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
update: (Int) -> Unit, callback: () -> Unit) {
val prevList = homepageCells.toList()
populateHomePageCells(studentLife, bearerToken, deviceID) {
for (i in 0 until NUM_CELLS) {
if (prevList[i] != homepageCells[i]) {
update(i)
} else {
Log.i("CellUpdates", "saved an update")
}
}
callback.invoke()
for (i in 0 until NUM_CELLS) {
if (prevList[i] != homepageCells[i]) {
update(i)
} else {
Log.i("CellUpdates", "saved an update ${i}")
}
}
callback.invoke()
}
}

Expand Down Expand Up @@ -98,7 +98,7 @@ class HomepageViewModel : HomepageDataModel, ViewModel() {
addCell(HomeCell2(), LAUNDRY_POS)
addCell(HomeCell2(), POST_POS)
addCell(HomeCell2(), DINING_POS)

setPostBlurView(true)
}

Expand Down

0 comments on commit 67be5b9

Please sign in to comment.