Skip to content

Commit

Permalink
Add Record Mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanLiou committed Jun 23, 2023
1 parent e8d429a commit ec0506d
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface WorkoutRecordDataSource {
suspend fun insertRecordDetails(
workoutRecordId: Long,
createAt: String,
sportRecordTypeId: Long,
weight: Double?,
reps: Long?,
time: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class WorkoutRecordDataSourceImpl(
override suspend fun insertRecordDetails(
workoutRecordId: Long,
createAt: String,
sportRecordTypeId: Long,
weight: Double?,
reps: Long?,
time: String?,
Expand All @@ -42,6 +43,7 @@ class WorkoutRecordDataSourceImpl(
workoutRecordId = workoutRecordId,
createAt = createAt,
lastModified = createAt,
sportRecordTypeId = sportRecordTypeId,
weight = weight,
reps = reps,
time = time,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.rayliu.commonmain.data.mapper

import com.rayliu.commonmain.data.database.DateTimeConverter
import com.rayliu.commonmain.data.database.RecordDetails
import com.rayliu.commonmain.data.mapper.basic.Mapper
import com.rayliu.commonmain.data.mapper.basic.NullableInputListMapper
import com.rayliu.commonmain.domain.model.DistanceTimeRecord
import com.rayliu.commonmain.domain.model.Record
import com.rayliu.commonmain.domain.model.SportRecordType
import com.rayliu.commonmain.domain.model.UnknownRecord
import com.rayliu.commonmain.domain.model.WeightRepsRecord
import com.rayliu.commonmain.domain.model.WeightTimeRecord
import org.koin.core.annotation.Factory

@Factory
class RecordMapper(
private val sportRecordTypeMapper: SportRecordTypeMapper,
private val dateTimeConverter: DateTimeConverter
) : Mapper<RecordDetails, Record> {
override fun map(input: RecordDetails): Record {
val type = sportRecordTypeMapper.map(input.sportRecordTypeId.toInt())
return when (type) {
SportRecordType.WEIGHT_REPS -> WeightRepsRecord(
id = input.id.toInt(),
workoutRecordId = input.workoutRecordId.toInt(),
createdAt = dateTimeConverter.toLocalDateTime(input.createAt),
lastModified = dateTimeConverter.toLocalDateTime(input.lastModified),
weight = input.weight?.toFloat() ?: 0f,
reps = input.reps?.toInt() ?: 0
)
SportRecordType.WEIGHT_TIME -> WeightTimeRecord(
id = input.id.toInt(),
workoutRecordId = input.workoutRecordId.toInt(),
createdAt = dateTimeConverter.toLocalDateTime(input.createAt),
lastModified = dateTimeConverter.toLocalDateTime(input.lastModified),
weight = input.weight?.toFloat() ?: 0f,
time = input.time.orEmpty()
)
SportRecordType.DISTANCE_TIME -> DistanceTimeRecord(
id = input.id.toInt(),
workoutRecordId = input.workoutRecordId.toInt(),
createdAt = dateTimeConverter.toLocalDateTime(input.createAt),
lastModified = dateTimeConverter.toLocalDateTime(input.lastModified),
distance = input.distance?.toFloat() ?: 0f,
time = input.time.orEmpty()
)
SportRecordType.UNKNOWN -> UnknownRecord(
id = input.id.toInt(),
workoutRecordId = input.workoutRecordId.toInt(),
createdAt = dateTimeConverter.toLocalDateTime(input.createAt),
lastModified = dateTimeConverter.toLocalDateTime(input.lastModified)
)
}
}
}

@Factory
class RecordListMapper(
private val mapper: RecordMapper
) : NullableInputListMapper<RecordDetails, Record> {
override fun map(input: List<RecordDetails>?): List<Record> {
return input?.map { mapper.map(it) }.orEmpty()
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.rayliu.commonmain.data.mapper

import com.rayliu.commonmain.data.mapper.basic.Mapper
import com.rayliu.commonmain.domain.model.SportsRecordType
import com.rayliu.commonmain.domain.model.SportRecordType
import org.koin.core.annotation.Factory

@Factory
class SportRecordTypeMapper : Mapper<Int, SportsRecordType> {
override fun map(input: Int): SportsRecordType {
if (!SportsRecordType.map.containsKey(input)) {
return SportsRecordType.UNKNOWN
class SportRecordTypeMapper : Mapper<Int, SportRecordType> {
override fun map(input: Int): SportRecordType {
if (!SportRecordType.map.containsKey(input)) {
return SportRecordType.UNKNOWN
}
return SportsRecordType.map[input] ?: SportsRecordType.UNKNOWN
return SportRecordType.map[input] ?: SportRecordType.UNKNOWN
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.rayliu.commonmain.domain.model

import kotlinx.datetime.LocalDateTime

interface Record {
val id: Int
val workoutRecordId: Int
val createdAt: LocalDateTime
val lastModified: LocalDateTime
}

data class WeightRepsRecord(
override val id: Int,
override val workoutRecordId: Int,
override val createdAt: LocalDateTime,
override val lastModified: LocalDateTime,
val weight: Float,
val reps: Int
) : Record

data class WeightTimeRecord(
override val id: Int,
override val workoutRecordId: Int,
override val createdAt: LocalDateTime,
override val lastModified: LocalDateTime,
val weight: Float,
val time: String
) : Record

data class DistanceTimeRecord(
override val id: Int,
override val workoutRecordId: Int,
override val createdAt: LocalDateTime,
override val lastModified: LocalDateTime,
val distance: Float,
val time: String
) : Record

data class UnknownRecord(
override val id: Int,
override val workoutRecordId: Int,
override val createdAt: LocalDateTime,
override val lastModified: LocalDateTime
) : Record
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.rayliu.commonmain.domain.model

import kotlinx.collections.immutable.ImmutableList
import kotlinx.datetime.LocalDateTime

data class SportRecord(
val id: Int,
val workoutId: Int,
val records: ImmutableList<Record>,
val createdAt: LocalDateTime
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.rayliu.commonmain.domain.model

enum class SportsRecordType(val id: Int) {
enum class SportRecordType(val id: Int) {
WEIGHT_REPS(0),
WEIGHT_TIME(1),
DISTANCE_TIME(2),
UNKNOWN(-1);

companion object {
val map = SportsRecordType.values().associateBy { it.id }
val map = SportRecordType.values().associateBy { it.id }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ data class WorkoutInfo(
val id: Int,
val name: String,
val categoryId: Int,
val sportRecordType: SportsRecordType,
val sportRecordType: SportRecordType,
val createdAt: LocalDateTime,
val lastModified: LocalDateTime
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE TABLE RecordDetails (
workoutRecordId INTEGER NOT NULL,
createAt Text NOT NULL ,
lastModified TEXT NOT NULL,
sportRecordTypeId INTEGER NOT NULL,
weight REAL,
reps INTEGER,
time TEXT,
Expand All @@ -13,7 +14,7 @@ CREATE TABLE RecordDetails (
insertNewRecord:
INSERT OR REPLACE
INTO RecordDetails
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);

getRecordDetails:
SELECT *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.foundation.lazy.ScalingLazyListState
import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.compose.rotaryinput.rotaryWithScroll
import com.rayliu.commonmain.domain.model.SportsRecordType
import com.rayliu.commonmain.domain.model.SportRecordType
import com.rayliu.commonmain.domain.model.WorkoutInfo
import com.rayliu.gymnote.wearos.theme.GymNoteTheme
import com.rayliu.gymnote.wearos.ui.OptionItem
Expand Down Expand Up @@ -90,7 +90,7 @@ private fun WorkoutListScreenPreview() {
id = 6278,
name = "Odessa Huber",
categoryId = 4839,
sportRecordType = SportsRecordType.WEIGHT_REPS,
sportRecordType = SportRecordType.WEIGHT_REPS,
createdAt = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()),
lastModified = Clock.System.now()
.toLocalDateTime(TimeZone.currentSystemDefault())
Expand All @@ -99,7 +99,7 @@ private fun WorkoutListScreenPreview() {
id = 4784,
name = "Blanca Willis",
categoryId = 7617,
sportRecordType = SportsRecordType.WEIGHT_REPS,
sportRecordType = SportRecordType.WEIGHT_REPS,
createdAt = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()),
lastModified = Clock.System.now()
.toLocalDateTime(TimeZone.currentSystemDefault())
Expand Down

0 comments on commit ec0506d

Please sign in to comment.