-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
136 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
shared/src/commonMain/kotlin/com/rayliu/commonmain/data/mapper/RecordMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
12 changes: 6 additions & 6 deletions
12
shared/src/commonMain/kotlin/com/rayliu/commonmain/data/mapper/SportRecordTypeMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
shared/src/commonMain/kotlin/com/rayliu/commonmain/domain/model/Record.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
11 changes: 11 additions & 0 deletions
11
shared/src/commonMain/kotlin/com/rayliu/commonmain/domain/model/SportRecord.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
4 changes: 2 additions & 2 deletions
4
...mmonmain/domain/model/SportsRecordType.kt → ...ommonmain/domain/model/SportRecordType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters