amethyst/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/Routes.kt

129 wiersze
4.2 KiB
Kotlin
Czysty Zwykły widok Historia

2023-01-11 18:31:20 +00:00
package com.vitorpamplona.amethyst.ui.navigation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
2023-01-12 17:47:31 +00:00
import androidx.navigation.NamedNavArgument
2023-01-11 18:31:20 +00:00
import androidx.navigation.NavHostController
2023-01-12 17:47:31 +00:00
import androidx.navigation.NavType
2023-01-11 18:31:20 +00:00
import androidx.navigation.compose.currentBackStackEntryAsState
2023-01-12 17:47:31 +00:00
import androidx.navigation.navArgument
import com.vitorpamplona.amethyst.NotificationCache
2023-01-11 18:31:20 +00:00
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.ui.dal.ChatroomListKnownFeedFilter
import com.vitorpamplona.amethyst.ui.dal.HomeNewThreadFeedFilter
import com.vitorpamplona.amethyst.ui.dal.NotificationFeedFilter
2023-01-11 18:31:20 +00:00
sealed class Route(
val route: String,
val icon: Int,
2023-03-13 17:47:44 +00:00
val hasNewItems: (Account, NotificationCache) -> Boolean = { _, _ -> false },
2023-03-13 12:17:35 +00:00
val arguments: List<NamedNavArgument> = emptyList()
2023-01-11 18:31:20 +00:00
) {
val base: String
get() = route.substringBefore("?")
2023-03-07 18:46:44 +00:00
object Home : Route(
2023-03-13 12:17:35 +00:00
route = "Home?scrollToTop={scrollToTop}",
icon = R.drawable.ic_home,
arguments = listOf(navArgument("scrollToTop") { type = NavType.BoolType; defaultValue = false }),
2023-03-13 17:47:44 +00:00
hasNewItems = { accountViewModel, cache -> homeHasNewItems(accountViewModel, cache) }
)
2023-03-07 18:46:44 +00:00
object Search : Route(
route = "Search?scrollToTop={scrollToTop}",
icon = R.drawable.ic_globe,
arguments = listOf(navArgument("scrollToTop") { type = NavType.BoolType; defaultValue = false })
)
2023-03-07 18:46:44 +00:00
object Notification : Route(
route = "Notification",
icon = R.drawable.ic_notifications,
2023-03-13 17:47:44 +00:00
hasNewItems = { accountViewModel, cache -> notificationHasNewItems(accountViewModel, cache) }
)
2023-03-07 18:46:44 +00:00
object Message : Route(
route = "Message",
icon = R.drawable.ic_dm,
2023-03-13 17:47:44 +00:00
hasNewItems = { accountViewModel, cache -> messagesHasNewItems(accountViewModel, cache) }
)
2023-03-07 18:46:44 +00:00
object Filters : Route(
route = "Filters",
2023-03-13 12:17:35 +00:00
icon = R.drawable.ic_security
)
2023-01-16 02:52:59 +00:00
2023-03-07 18:46:44 +00:00
object Profile : Route(
route = "User/{id}",
icon = R.drawable.ic_profile,
2023-03-13 12:17:35 +00:00
arguments = listOf(navArgument("id") { type = NavType.StringType })
2023-01-16 02:52:59 +00:00
)
2023-01-12 17:47:31 +00:00
2023-03-07 18:46:44 +00:00
object Note : Route(
route = "Note/{id}",
icon = R.drawable.ic_moments,
2023-03-13 12:17:35 +00:00
arguments = listOf(navArgument("id") { type = NavType.StringType })
2023-01-12 17:47:31 +00:00
)
2023-01-14 22:56:18 +00:00
2023-03-15 21:02:49 +00:00
object Hashtag : Route(
route = "Hashtag/{id}",
icon = R.drawable.ic_moments,
arguments = listOf(navArgument("id") { type = NavType.StringType })
)
2023-03-07 18:46:44 +00:00
object Room : Route(
route = "Room/{id}",
icon = R.drawable.ic_moments,
2023-03-13 12:17:35 +00:00
arguments = listOf(navArgument("id") { type = NavType.StringType })
2023-01-14 22:56:18 +00:00
)
2023-01-16 21:34:49 +00:00
2023-03-07 18:46:44 +00:00
object Channel : Route(
route = "Channel/{id}",
icon = R.drawable.ic_moments,
2023-03-13 12:17:35 +00:00
arguments = listOf(navArgument("id") { type = NavType.StringType })
2023-01-16 21:34:49 +00:00
)
2023-01-11 18:31:20 +00:00
}
2023-03-07 18:46:44 +00:00
// **
// * Functions below only exist because we have not broken the datasource classes into backend and frontend.
// **
2023-01-11 18:31:20 +00:00
@Composable
fun currentRoute(navController: NavHostController): String? {
2023-01-11 18:31:20 +00:00
val navBackStackEntry by navController.currentBackStackEntryAsState()
return navBackStackEntry?.destination?.route
}
2023-03-13 17:47:44 +00:00
private fun homeHasNewItems(account: Account, cache: NotificationCache): Boolean {
val lastTime = cache.load("HomeFollows")
HomeNewThreadFeedFilter.account = account
return (
HomeNewThreadFeedFilter.feed().firstOrNull { it.createdAt() != null }?.createdAt()
?: 0
) > lastTime
}
2023-03-13 17:47:44 +00:00
private fun notificationHasNewItems(account: Account, cache: NotificationCache): Boolean {
val lastTime = cache.load("Notification")
NotificationFeedFilter.account = account
return (
NotificationFeedFilter.feed().firstOrNull { it.createdAt() != null }?.createdAt()
?: 0
) > lastTime
}
2023-03-13 17:47:44 +00:00
private fun messagesHasNewItems(account: Account, cache: NotificationCache): Boolean {
ChatroomListKnownFeedFilter.account = account
val note = ChatroomListKnownFeedFilter.feed().firstOrNull {
it.createdAt() != null && it.channel() == null && it.author != account.userProfile()
} ?: return false
2023-03-13 17:47:44 +00:00
val lastTime = cache.load("Room/${note.author?.pubkeyHex}")
return (note.createdAt() ?: 0) > lastTime
2023-03-07 18:46:44 +00:00
}