Skip to content

Commit

Permalink
redisign for ships details screen and some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NicosNicolaou16 committed Jul 28, 2024
1 parent 7bd7b1e commit a8b79d7
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ private fun ShipDetailsView(
paddingValues: PaddingValues
) {
val context = LocalContext.current
Box(contentAlignment = Alignment.TopCenter) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Box(contentAlignment = Alignment.TopStart) {
Column(horizontalAlignment = Alignment.Start) {
AsyncImage(
model = ImageRequest.Builder(context = context).apply {
data(shipData?.image)
Expand All @@ -99,6 +99,8 @@ private fun ShipDetailsView(
.height(height = 300.dp)
)
BasicInfo(shipData)
HorizontalRolesAndMissionsList(shipData?.missions?.map { it.name ?: "" }
?.toMutableList() ?: mutableListOf())
}
}
}
Expand All @@ -112,7 +114,7 @@ private fun BasicInfo(shipData: ShipsModel?) {
.padding(9.dp)
) {
Column(horizontalAlignment = Alignment.Start) {
androidx.compose.material.Text(
Text(
text = stringResource(R.string.ship_name) + shipData?.ship_name,
modifier = Modifier.width(width = 300.dp),
maxLines = 2,
Expand All @@ -124,7 +126,7 @@ private fun BasicInfo(shipData: ShipsModel?) {
),
color = Color.White,
)
androidx.compose.material.Text(
Text(
text = stringResource(R.string.ship_type) + shipData?.ship_type,
style = TextStyle(
fontSize = 21.sp,
Expand All @@ -138,7 +140,7 @@ private fun BasicInfo(shipData: ShipsModel?) {
verticalArrangement = Arrangement.spacedBy(3.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
androidx.compose.material.Text(
Text(
text = stringResource(R.string.is_active),
style = TextStyle(
fontSize = 21.sp,
Expand All @@ -155,4 +157,42 @@ private fun BasicInfo(shipData: ShipsModel?) {
)
}
}
}

@Composable
private fun HorizontalRolesAndMissionsList(values: MutableList<String>) {
Column(horizontalAlignment = Alignment.Start) {
Box(modifier = Modifier.padding(11.dp)) {
Text(
text = stringResource(R.string.missions),
style = TextStyle(
fontSize = 21.sp,
textAlign = TextAlign.Center,
fontWeight = FontWeight.Bold
),
)
}
LazyRow {
items(values) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.padding(11.dp)
.clip(shape = RoundedCornerShape(9.dp))
.background(color = Color.DarkGray)
.size(100.dp),
) {
Text(
text = it,
style = TextStyle(
fontSize = 21.sp,
textAlign = TextAlign.Center,
fontWeight = FontWeight.Bold
),
color = Color.Gray,
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ package com.nicos.ships.data.room_database.init_database
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.nicos.ships.data.room_database.ships.MissionsDao
import com.nicos.ships.data.room_database.ships.MissionsModel
import com.nicos.ships.data.room_database.ships.PositionDao
import com.nicos.ships.data.room_database.ships.PositionModel
import com.nicos.ships.data.room_database.ships.ShipsDao
import com.nicos.ships.data.room_database.ships.ShipsModel
import com.nicos.ships.data.room_database.type_converter.ConverterMission
import com.nicos.ships.data.room_database.type_converter.ConverterPosition
import javax.inject.Inject

@Database(
entities = [ShipsModel::class, PositionModel::class],
entities = [ShipsModel::class, PositionModel::class, MissionsModel::class],
version = 1,
exportSchema = false
)
@TypeConverters(
ConverterPosition::class,
ConverterMission::class,
)
abstract class MyRoomDatabase : RoomDatabase() {
abstract fun shipDao(): ShipsDao
abstract fun positionDao(): PositionDao
abstract fun missionsDao(): MissionsDao

@Inject
internal lateinit var myRoomDatabase: MyRoomDatabase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.nicos.ships.data.room_database.ships

import androidx.room.Dao
import androidx.room.Query
import com.nicos.ships.data.room_database.init_database.BaseDao

@Dao
interface MissionsDao : BaseDao<MissionsModel, MutableList<MissionsModel>> {

@Query("SELECT * FROM MissionsModel")
suspend fun getAllMissions(): MutableList<MissionsModel>

@Query("DELETE FROM missionsmodel")
suspend fun deleteAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.nicos.ships.data.room_database.ships

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.nicos.ships.data.room_database.init_database.MyRoomDatabase

/**
* One to Many
* */
@Entity
data class MissionsModel(
@PrimaryKey(autoGenerate = true)
var id: Long = 0,
var name: String?,
var flight: Int?,
var ship_id_missions: String?
) {

companion object {
suspend fun insertTheMissions(
missionsModelList: MutableList<MissionsModel>,
shipId: String,
myRoomDatabase: MyRoomDatabase
) {
val missionsList = mutableListOf<MissionsModel>()
missionsModelList.forEach { mission ->
mission.ship_id_missions = shipId
missionsList.add(mission)
}
myRoomDatabase.missionsDao().insertOrReplaceList(missionsList)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import kotlinx.coroutines.flow.flow

@Entity
data class PositionModel(
@PrimaryKey(autoGenerate = true)
var position_id: Long,
var latitude: Double?,
var longitude: Double?
@PrimaryKey(autoGenerate = true)
var position_id: Long,
var latitude: Double?,
var longitude: Double?
) {

constructor() : this(-1, null, null)

companion object {
suspend fun insertThePosition(positionModel: PositionModel, myRoomDatabase: MyRoomDatabase) =
suspend fun insertThePosition(
positionModel: PositionModel,
myRoomDatabase: MyRoomDatabase
) =
flow {
myRoomDatabase.positionDao().insertOrReplaceObject(positionModel)
emit(myRoomDatabase.positionDao().getPosition())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.nicos.ships.data.room_database.ships
import androidx.room.*
import com.google.gson.annotations.SerializedName
import com.nicos.ships.data.room_database.init_database.MyRoomDatabase
import com.nicos.ships.data.room_database.type_converter.ConverterMission
import com.nicos.ships.data.room_database.type_converter.ConverterPosition
import kotlinx.coroutines.flow.flow

Expand All @@ -29,11 +30,35 @@ data class ShipsModel(
var positionId: Long,
var successful_landings: Int?,
var attempted_landings: Int?,
@TypeConverters(ConverterMission::class)
var missions: MutableList<MissionsModel>,
var url: String?,
var image: String?,
) {

constructor() : this("", null,null, null, null,null, null,null, null,null, null,null, null,null, PositionModel(),-1, null,null, null, null)
constructor() : this(
"",
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
PositionModel(),
-1,
null,
null,
mutableListOf(),
null,
null
)

companion object {
suspend fun insertTheShips(
Expand All @@ -60,6 +85,7 @@ data class ShipsModel(
myRoomDatabase.positionDao().deleteAll()
shipsModelList.forEach { ship ->
savePosition(ship, myRoomDatabase)
saveMissions(ship, myRoomDatabase)
shipsModelListSaved.add(ship) //add the ship data into list to insert into the database
}
emit(shipsModelListSaved)
Expand All @@ -74,5 +100,13 @@ data class ShipsModel(
it.position_id //get the position_id from PositionModel and assign to positionId (ShipModel)
}
}

private suspend fun saveMissions(ship: ShipsModel, myRoomDatabase: MyRoomDatabase) {
MissionsModel.insertTheMissions(
ship.missions,
ship.ship_id,
myRoomDatabase
) //insert missions list object
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ package com.nicos.ships.data.room_database.type_converter
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.nicos.ships.data.room_database.ships.MissionsModel
import com.nicos.ships.data.room_database.ships.PositionModel

class ConverterMission {
@TypeConverter
fun fromStringToMissionsList(value: String): MutableList<MissionsModel>? {
return Gson().fromJson(value, object : TypeToken<MutableList<MissionsModel>>() {}.type)
}

@TypeConverter
fun fromMissionsListToString(list: MutableList<MissionsModel>?): String = Gson().toJson(list)
}

class ConverterPosition {

@TypeConverter
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/com/nicos/ships/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat

private val DarkColorScheme = darkColorScheme(
primary = Purple80,
secondary = PurpleGrey80,
primary = Color.DarkGray,
secondary = Color.DarkGray,
tertiary = Pink80
)

private val LightColorScheme = lightColorScheme(
primary = Purple40,
secondary = PurpleGrey40,
primary = Color.DarkGray,
secondary = Color.DarkGray,
tertiary = Pink40

/* Other default colors to override
Expand All @@ -41,7 +42,7 @@ private val LightColorScheme = lightColorScheme(
fun ShipsTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
dynamicColor: Boolean = false,
content: @Composable () -> Unit
) {
val colorScheme = when {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<string name="is_active">Is Active\n</string>
<string name="ship_type">"Ship Type: "</string>
<string name="ship_name">"Ship Name: "</string>
<string name="missions">Missions</string>
</resources>

0 comments on commit a8b79d7

Please sign in to comment.