-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from crowdproj/wip-32
cards page
- Loading branch information
Showing
19 changed files
with
984 additions
and
129 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
9 changes: 9 additions & 0 deletions
9
android/app/src/main/java/com/github/sszuev/flashcards/android/Card.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,9 @@ | ||
package com.github.sszuev.flashcards.android | ||
|
||
data class Card ( | ||
val cardId: String, | ||
val dictionaryId: String, | ||
val word: String, | ||
val translation: String, | ||
val answered: Int | ||
) |
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
63 changes: 63 additions & 0 deletions
63
android/app/src/main/java/com/github/sszuev/flashcards/android/models/CardViewModel.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,63 @@ | ||
package com.github.sszuev.flashcards.android.models | ||
|
||
import android.util.Log | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.lifecycle.ViewModel | ||
import com.github.sszuev.flashcards.android.Card | ||
import com.github.sszuev.flashcards.android.repositories.CardsRepository | ||
import com.github.sszuev.flashcards.android.toCard | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class CardViewModel( | ||
private val repository: CardsRepository | ||
) : ViewModel() { | ||
|
||
private val tag = "CardViewModel" | ||
|
||
val cads = mutableStateOf<List<Card>>(emptyList()) | ||
val isLoading = mutableStateOf(true) | ||
val errorMessage = mutableStateOf<String?>(null) | ||
|
||
val selectedCardId = mutableStateOf<String?>(null) | ||
|
||
var sortField = mutableStateOf<String?>("name") | ||
var isAscending = mutableStateOf(true) | ||
|
||
suspend fun loadCards(dictionaryId: String) = withContext(Dispatchers.IO) { | ||
Log.d(tag, "load cards for dictionary = $dictionaryId") | ||
isLoading.value = true | ||
errorMessage.value = null | ||
cads.value = emptyList() | ||
try { | ||
cads.value = repository.getAll(dictionaryId).map { it.toCard() } | ||
} catch (e: Exception) { | ||
errorMessage.value = "Failed to load cards: ${e.localizedMessage}" | ||
Log.e(tag, "Failed to load cards", e) | ||
} finally { | ||
isLoading.value = false | ||
} | ||
} | ||
|
||
fun sortBy(field: String) { | ||
if (sortField.value == field) { | ||
isAscending.value = !isAscending.value | ||
} else { | ||
sortField.value = field | ||
isAscending.value = true | ||
} | ||
applySorting() | ||
} | ||
|
||
private fun applySorting() { | ||
cads.value = cads.value.sortedWith { a, b -> | ||
val result = when (sortField.value) { | ||
"word" -> a.word.compareTo(b.word) | ||
"translation" -> a.translation.compareTo(b.translation) | ||
"status" -> a.answered.compareTo(b.answered) | ||
else -> 0 | ||
} | ||
if (isAscending.value) result else -result | ||
} | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
android/app/src/main/java/com/github/sszuev/flashcards/android/repositories/CardResource.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,33 @@ | ||
package com.github.sszuev.flashcards.android.repositories | ||
|
||
import com.github.sszuev.flashcards.android.utils.MapStringAnySerializer | ||
import kotlinx.serialization.Polymorphic | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class CardResource( | ||
val cardId: String, | ||
val dictionaryId: String? = null, | ||
val words: List<CardWordResource>? = null, | ||
val stats: Map<String, Long>? = null, | ||
@Serializable(with = MapStringAnySerializer::class) | ||
val details: Map<String, @Polymorphic Any>? = null, | ||
val answered: Int? = null, | ||
) | ||
|
||
@Serializable | ||
data class CardWordResource( | ||
val word: String? = null, | ||
val transcription: String? = null, | ||
val partOfSpeech: String? = null, | ||
val translations: List<List<String>>? = null, | ||
val examples: List<CardWordExampleResource>? = null, | ||
val sound: String? = null, | ||
val primary: Boolean? = null | ||
) | ||
|
||
@Serializable | ||
data class CardWordExampleResource( | ||
val example: String? = null, | ||
val translation: String? = null | ||
) |
46 changes: 46 additions & 0 deletions
46
...id/app/src/main/java/com/github/sszuev/flashcards/android/repositories/CardsRepository.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,46 @@ | ||
package com.github.sszuev.flashcards.android.repositories | ||
|
||
import android.util.Log | ||
import io.ktor.client.request.setBody | ||
import kotlinx.serialization.Serializable | ||
import java.util.UUID | ||
|
||
class CardsRepository( | ||
private val serverUri: String, | ||
) { | ||
private val tag = "CardsRepository" | ||
|
||
suspend fun getAll(dictionaryId: String): List<CardResource> { | ||
val requestId = UUID.randomUUID().toString() | ||
Log.d(tag, "Get all cards with requestId=$requestId and dictionaryId=$dictionaryId") | ||
val container = authPost<GetAllCardsResponse>("$serverUri/v1/api/cards/get-all") { | ||
setBody( | ||
GetAllCardsRequest( | ||
requestType = "getAllCards", | ||
requestId = requestId, | ||
dictionaryId = dictionaryId, | ||
) | ||
) | ||
} | ||
Log.d( | ||
tag, | ||
"Received response for requestId: $requestId, cards count: ${container.cards.size}" | ||
) | ||
return container.cards | ||
} | ||
|
||
@Serializable | ||
private data class GetAllCardsResponse( | ||
val responseType: String? = null, | ||
val requestId: String? = null, | ||
val cards: List<CardResource> = emptyList() | ||
) | ||
|
||
@Suppress("unused") | ||
@Serializable | ||
data class GetAllCardsRequest( | ||
val requestType: String? = null, | ||
val requestId: String? = null, | ||
val dictionaryId: String? = null | ||
) | ||
} |
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
Oops, something went wrong.