Skip to content

Commit

Permalink
Merge pull request #6 from mutukuian/Caching
Browse files Browse the repository at this point in the history
implement navigation feature
  • Loading branch information
mutukuian committed May 13, 2024
2 parents 23e0e3a + 6b28847 commit 00ddc72
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.kocelainterview.data.local_data_source

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query

@Dao
interface ShipDao {
@Query("SELECT * FROM ships")
fun getShips(): List<ShipEntity>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun saveShips(ships: List<ShipEntity>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.kocelainterview.data.local_data_source

import androidx.room.Database
import androidx.room.RoomDatabase

@Database(entities = [ShipEntity::class], version = 1)
abstract class ShipDatabase:RoomDatabase (){
abstract fun shipDao():ShipDao
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.kocelainterview.data.local_data_source

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.example.kocelainterview.data.remote.dto.ShipDto

@Entity(tableName = "ships")
data class ShipEntity(
@PrimaryKey val ship_id: String,
val active: Boolean,
val image: String?,
val ship_name: String,
val weight_kg: Int,
val year_built: Int
)


fun ShipEntity.toShipDto(): ShipDto {
return ShipDto(
ship_id = ship_id,
active = active,
image = image,
ship_name = ship_name,
weight_kg = weight_kg,
year_built = year_built

)
}




Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
package com.example.kocelainterview.data.remote.dto


import com.example.kocelainterview.data.local_data_source.ShipEntity
import com.example.kocelainterview.domain.model.Ship



data class ShipDto(
val abs: Int,
// val abs: Int,
val active: Boolean,
val attempted_landings: Any,
val `class`: Int,
val course_deg: Any,
val home_port: String,
// val attempted_landings: Any?,
// val `class`: Int,
// val course_deg: Any,
// val home_port: String,
val image: String,
val imo: Int,
val mmsi: Int,
val roles: List<String>,
// val imo: Int,
// val mmsi: Int,
// val roles: List<String>,
val ship_id: String,
val ship_model: Any,
// val ship_model: Any?,
val ship_name: String,
val ship_type: String,
val speed_kn: Int,
val status: String,
val successful_landings: Any,
val url: String,
// val ship_type: String,
// val speed_kn: Int,
// val status: String,
// val successful_landings: Any?,
// val url: String,
val weight_kg: Int,
val weight_lbs: Int,
// val weight_lbs: Int,
val year_built: Int
)

fun ShipDto.toEntity(): ShipEntity {
return ShipEntity(
ship_id = ship_id,
active = active,
image = image,
ship_name = ship_name,
weight_kg = weight_kg,
year_built = year_built
)
}





fun ShipDto.toShip():Ship{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
package com.example.kocelainterview.data.repository

import com.example.kocelainterview.data.local_data_source.ShipDao
import com.example.kocelainterview.data.local_data_source.toShipDto
import com.example.kocelainterview.data.remote.api_service.ShipsApi
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 javax.inject.Inject

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

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

// Fetch from API if not found in cache
val ships = api.getShips()

// Save to Room database
shipDao.saveShips(ships.map { it.toEntity() })

return ships
}

override suspend fun getShipById(shipId: String): ShipDetailDto {
return api.getShipById(shipId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ package com.example.kocelainterview.domain.model
data class Ship(

val active: Boolean,
// val home_port: String,
val image: String?,
val ship_id: String,
//val ship_model: Any,
val ship_name: String,
//val ship_type: String,
// val speed_kn: Int,
// val status: String,
val weight_kg: Int,
// val url: String,
//val weight_lbs: Int,
val year_built: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.example.kocelainterview.domain.use_case.get_ships
import com.example.kocelainterview.common.core.Resource
import com.example.kocelainterview.data.remote.dto.ShipDto
import com.example.kocelainterview.data.remote.dto.toShip
import com.example.kocelainterview.domain.model.Ship
import com.example.kocelainterview.domain.repository_interface.ShipRepository
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
Expand Down

0 comments on commit 00ddc72

Please sign in to comment.