Skip to content

Commit 329cece

Browse files
committed
Test the instance of object instead of null
1 parent 517fd55 commit 329cece

File tree

12 files changed

+67
-35
lines changed

12 files changed

+67
-35
lines changed

app/src/main/java/com/android/sample/app/database/Room.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import androidx.room.Database
44
import androidx.room.RoomDatabase
55
import androidx.room.TypeConverters
66
import com.android.sample.app.database.dashboard.DashboardDao
7-
import com.android.sample.app.database.dashboard.DbDashboard
7+
import com.android.sample.app.database.dashboard.DashboardEntity
88
import com.android.sample.app.database.dashboard.DbLinkConverter
9-
import com.android.sample.app.database.section.DbSection
9+
import com.android.sample.app.database.section.SectionEntity
1010
import com.android.sample.app.database.section.SectionDao
1111

12-
@Database(entities = [DbDashboard::class, DbSection::class], version = 1, exportSchema = false)
12+
@Database(entities = [DashboardEntity::class, SectionEntity::class], version = 1, exportSchema = false)
1313
@TypeConverters(DbLinkConverter::class)
1414
abstract class AppDatabase : RoomDatabase() {
1515
abstract fun dashboardDao(): DashboardDao

app/src/main/java/com/android/sample/app/database/dashboard/DashboardDao.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import androidx.room.Query
99
interface DashboardDao {
1010

1111
@Insert(onConflict = OnConflictStrategy.REPLACE)
12-
suspend fun insert(dashboard: DbDashboard)
12+
suspend fun insert(dashboard: DashboardEntity)
1313

1414
@Query("SELECT * FROM dashboard")
15-
suspend fun getDashboard(): DbDashboard?
15+
suspend fun getDashboard(): DashboardEntity?
1616
}

app/src/main/java/com/android/sample/app/database/dashboard/DbDashboard.kt renamed to app/src/main/java/com/android/sample/app/database/dashboard/DashboardEntity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import com.android.sample.app.domain.Links
88

99

1010
@Entity(tableName = "dashboard")
11-
class DbDashboard(
11+
class DashboardEntity(
1212
@PrimaryKey val primaryKey: String = "dashboard",
13-
val sections: List<DbLink>
13+
val sections: List<LinkEntity>
1414
)
1515

16-
fun DbDashboard.asDomainModel(): Dashboard {
16+
fun DashboardEntity.asDomainModel(): Dashboard {
1717
return Dashboard(
1818
links = Links(sections = this.sections.map {
1919
Link(id = it.id, title = it.title, href = it.href)

app/src/main/java/com/android/sample/app/database/dashboard/DbLinkConverter.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import com.google.gson.Gson
66
class DbLinkConverter {
77

88
@TypeConverter
9-
fun jsonToList(value: String): List<DbLink> =
10-
Gson().fromJson(value, Array<DbLink>::class.java).toList()
9+
fun jsonToList(value: String): List<LinkEntity> =
10+
Gson().fromJson(value, Array<LinkEntity>::class.java).toList()
1111

1212
@TypeConverter
13-
fun listToJson(value: List<DbLink?>) = Gson().toJson(value.filterNotNull())
13+
fun listToJson(value: List<LinkEntity?>) = Gson().toJson(value.filterNotNull())
1414
}

app/src/main/java/com/android/sample/app/database/dashboard/DbLink.kt renamed to app/src/main/java/com/android/sample/app/database/dashboard/LinkEntity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.android.sample.app.database.dashboard
22

3-
class DbLink(
3+
class LinkEntity(
44
val id: String,
55
val title: String,
66
val href: String

app/src/main/java/com/android/sample/app/database/section/SectionDao.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import androidx.room.Query
88
@Dao
99
interface SectionDao {
1010
@Insert(onConflict = OnConflictStrategy.REPLACE)
11-
suspend fun insert(data: DbSection)
11+
suspend fun insert(data: SectionEntity)
1212

1313
@Query("SELECT * FROM sections WHERE id=:id")
14-
suspend fun getSection(id: String): DbSection?
14+
suspend fun getSection(id: String): SectionEntity?
1515
}

app/src/main/java/com/android/sample/app/database/section/DbSection.kt renamed to app/src/main/java/com/android/sample/app/database/section/SectionEntity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import androidx.room.PrimaryKey
55
import com.android.sample.app.domain.Section
66

77
@Entity(tableName = "sections")
8-
class DbSection(
8+
class SectionEntity(
99
@PrimaryKey val id: String,
1010
val title: String,
1111
val description: String
1212
)
1313

1414

15-
fun DbSection.asDomainModel(): Section =
15+
fun SectionEntity.asDomainModel(): Section =
1616
Section(sectionId = this.id, title = this.title, description = this.description)
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
package com.android.sample.app.domain
22

3-
import com.android.sample.app.database.dashboard.DbDashboard
4-
import com.android.sample.app.database.dashboard.DbLink
3+
import com.android.sample.app.database.dashboard.DashboardEntity
4+
import com.android.sample.app.database.dashboard.LinkEntity
55
import com.google.gson.annotations.SerializedName
66

77
class Dashboard(
88
@SerializedName("_links") val links: Links
9-
)
9+
) {
10+
override fun equals(other: Any?): Boolean {
11+
if (this === other) return true
12+
if (other == null || javaClass != other.javaClass) return false
13+
return links == (other as Dashboard).links
14+
}
1015

11-
fun Dashboard.asDatabaseModel(): DbDashboard {
12-
return DbDashboard(
16+
override fun hashCode(): Int {
17+
return links.hashCode()
18+
}
19+
}
20+
21+
fun Dashboard.asDatabaseModel(): DashboardEntity {
22+
return DashboardEntity(
1323
sections = this.links.sections.map {
14-
DbLink(id = it.id, title = it.title, href = it.href)
24+
LinkEntity(id = it.id, title = it.title, href = it.href)
1525
}
1626
)
1727
}

app/src/main/java/com/android/sample/app/domain/Links.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,14 @@ import com.google.gson.annotations.SerializedName
44

55
class Links(
66
@SerializedName("viaplay:sections") val sections: List<Link>
7-
)
7+
) {
8+
override fun equals(other: Any?): Boolean {
9+
if (this === other) return true
10+
if (other == null || javaClass != other.javaClass) return false
11+
return sections == (other as Links).sections
12+
}
13+
14+
override fun hashCode(): Int {
15+
return sections.hashCode()
16+
}
17+
}
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package com.android.sample.app.domain
22

3-
import com.android.sample.app.database.section.DbSection
3+
import com.android.sample.app.database.section.SectionEntity
44

55
class Section(
66
val sectionId: String,
77
val title: String,
88
val description: String
9-
)
9+
) {
10+
override fun equals(other: Any?): Boolean {
11+
if (this === other) return true
12+
if (other == null || javaClass != other.javaClass) return false
13+
return sectionId == (other as Section).sectionId
14+
}
1015

11-
fun Section.asDatabaseModel(): DbSection =
12-
DbSection(id = this.sectionId, title = this.title, description = this.description)
16+
override fun hashCode(): Int {
17+
return sectionId.hashCode()
18+
}
19+
}
20+
21+
fun Section.asDatabaseModel(): SectionEntity =
22+
SectionEntity(id = this.sectionId, title = this.title, description = this.description)

0 commit comments

Comments
 (0)