-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/past conference selection (#1447)
* Store past conference attendance * Add group header for years * Improve layout accessibility * Specify constants --------- Co-authored-by: Ashley Davies <[email protected]>
- Loading branch information
Showing
5 changed files
with
133 additions
and
43 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
55 changes: 39 additions & 16 deletions
55
conferences-app/src/commonMain/kotlin/io/ashdavies/party/past/PastEventsPresenter.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,35 +1,58 @@ | ||
package io.ashdavies.party.past | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.produceState | ||
import io.ashdavies.aggregator.AsgConference | ||
import app.cash.sqldelight.coroutines.asFlow | ||
import app.cash.sqldelight.coroutines.mapToList | ||
import io.ashdavies.aggregator.callable.PastConferencesCallable | ||
import io.ashdavies.party.events.Event | ||
import io.ashdavies.party.events.AttendanceQueries | ||
import kotlinx.collections.immutable.toImmutableList | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.LocalDate | ||
import kotlinx.serialization.json.Json | ||
import okio.ByteString.Companion.encode | ||
|
||
@Composable | ||
internal fun PastEventsPresenter( | ||
pastConferencesCallable: PastConferencesCallable, | ||
attendanceQueries: AttendanceQueries, | ||
ioDispatcher: CoroutineDispatcher, | ||
): PastEventsScreen.State { | ||
val itemList by produceState(emptyList()) { | ||
value = pastConferencesCallable(Unit).map { it.toEvent() } | ||
val attendanceList by attendanceQueries | ||
.selectAll { id, _ -> id } | ||
.asFlow() | ||
.mapToList(ioDispatcher) | ||
.collectAsState(emptyList()) | ||
|
||
val itemList by produceState(emptyList(), attendanceList) { | ||
value = pastConferencesCallable(Unit).map { | ||
val startDate = LocalDate.parse(it.dateStart) | ||
val uuid = Json.encodeToString(it) | ||
.encode() | ||
.md5() | ||
.hex() | ||
|
||
PastEventsScreen.State.Item( | ||
uuid = uuid, | ||
title = "${it.name} ${startDate.year}", | ||
subtitle = it.location, | ||
group = "${startDate.year}", | ||
attended = uuid in attendanceList, | ||
) | ||
} | ||
} | ||
|
||
return PastEventsScreen.State( | ||
itemList = itemList.toImmutableList(), | ||
) | ||
) { event -> | ||
when (event) { | ||
is PastEventsScreen.Event.MarkAttendance -> when (event.value) { | ||
true -> attendanceQueries.insert(event.id, "${Clock.System.now()}") | ||
false -> attendanceQueries.delete(event.id) | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal fun AsgConference.toEvent(): Event = Event( | ||
id = hash(), name = name, website = website, location = location, dateStart = dateStart, | ||
dateEnd = dateEnd, imageUrl = imageUrl, status = status, online = online, | ||
cfpStart = cfp?.start, cfpEnd = cfp?.end, cfpSite = cfp?.site, | ||
) | ||
|
||
private inline fun <reified T : Any> T.hash() = Json | ||
.encodeToString(this) | ||
.encode().md5().hex() |
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
18 changes: 18 additions & 0 deletions
18
conferences-app/src/commonMain/sqldelight/io/ashdavies/party/events/Attendance.sq
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 @@ | ||
CREATE TABLE attendance( | ||
id TEXT NOT NULL UNIQUE PRIMARY KEY, | ||
registeredOn TEXT | ||
); | ||
|
||
selectAll: | ||
SELECT * | ||
FROM attendance | ||
ORDER BY registeredOn; | ||
|
||
insert: | ||
INSERT INTO attendance VALUES (?, ?); | ||
|
||
delete: | ||
DELETE FROM attendance WHERE id = :id; | ||
|
||
deleteAll: | ||
DELETE FROM attendance; |