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-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-10 16:59:18 +00:00
|
|
|
"Home?forceRefresh={forceRefresh}",
|
2023-03-07 18:46:44 +00:00
|
|
|
R.drawable.ic_home,
|
2023-03-10 16:59:18 +00:00
|
|
|
arguments = listOf(navArgument("forceRefresh") { type = NavType.BoolType; defaultValue = false }),
|
2023-03-13 03:35:08 +00:00
|
|
|
hasNewItems = { accountViewModel, cache, context -> homeHasNewItems(accountViewModel, cache, context) },
|
2023-03-11 15:13:04 +00:00
|
|
|
buildScreen = { accountViewModel, _, navController ->
|
2023-03-10 16:59:18 +00:00
|
|
|
{ backStackEntry ->
|
2023-03-13 05:07:01 +00:00
|
|
|
HomeScreen(accountViewModel, navController, backStackEntry.arguments?.getBoolean("forceRefresh", false) ?: false)
|
2023-03-10 16:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
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-10 16:59:18 +00:00
|
|
|
"Search?forceRefresh={forceRefresh}",
|
2023-03-07 18:46:44 +00:00
|
|
|
R.drawable.ic_globe,
|
2023-03-10 16:59:18 +00:00
|
|
|
arguments = listOf(navArgument("forceRefresh") { type = NavType.BoolType; defaultValue = false }),
|
2023-03-13 03:35:08 +00:00
|
|
|
buildScreen = { acc, _, navController ->
|
2023-03-10 16:59:18 +00:00
|
|
|
{ backStackEntry ->
|
2023-03-13 05:07:01 +00:00
|
|
|
SearchScreen(acc, navController, backStackEntry.arguments?.getBoolean("forceRefresh", false) ?: false)
|
2023-03-10 16:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
hasNewItems = { accountViewModel, cache, context ->
|
|
|
|
notificationHasNewItems(accountViewModel, cache, context)
|
|
|
|
},
|
|
|
|
buildScreen = { accountViewModel, _, navController ->
|
|
|
|
{ NotificationScreen(accountViewModel, navController) }
|
|
|
|
}
|
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,
|
|
|
|
hasNewItems = { accountViewModel, cache, context ->
|
|
|
|
messagesHasNewItems(accountViewModel, cache, context)
|
|
|
|
},
|
|
|
|
buildScreen = { accountViewModel, _, navController ->
|
|
|
|
{ ChatroomListScreen(accountViewModel, navController) }
|
|
|
|
}
|
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",
|
|
|
|
icon = R.drawable.ic_security,
|
|
|
|
buildScreen = { accountViewModel, _, navController ->
|
|
|
|
{ FiltersScreen(accountViewModel, navController) }
|
|
|
|
}
|
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-07 18:46:44 +00:00
|
|
|
arguments = listOf(navArgument("id") { type = NavType.StringType }),
|
2023-03-11 15:13:04 +00:00
|
|
|
buildScreen = { accountViewModel, _, navController ->
|
|
|
|
{ ProfileScreen(it.arguments?.getString("id"), accountViewModel, navController) }
|
|
|
|
}
|
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-07 18:46:44 +00:00
|
|
|
arguments = listOf(navArgument("id") { type = NavType.StringType }),
|
2023-03-11 15:13:04 +00:00
|
|
|
buildScreen = { accountViewModel, _, navController ->
|
|
|
|
{ ThreadScreen(it.arguments?.getString("id"), accountViewModel, navController) }
|
|
|
|
}
|
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(
|
2023-03-11 15:13:04 +00:00
|
|
|
route = "Room/{id}",
|
|
|
|
icon = R.drawable.ic_moments,
|
2023-03-07 18:46:44 +00:00
|
|
|
arguments = listOf(navArgument("id") { type = NavType.StringType }),
|
2023-03-11 15:13:04 +00:00
|
|
|
buildScreen = { accountViewModel, _, navController ->
|
|
|
|
{ ChatroomScreen(it.arguments?.getString("id"), accountViewModel, navController) }
|
|
|
|
}
|
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-07 18:46:44 +00:00
|
|
|
arguments = listOf(navArgument("id") { type = NavType.StringType }),
|
2023-03-11 15:13:04 +00:00
|
|
|
buildScreen = { accountViewModel, accountStateViewModel, navController ->
|
|
|
|
{
|
|
|
|
ChannelScreen(
|
|
|
|
it.arguments?.getString("id"),
|
|
|
|
accountViewModel,
|
|
|
|
accountStateViewModel,
|
|
|
|
navController
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
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-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-11 15:13:04 +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-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-11 15:13:04 +00:00
|
|
|
private fun messagesHasNewItems(
|
|
|
|
account: Account,
|
|
|
|
cache: NotificationCache,
|
|
|
|
context: Context
|
|
|
|
): 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-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
|
|
|
}
|