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

149 wiersze
5.6 KiB
Kotlin
Czysty Zwykły widok Historia

2023-01-11 18:31:20 +00:00
package com.vitorpamplona.amethyst.ui.navigation
import android.content.Context
2023-01-11 18:31:20 +00:00
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.NavBackStackEntry
2023-01-12 17:47:31 +00:00
import androidx.navigation.NavController
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-19 03:00:32 +00:00
import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel
2023-02-27 16:28:54 +00:00
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
2023-02-27 15:52:39 +00:00
import com.vitorpamplona.amethyst.ui.screen.loggedIn.ChannelScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.ChatroomListScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.ChatroomScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.FiltersScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.HomeScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.NotificationScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.ProfileScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.SearchScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.ThreadScreen
2023-01-11 18:31:20 +00:00
sealed class Route(
val route: String,
val icon: Int,
2023-03-07 18:46:44 +00:00
val hasNewItems: (Account, NotificationCache, Context) -> Boolean = { _, _, _ -> false },
2023-01-12 17:47:31 +00:00
val arguments: List<NamedNavArgument> = emptyList(),
2023-01-19 03:00:32 +00:00
val buildScreen: (AccountViewModel, AccountStateViewModel, NavController) -> @Composable (NavBackStackEntry) -> Unit
2023-01-11 18:31:20 +00:00
) {
2023-03-07 18:46:44 +00:00
object Home : Route(
"Home",
R.drawable.ic_home,
hasNewItems = { acc, cache, ctx -> homeHasNewItems(acc, cache, ctx) },
buildScreen = { acc, accSt, nav -> { _ -> HomeScreen(acc, nav) } }
)
2023-03-07 18:46:44 +00:00
object Search : Route(
"Search",
R.drawable.ic_globe,
buildScreen = { acc, accSt, nav -> { _ -> SearchScreen(acc, nav) } }
)
2023-03-07 18:46:44 +00:00
object Notification : Route(
"Notification",
R.drawable.ic_notifications,
hasNewItems = { acc, cache, ctx -> notificationHasNewItems(acc, cache, ctx) },
2023-03-07 18:46:44 +00:00
buildScreen = { acc, accSt, nav -> { _ -> NotificationScreen(acc, nav) } }
)
2023-03-07 18:46:44 +00:00
object Message : Route(
"Message",
R.drawable.ic_dm,
hasNewItems = { acc, cache, ctx -> messagesHasNewItems(acc, cache, ctx) },
2023-03-07 18:46:44 +00:00
buildScreen = { acc, accSt, nav -> { _ -> ChatroomListScreen(acc, nav) } }
)
2023-03-07 18:46:44 +00:00
object Filters : Route(
"Filters",
R.drawable.ic_security,
buildScreen = { acc, accSt, nav -> { _ -> FiltersScreen(acc, nav) } }
)
2023-01-16 02:52:59 +00:00
2023-03-07 18:46:44 +00:00
object Profile : Route(
"User/{id}",
R.drawable.ic_profile,
arguments = listOf(navArgument("id") { type = NavType.StringType }),
buildScreen = { acc, accSt, nav -> { ProfileScreen(it.arguments?.getString("id"), acc, nav) } }
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(
"Note/{id}",
R.drawable.ic_moments,
arguments = listOf(navArgument("id") { type = NavType.StringType }),
buildScreen = { acc, accSt, nav -> { ThreadScreen(it.arguments?.getString("id"), acc, nav) } }
2023-01-12 17:47:31 +00:00
)
2023-01-14 22:56:18 +00:00
2023-03-07 18:46:44 +00:00
object Room : Route(
"Room/{id}",
R.drawable.ic_moments,
arguments = listOf(navArgument("id") { type = NavType.StringType }),
buildScreen = { acc, accSt, nav -> { ChatroomScreen(it.arguments?.getString("id"), acc, nav) } }
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(
"Channel/{id}",
R.drawable.ic_moments,
arguments = listOf(navArgument("id") { type = NavType.StringType }),
buildScreen = { acc, accSt, nav -> { ChannelScreen(it.arguments?.getString("id"), acc, accSt, nav) } }
2023-01-16 21:34:49 +00:00
)
2023-01-11 18:31:20 +00:00
}
val Routes = listOf(
// bottom
Route.Home,
Route.Message,
Route.Search,
Route.Notification,
2023-03-07 18:46:44 +00:00
// drawer
2023-01-11 18:31:20 +00:00
Route.Profile,
2023-01-14 22:56:18 +00:00
Route.Note,
2023-01-16 21:34:49 +00:00
Route.Room,
Route.Channel,
Route.Filters
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
public fun currentRoute(navController: NavHostController): String? {
val navBackStackEntry by navController.currentBackStackEntryAsState()
return navBackStackEntry?.destination?.route
}
private fun homeHasNewItems(account: Account, cache: NotificationCache, context: Context): Boolean {
val lastTime = cache.load("HomeFollows", context)
HomeNewThreadFeedFilter.account = account
return (HomeNewThreadFeedFilter.feed().firstOrNull { it.createdAt() != null }?.createdAt() ?: 0) > lastTime
}
private fun notificationHasNewItems(account: Account, cache: NotificationCache, context: Context): Boolean {
val lastTime = cache.load("Notification", context)
NotificationFeedFilter.account = account
return (NotificationFeedFilter.feed().firstOrNull { it.createdAt() != null }?.createdAt() ?: 0) > lastTime
}
private fun messagesHasNewItems(account: Account, cache: NotificationCache, context: Context): Boolean {
ChatroomListKnownFeedFilter.account = account
val note = ChatroomListKnownFeedFilter.feed().firstOrNull {
it.createdAt() != null && it.channel() == null && it.author != account.userProfile()
} ?: return false
val lastTime = cache.load("Room/${note.author?.pubkeyHex}", context)
return (note.createdAt() ?: 0) > lastTime
2023-03-07 18:46:44 +00:00
}