2023-01-11 18:31:20 +00:00
|
|
|
package com.vitorpamplona.amethyst.ui.navigation
|
|
|
|
|
2023-02-18 18:06:53 +00:00
|
|
|
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
|
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-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,
|
2023-02-18 18:06:53 +00:00
|
|
|
hasNewItems = { acc, cache, ctx -> homeHasNewItems(acc, cache, ctx) },
|
2023-01-27 01:09:56 +00:00
|
|
|
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-01-27 01:09:56 +00:00
|
|
|
)
|
2023-03-07 18:46:44 +00:00
|
|
|
object Notification : Route(
|
|
|
|
"Notification",
|
|
|
|
R.drawable.ic_notifications,
|
2023-02-18 18:06:53 +00:00
|
|
|
hasNewItems = { acc, cache, ctx -> notificationHasNewItems(acc, cache, ctx) },
|
2023-03-07 18:46:44 +00:00
|
|
|
buildScreen = { acc, accSt, nav -> { _ -> NotificationScreen(acc, nav) } }
|
2023-01-27 01:09:56 +00:00
|
|
|
)
|
|
|
|
|
2023-03-07 18:46:44 +00:00
|
|
|
object Message : Route(
|
|
|
|
"Message",
|
|
|
|
R.drawable.ic_dm,
|
2023-02-18 18:06:53 +00:00
|
|
|
hasNewItems = { acc, cache, ctx -> messagesHasNewItems(acc, cache, ctx) },
|
2023-03-07 18:46:44 +00:00
|
|
|
buildScreen = { acc, accSt, nav -> { _ -> ChatroomListScreen(acc, nav) } }
|
2023-01-27 01:09:56 +00:00
|
|
|
)
|
|
|
|
|
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-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(
|
|
|
|
"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,
|
2023-01-24 19:59:21 +00:00
|
|
|
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
|
2023-01-27 01:09:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 18:06:53 +00:00
|
|
|
private fun homeHasNewItems(account: Account, cache: NotificationCache, context: Context): Boolean {
|
|
|
|
val lastTime = cache.load("HomeFollows", context)
|
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-03 16:35:29 +00:00
|
|
|
return (HomeNewThreadFeedFilter.feed().firstOrNull { it.createdAt() != null }?.createdAt() ?: 0) > lastTime
|
2023-01-27 01:09:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 18:06:53 +00:00
|
|
|
private fun notificationHasNewItems(account: Account, cache: NotificationCache, context: Context): Boolean {
|
2023-01-27 01:09:56 +00:00
|
|
|
val lastTime = cache.load("Notification", context)
|
2023-02-18 18:06:53 +00:00
|
|
|
|
|
|
|
NotificationFeedFilter.account = account
|
|
|
|
|
2023-03-03 16:35:29 +00:00
|
|
|
return (NotificationFeedFilter.feed().firstOrNull { it.createdAt() != null }?.createdAt() ?: 0) > lastTime
|
2023-01-27 01:09:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 18:06:53 +00:00
|
|
|
private fun messagesHasNewItems(account: Account, cache: NotificationCache, context: Context): Boolean {
|
|
|
|
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-02-21 21:40:27 +00:00
|
|
|
val lastTime = cache.load("Room/${note.author?.pubkeyHex}", context)
|
|
|
|
|
2023-03-03 16:35:29 +00:00
|
|
|
return (note.createdAt() ?: 0) > lastTime
|
2023-03-07 18:46:44 +00:00
|
|
|
}
|