Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement navigation feature #6

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading