Skip to content

Commit

Permalink
caching of api data to room database fix -it worked
Browse files Browse the repository at this point in the history
  • Loading branch information
mutukuian committed May 13, 2024
1 parent b62d223 commit 11a6517
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object RepositoryModule {
context.applicationContext,
ShipDatabase::class.java,
"ship_database"
).build()
).fallbackToDestructiveMigration().build()
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.example.kocelainterview.data.local_data_source
import androidx.room.Database
import androidx.room.RoomDatabase

@Database(entities = [ShipEntity::class], version = 1, exportSchema = false)
@Database(entities = [ShipEntity::class], version = 2, exportSchema = false)
abstract class ShipDatabase:RoomDatabase (){
abstract fun shipDao():ShipDao
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ data class ShipEntity(

val ship_id: String,
val active: Boolean,
val image: String,
val image: String?,
val ship_name: String,
@PrimaryKey
val weight_kg: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class ShipDto(
// val `class`: Int,
// val course_deg: Any,
// val home_port: String,
val image: String,
val image: String?,
// val imo: Int,
// val mmsi: Int,
// val roles: List<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import com.example.kocelainterview.data.remote.dto.ShipDetailDto
import com.example.kocelainterview.data.remote.dto.ShipDto
import com.example.kocelainterview.data.remote.dto.toEntity
import com.example.kocelainterview.domain.repository_interface.ShipRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

class ShipRepositoryImpl @Inject constructor(
private val api: ShipsApi,
private val shipDao: ShipDao
) : ShipRepository {

override suspend fun getShips(): List<ShipDto> {
override suspend fun getShips(): List<ShipDto> = withContext(Dispatchers.IO) {
// Check Room database first
val cachedShips = shipDao.getShips()
if (cachedShips.isNotEmpty()) {
return cachedShips.map { it.toShipDto() }
return@withContext cachedShips.map { it.toShipDto() }
}

// Fetch from API if not found in cache
Expand All @@ -27,10 +29,10 @@ class ShipRepositoryImpl @Inject constructor(
// Save to Room database
shipDao.saveShips(ships.map { it.toEntity() })

return ships
return@withContext ships
}

override suspend fun getShipById(shipId: String): ShipDetailDto {
return api.getShipById(shipId)
override suspend fun getShipById(shipId: String): ShipDetailDto= withContext(Dispatchers.IO) {
return@withContext api.getShipById(shipId)
}
}

0 comments on commit 11a6517

Please sign in to comment.