From 67be5b9ad510b592ac88ab1a88526c6807e1fc8e Mon Sep 17 00:00:00 2001 From: meiron03 Date: Fri, 5 Jan 2024 15:20:44 -0500 Subject: [PATCH] Add proper equality checks on update --- .../labs/pennmobile/classes/Article.kt | 27 ++++++++++++++++++- .../labs/pennmobile/classes/CalendarEvent.kt | 17 +++++++++++- .../pennmobile/classes/HomepageViewModel.kt | 18 ++++++------- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/Article.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/Article.kt index 72b63c7f..de139a6e 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/Article.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/Article.kt @@ -28,4 +28,29 @@ class Article { @SerializedName("link") @Expose val articleUrl: String? = null -} \ No newline at end of file + + 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() + } +} diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/CalendarEvent.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/CalendarEvent.kt index 267b80bc..617e9ec9 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/CalendarEvent.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/CalendarEvent.kt @@ -10,4 +10,19 @@ class CalendarEvent { @SerializedName("date") @Expose var date: String? = null -} \ No newline at end of file + + 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() + } +} diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt index 50f0c7b9..44806944 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/HomepageViewModel.kt @@ -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() } } @@ -98,7 +98,7 @@ class HomepageViewModel : HomepageDataModel, ViewModel() { addCell(HomeCell2(), LAUNDRY_POS) addCell(HomeCell2(), POST_POS) addCell(HomeCell2(), DINING_POS) - + setPostBlurView(true) }