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
|
2023-01-27 01:09:56 +00:00
|
|
|
import com.vitorpamplona.amethyst.NotificationCache
|
2023-01-11 18:31:20 +00:00
|
|
|
import com.vitorpamplona.amethyst.R
|
2023-02-18 18:06:53 +00:00
|
|
|
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
|
|
|
) {
|
2023-03-10 16:59:18 +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,
|
2023-03-13 11:03:15 +00:00
|
|
|
arguments = listOf(navArgument("scrollToTop") { type = NavType.BoolType; defaultValue = false }),
|
2023-03-13 17:47:44 +00:00
|
|
|
hasNewItems = { accountViewModel, cache -> homeHasNewItems(accountViewModel, cache) }
|
2023-01-27 01:09:56 +00:00
|
|
|
)
|
2023-03-10 16:59:18 +00:00
|
|
|
|
2023-03-07 18:46:44 +00:00
|
|
|
object Search : Route(
|
2023-03-13 11:03:15 +00:00
|
|
|
route = "Search?scrollToTop={scrollToTop}",
|
|
|
|
icon = R.drawable.ic_globe,
|
|
|
|
arguments = listOf(navArgument("scrollToTop") { type = NavType.BoolType; defaultValue = false })
|
2023-01-27 01:09:56 +00:00
|
|
|
)
|
2023-03-10 16:59:18 +00:00
|
|
|
|
2023-03-07 18:46:44 +00:00
|
|
|
object Notification : Route(
|
2023-03-11 15:13:04 +00:00
|
|
|
route = "Notification",
|
|
|
|
icon = R.drawable.ic_notifications,
|
2023-03-13 17:47:44 +00:00
|
|
|
hasNewItems = { accountViewModel, cache -> notificationHasNewItems(accountViewModel, cache) }
|
2023-01-27 01:09:56 +00:00
|
|
|
)
|
|
|
|
|
2023-03-07 18:46:44 +00:00
|
|
|
object Message : Route(
|
2023-03-11 15:13:04 +00:00
|
|
|
route = "Message",
|
|
|
|
icon = R.drawable.ic_dm,
|
2023-03-13 17:47:44 +00:00
|
|
|
hasNewItems = { accountViewModel, cache -> messagesHasNewItems(accountViewModel, cache) }
|
2023-01-27 01:09:56 +00:00
|
|
|
)
|
|
|
|
|
2023-03-07 18:46:44 +00:00
|
|
|
object Filters : Route(
|
2023-03-11 15:13:04 +00:00
|
|
|
route = "Filters",
|
2023-03-13 12:17:35 +00:00
|
|
|
icon = R.drawable.ic_security
|
2023-01-27 01:09:56 +00:00
|
|
|
)
|
2023-01-16 02:52:59 +00:00
|
|
|
|
2023-03-07 18:46:44 +00:00
|
|
|
object Profile : Route(
|
2023-03-11 15:13:04 +00:00
|
|
|
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(
|
2023-03-11 15:13:04 +00:00
|
|
|
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(
|
2023-03-11 15:13:04 +00:00
|
|
|
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(
|
2023-03-11 15:13:04 +00:00
|
|
|
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
|
2023-03-13 11:03:15 +00:00
|
|
|
fun currentRoute(navController: NavHostController): String? {
|
2023-01-11 18:31:20 +00:00
|
|
|
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
|
|
|
return navBackStackEntry?.destination?.route
|
2023-01-27 01:09:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 17:47:44 +00:00
|
|
|
private fun homeHasNewItems(account: Account, cache: NotificationCache): Boolean {
|
|
|
|
val lastTime = cache.load("HomeFollows")
|
2023-01-27 01:09:56 +00:00
|
|
|
|
2023-02-18 18:06:53 +00:00
|
|
|
HomeNewThreadFeedFilter.account = account
|
2023-01-27 01:09:56 +00:00
|
|
|
|
2023-03-11 15:13:04 +00:00
|
|
|
return (
|
|
|
|
HomeNewThreadFeedFilter.feed().firstOrNull { it.createdAt() != null }?.createdAt()
|
|
|
|
?: 0
|
|
|
|
) > lastTime
|
2023-01-27 01:09:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 17:47:44 +00:00
|
|
|
private fun notificationHasNewItems(account: Account, cache: NotificationCache): Boolean {
|
|
|
|
val lastTime = cache.load("Notification")
|
2023-02-18 18:06:53 +00:00
|
|
|
|
|
|
|
NotificationFeedFilter.account = account
|
|
|
|
|
2023-03-11 15:13:04 +00:00
|
|
|
return (
|
|
|
|
NotificationFeedFilter.feed().firstOrNull { it.createdAt() != null }?.createdAt()
|
|
|
|
?: 0
|
|
|
|
) > lastTime
|
2023-01-27 01:09:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 17:47:44 +00:00
|
|
|
private fun messagesHasNewItems(account: Account, cache: NotificationCache): Boolean {
|
2023-02-18 18:06:53 +00:00
|
|
|
ChatroomListKnownFeedFilter.account = account
|
|
|
|
|
2023-02-21 21:40:27 +00:00
|
|
|
val note = ChatroomListKnownFeedFilter.feed().firstOrNull {
|
2023-03-03 16:35:29 +00:00
|
|
|
it.createdAt() != null && it.channel() == null && it.author != account.userProfile()
|
2023-02-21 21:40:27 +00:00
|
|
|
} ?: return false
|
2023-02-18 18:06:53 +00:00
|
|
|
|
2023-03-13 17:47:44 +00:00
|
|
|
val lastTime = cache.load("Room/${note.author?.pubkeyHex}")
|
2023-02-21 21:40:27 +00:00
|
|
|
|
2023-03-03 16:35:29 +00:00
|
|
|
return (note.createdAt() ?: 0) > lastTime
|
2023-03-07 18:46:44 +00:00
|
|
|
}
|