1+ import arrow.core.NonEmptyList
12import arrow.core.getOrElse
23import arrow.core.raise.either
4+ import arrow.core.raise.nullable
5+ import arrow.core.raise.recover
6+ import arrow.core.toNonEmptyListOrNull
37import client.ScryfallApi
8+ import co.touchlab.kermit.Logger
49import com.mfriend.collection.CollectionImporter
510import com.mfriend.db.Card
611import com.mfriend.db.DatabaseHelper
@@ -13,13 +18,16 @@ class CliViewModel(
1318 composeLogger : ComposeLogger ,
1419) {
1520 val logs = composeLogger.logs
21+
1622 // TODO Replace with raise and expose results to UI
1723 suspend fun translateCsv (filePath : String ) {
1824 val succ = either {
1925 val imported = importer.parseCardCastle(filePath).bind()
2026 val cardRows = imported.map { importCard ->
21- api.searchCard(" ${importCard.name} s:${importCard.set} cn:\" ${importCard.number} \" " ).bind()
22- .first() to importCard.count
27+ val results = withErrorString {
28+ api.searchCardRaise(" ${importCard.name} s:${importCard.set} cn:\" ${importCard.number} \" " )
29+ }
30+ results.first() to importCard.count
2331 }.map { (card, count) ->
2432 Card (
2533 - 1 , // TODO Fix insert
@@ -39,22 +47,25 @@ class CliViewModel(
3947 }
4048
4149 suspend fun searchAndAddCards (query : String ) {
42- api.searchCard(query)
43- .map {
44- val selection = it.first()
45- val card = Card (
46- - 1 , // TODO Fix insert
47- selection.name,
48- selection.set,
49- selection.setName,
50- selection.imageUris?.large?.url,
51- selection.scryfallUrl.url,
52- )
53- database.insertCard(card)
54- }.mapLeft { throw Exception (it) }
50+ // TODO make this pure or something
51+ recover({
52+ val results = api.searchCardRaise(query)
53+ val selection = results.first()
54+ val card = Card (
55+ - 1 , // TODO Fix insert
56+ selection.name,
57+ selection.set,
58+ selection.setName,
59+ selection.imageUris?.large?.url,
60+ selection.scryfallUrl.url,
61+ )
62+ database.insertCard(card)
63+ }, { Logger .e(it.toString()) })
5564 }
5665
5766 fun getCards () = database.getCards()
5867
59- suspend fun searchCards (query : String ): List <CardDto >? = api.searchCard(query).getOrElse { null }
68+ suspend fun searchCards (query : String ): NonEmptyList <CardDto >? = nullable {
69+ ignoreErrors { api.searchCardRaise(query).toNonEmptyListOrNull() }
70+ }
6071}
0 commit comments