Skip to content

Commit

Permalink
androidApp: Initial support for announcements
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
  • Loading branch information
theimpulson committed Nov 2, 2024
1 parent c79c36a commit 297db45
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import app.opass.ccip.android.ui.screens.announcement.AnnouncementScreen
import app.opass.ccip.android.ui.screens.event.EventScreen
import app.opass.ccip.android.ui.screens.eventpreview.EventPreviewScreen
import app.opass.ccip.android.ui.screens.schedule.ScheduleScreen
Expand Down Expand Up @@ -56,6 +57,10 @@ fun SetupNavGraph(navHostController: NavHostController, startDestination: Screen
composable<Screen.Ticket> { backStackEntry ->
backStackEntry.toRoute<Screen.Ticket>().TicketScreen(navHostController)
}

composable<Screen.Announcement> { backStackEntry ->
backStackEntry.toRoute<Screen.Announcement>().AnnouncementScreen(navHostController)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ sealed class Screen(@StringRes val title: Int, @DrawableRes val icon: Int) {
title = R.string.ticket,
icon = R.drawable.ic_ticket
)

@Serializable
data class Announcement(val eventId: String, val token: String? = null) : Screen(
title = R.string.announcement,
icon = R.drawable.ic_announcement
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* SPDX-FileCopyrightText: 2024 OPass
* SPDX-License-Identifier: GPL-3.0-only
*/

package app.opass.ccip.android.ui.screens.announcement

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavHostController
import app.opass.ccip.android.R
import app.opass.ccip.android.ui.components.TopAppBar
import app.opass.ccip.android.ui.extensions.browse
import app.opass.ccip.android.ui.navigation.Screen
import app.opass.ccip.network.models.fastpass.Announcement

@Composable
fun Screen.Announcement.AnnouncementScreen(
navHostController: NavHostController,
viewModel: AnnouncementViewModel = hiltViewModel()
) {
val context = LocalContext.current
val announcements by viewModel.announcements.collectAsStateWithLifecycle()

LaunchedEffect(key1 = Unit) {
viewModel.getAnnouncements(this@AnnouncementScreen.eventId, this@AnnouncementScreen.token)
}

Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
TopAppBar(
title = stringResource(this.title),
navHostController = navHostController,
actions = {
IconButton(
onClick = {
viewModel.getAnnouncements(
this@AnnouncementScreen.eventId,
this@AnnouncementScreen.token,
true
)
}
) {
Icon(
painter = painterResource(id = R.drawable.ic_refresh),
contentDescription = null
)
}
}
)
}
) { paddingValues ->
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues),
contentPadding = PaddingValues(10.dp)
) {
items(announcements) { announcement: Announcement ->
AnnouncementItem(announcement) {
context.browse(announcement.url)
}
}
}
}
}

@Composable
fun AnnouncementItem(announcement: Announcement, onClicked: () -> Unit = {}) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp, vertical = 10.dp)
.clickable(enabled = announcement.url.isNotBlank()) { onClicked() }
) {
Text(text = announcement.message, style = MaterialTheme.typography.bodyLarge)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* SPDX-FileCopyrightText: 2024 OPass
* SPDX-License-Identifier: GPL-3.0-only
*/

package app.opass.ccip.android.ui.screens.announcement

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.opass.ccip.helpers.PortalHelper
import app.opass.ccip.network.models.fastpass.Announcement
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AnnouncementViewModel @Inject constructor(
private val portalHelper: PortalHelper
): ViewModel() {

private val _announcements: MutableStateFlow<List<Announcement>> = MutableStateFlow(emptyList())
val announcements = _announcements.asStateFlow()

fun getAnnouncements(eventId: String, token: String? = null, forceReload: Boolean = false) {
viewModelScope.launch {
_announcements.value = portalHelper.getAnnouncements(eventId, token, forceReload)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ fun Screen.Event.EventScreen(
FeatureItem(
label = stringResource(id = R.string.announcement),
iconRes = R.drawable.ic_announcement
)
) {
navHostController.navigate(
Screen.Announcement(this@EventScreen.id, attendee?.token)
)
}
}

FeatureType.FAST_PASS -> {
Expand Down

0 comments on commit 297db45

Please sign in to comment.