-
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
1 parent
52214b9
commit fecc8e9
Showing
15 changed files
with
244 additions
and
148 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
modules/dto/src/main/kotlin/ru/astrainteractive/astrarating/model/PlayerModel.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,5 @@ | ||
package ru.astrainteractive.astrarating.model | ||
|
||
import java.util.UUID | ||
|
||
data class PlayerModel(val uuid: UUID, val name: String) |
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,19 @@ | ||
plugins { | ||
kotlin("jvm") | ||
kotlin("plugin.serialization") | ||
} | ||
|
||
dependencies { | ||
// Kotlin | ||
implementation(libs.bundles.kotlin) | ||
// AstraLibs | ||
implementation(libs.minecraft.astralibs.ktxcore) | ||
// klibs | ||
implementation(libs.klibs.mikro) | ||
// Test | ||
testImplementation(libs.bundles.testing.kotlin) | ||
testImplementation(libs.tests.kotlin.test) | ||
// Local | ||
implementation(projects.modules.apiRating) | ||
implementation(projects.modules.dto) | ||
} |
16 changes: 16 additions & 0 deletions
16
.../src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/AllRatingsComponent.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,16 @@ | ||
package ru.astrainteractive.astrarating.feature.allrating | ||
|
||
import kotlinx.coroutines.flow.StateFlow | ||
import ru.astrainteractive.astrarating.dto.UserAndRating | ||
import ru.astrainteractive.astrarating.model.UsersRatingsSort | ||
|
||
interface AllRatingsComponent { | ||
val model: StateFlow<Model> | ||
fun onSortClicked() | ||
fun close() | ||
|
||
data class Model( | ||
val userRatings: List<UserAndRating> = emptyList(), | ||
val sort: UsersRatingsSort = UsersRatingsSort.ASC | ||
) | ||
} |
40 changes: 40 additions & 0 deletions
40
...in/kotlin/ru/astrainteractive/astrarating/feature/allrating/DefaultAllRatingsComponent.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,40 @@ | ||
package ru.astrainteractive.astrarating.feature.allrating | ||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import ru.astrainteractive.astralibs.async.AsyncComponent | ||
import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi | ||
import ru.astrainteractive.astrarating.model.UsersRatingsSort | ||
import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers | ||
import ru.astrainteractive.klibs.mikro.core.util.next | ||
|
||
class DefaultAllRatingsComponent( | ||
private val dbApi: RatingDBApi, | ||
dispatchers: KotlinDispatchers | ||
) : AllRatingsComponent, AsyncComponent() { | ||
override val model = MutableStateFlow(AllRatingsComponent.Model()) | ||
|
||
override fun onSortClicked() { | ||
val nextSort = model.value.sort.next(UsersRatingsSort.values()) | ||
val userRatings = model.value.userRatings | ||
val sortedUserRatings = if (nextSort == UsersRatingsSort.ASC) { | ||
userRatings.sortedBy { it.rating.rating } | ||
} else { | ||
userRatings.sortedByDescending { it.rating.rating } | ||
} | ||
model.update { | ||
it.copy(userRatings = sortedUserRatings) | ||
} | ||
} | ||
|
||
init { | ||
componentScope.launch(dispatchers.IO) { | ||
val userRatings = dbApi.fetchUsersTotalRating().getOrDefault(emptyList()) | ||
model.update { | ||
it.copy(userRatings = userRatings) | ||
} | ||
onSortClicked() | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...lin/ru/astrainteractive/astrarating/feature/playerrating/DefaultPlayerRatingsComponent.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 ru.astrainteractive.astrarating.feature.playerrating | ||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import ru.astrainteractive.astralibs.async.AsyncComponent | ||
import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi | ||
import ru.astrainteractive.astrarating.dto.UserAndRating | ||
import ru.astrainteractive.astrarating.model.PlayerModel | ||
import ru.astrainteractive.astrarating.model.UserRatingsSort | ||
import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers | ||
import ru.astrainteractive.klibs.mikro.core.util.next | ||
|
||
class DefaultPlayerRatingsComponent( | ||
playerModel: PlayerModel, | ||
private val dbApi: RatingDBApi, | ||
private val dispatchers: KotlinDispatchers | ||
) : PlayerRatingsComponent, AsyncComponent() { | ||
override val model = MutableStateFlow( | ||
PlayerRatingsComponent.Model(playerModel = playerModel) | ||
) | ||
|
||
override fun onSortClicked() { | ||
componentScope.launch(dispatchers.IO) { | ||
val newSort = model.value.sort.next(UserRatingsSort.values()) | ||
val previousUserRatings = model.value.userRatings | ||
val newUserRatings = when (newSort) { | ||
UserRatingsSort.DATE_ASC -> previousUserRatings.sortedBy { it.rating.time } | ||
UserRatingsSort.DATE_DESC -> previousUserRatings.sortedByDescending { it.rating.time } | ||
UserRatingsSort.PLAYER_ASC -> previousUserRatings.sortedBy { it.userCreatedReport.id } | ||
UserRatingsSort.PLAYER_DESC -> previousUserRatings.sortedByDescending { it.userCreatedReport.id } | ||
UserRatingsSort.RATING_ASC -> previousUserRatings.sortedBy { it.rating.rating } | ||
UserRatingsSort.RATING_DESC -> previousUserRatings.sortedBy { it.rating.rating } | ||
} | ||
model.update { | ||
it.copy( | ||
sort = newSort, | ||
userRatings = newUserRatings | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onDeleteClicked(item: UserAndRating) { | ||
componentScope.launch(dispatchers.IO) { | ||
dbApi.deleteUserRating(item.rating) | ||
} | ||
} | ||
|
||
private fun reload() { | ||
componentScope.launch(dispatchers.IO) { | ||
val playerModel = model.value.playerModel | ||
val userRatings = dbApi.fetchUserRatings(playerModel.name).getOrDefault(emptyList()) | ||
model.update { | ||
it.copy(userRatings = userRatings) | ||
} | ||
onSortClicked() | ||
} | ||
} | ||
|
||
init { | ||
reload() | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ain/kotlin/ru/astrainteractive/astrarating/feature/playerrating/PlayerRatingsComponent.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,18 @@ | ||
package ru.astrainteractive.astrarating.feature.playerrating | ||
|
||
import kotlinx.coroutines.flow.StateFlow | ||
import ru.astrainteractive.astrarating.dto.UserAndRating | ||
import ru.astrainteractive.astrarating.model.PlayerModel | ||
import ru.astrainteractive.astrarating.model.UserRatingsSort | ||
|
||
interface PlayerRatingsComponent { | ||
val model: StateFlow<Model> | ||
fun onSortClicked() | ||
fun onDeleteClicked(item: UserAndRating) | ||
fun close() | ||
data class Model( | ||
val playerModel: PlayerModel, | ||
val userRatings: List<UserAndRating> = emptyList(), | ||
val sort: UserRatingsSort = UserRatingsSort.values().first() | ||
) | ||
} |
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
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
57 changes: 0 additions & 57 deletions
57
...ain/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUIViewModel.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.