Skip to content

Commit

Permalink
Allow sorting by both dates (added and modified, Linkding only)
Browse files Browse the repository at this point in the history
  • Loading branch information
fibelatti committed Sep 21, 2024
1 parent 26ee097 commit 4d70f14
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ class BookmarksDaoTest : BaseDbTest() {
bookmarksDao.saveBookmarks(list)

// WHEN
val result = bookmarksDao.getAllBookmarks(sortType = 2)
val result = bookmarksDao.getAllBookmarks(sortType = 4)

// THEN
assertThat(result).isEqualTo(
Expand All @@ -606,7 +606,7 @@ class BookmarksDaoTest : BaseDbTest() {
bookmarksDao.saveBookmarks(list)

// WHEN
val result = bookmarksDao.getAllBookmarks(sortType = 0)
val result = bookmarksDao.getAllBookmarks(sortType = 5)

// THEN
assertThat(result).isEqualTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class PostsDaoTest : BaseDbTest() {
postsDao.savePosts(list)

// WHEN
val result = postsDao.getAllPosts(sortType = 2)
val result = postsDao.getAllPosts(sortType = 4)

// THEN
assertThat(result).isEqualTo(
Expand All @@ -574,7 +574,7 @@ class PostsDaoTest : BaseDbTest() {
postsDao.savePosts(list)

// WHEN
val result = postsDao.getAllPosts(sortType = 3)
val result = postsDao.getAllPosts(sortType = 5)

// THEN
assertThat(result).isEqualTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ sealed class SortType(val index: Int)

data object ByDateAddedNewestFirst : SortType(index = 0)
data object ByDateAddedOldestFirst : SortType(index = 1)
data object ByTitleAlphabetical : SortType(index = 2)
data object ByTitleAlphabeticalReverse : SortType(index = 3)
data object ByDateModifiedNewestFirst : SortType(index = 2)
data object ByDateModifiedOldestFirst : SortType(index = 3)
data object ByTitleAlphabetical : SortType(index = 4)
data object ByTitleAlphabeticalReverse : SortType(index = 5)
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ interface BookmarksDao {
private const val ORDER_BY_SUB_QUERY = "order by " +
"case when :sortType = 0 then dateAdded end DESC, " +
"case when :sortType = 1 then dateAdded end ASC," +
"case when :sortType = 2 then title end ASC," +
"case when :sortType = 3 then title end DESC"
"case when :sortType = 2 then dateModified end DESC, " +
"case when :sortType = 3 then dateModified end ASC," +
"case when :sortType = 4 then title end ASC," +
"case when :sortType = 5 then title end DESC"

@JvmStatic
fun preFormatTerm(term: String): String = term
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ interface PostsDao {
private const val ORDER_BY_SUB_QUERY = "order by " +
"case when :sortType = 0 then time end DESC, " +
"case when :sortType = 1 then time end ASC," +
"case when :sortType = 2 then description end ASC," +
"case when :sortType = 3 then description end DESC"
"case when :sortType = 4 then description end ASC," +
"case when :sortType = 5 then description end DESC"

@JvmStatic
fun preFormatTerm(term: String): String = term
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import com.fibelatti.pinboard.core.extension.showBanner
import com.fibelatti.pinboard.features.MainViewModel
import com.fibelatti.pinboard.features.appstate.All
import com.fibelatti.pinboard.features.appstate.AppStateViewModel
import com.fibelatti.pinboard.features.appstate.ByDateModifiedNewestFirst
import com.fibelatti.pinboard.features.appstate.ByDateModifiedOldestFirst
import com.fibelatti.pinboard.features.appstate.ClearSearch
import com.fibelatti.pinboard.features.appstate.GetNextPostPage
import com.fibelatti.pinboard.features.appstate.Loaded
Expand All @@ -71,6 +73,7 @@ import com.fibelatti.pinboard.features.appstate.ShouldForceLoad
import com.fibelatti.pinboard.features.appstate.ShouldLoadFirstPage
import com.fibelatti.pinboard.features.appstate.ShouldLoadNextPage
import com.fibelatti.pinboard.features.appstate.SidePanelContent
import com.fibelatti.pinboard.features.appstate.SortType
import com.fibelatti.pinboard.features.appstate.ViewCategory
import com.fibelatti.pinboard.features.appstate.ViewPost
import com.fibelatti.pinboard.features.appstate.ViewSearch
Expand Down Expand Up @@ -136,6 +139,7 @@ fun BookmarkListScreen(
isLoading = (postListLoading || postDetailScreenState.isLoading) && !hasError,
onScrollDirectionChanged = mainViewModel::setCurrentScrollDirection,
onNextPageRequested = { appStateViewModel.runAction(GetNextPostPage) },
sortType = currentState.sortType,
searchParameters = currentState.searchParameters,
onActiveSearchClicked = { appStateViewModel.runAction(ViewSearch) },
onClearClicked = { appStateViewModel.runAction(ClearSearch) },
Expand Down Expand Up @@ -167,12 +171,13 @@ fun BookmarkListScreen(
isLoading: Boolean,
onScrollDirectionChanged: (ScrollDirection) -> Unit,
onNextPageRequested: () -> Unit,
sortType: SortType,
searchParameters: SearchParameters,
onActiveSearchClicked: () -> Unit,
onClearClicked: () -> Unit,
onSaveClicked: () -> Unit,
onShareClicked: (SearchParameters) -> Unit,
onPullToRefresh: () -> Unit = {},
onPullToRefresh: () -> Unit,
onPostClicked: (Post) -> Unit,
onPostLongClicked: (Post) -> Unit,
onTagClicked: (Tag) -> Unit,
Expand Down Expand Up @@ -264,6 +269,7 @@ fun BookmarkListScreen(
BookmarkItem(
appMode = appMode,
post = post,
sortType = sortType,
onPostClicked = onPostClicked,
onPostLongClicked = onPostLongClicked,
showDescription = showPostDescription,
Expand Down Expand Up @@ -355,6 +361,7 @@ private fun ActiveSearch(
private fun BookmarkItem(
appMode: AppMode,
post: Post,
sortType: SortType,
onPostClicked: (Post) -> Unit,
onPostLongClicked: (Post) -> Unit,
showDescription: Boolean,
Expand Down Expand Up @@ -436,7 +443,11 @@ private fun BookmarkItem(
}

BookmarkFlags(
time = post.displayDateAdded,
time = if (sortType == ByDateModifiedNewestFirst || sortType == ByDateModifiedOldestFirst) {
post.displayDateModified
} else {
post.displayDateAdded
},
private = post.private,
readLater = post.readLater,
)
Expand Down Expand Up @@ -551,6 +562,7 @@ private fun BookmarkListScreenPreview(
isLoading = true,
onScrollDirectionChanged = {},
onNextPageRequested = {},
sortType = ByDateModifiedNewestFirst,
searchParameters = SearchParameters(term = "bookmark"),
onActiveSearchClicked = {},
onClearClicked = {},
Expand Down Expand Up @@ -595,6 +607,7 @@ private fun BookmarkItemPreview(
BookmarkItem(
appMode = AppMode.PINBOARD,
post = post,
sortType = ByDateModifiedNewestFirst,
onPostClicked = {},
onPostLongClicked = {},
showDescription = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import com.fibelatti.core.functional.Success
import com.fibelatti.pinboard.R
import com.fibelatti.pinboard.core.AppConfig
import com.fibelatti.pinboard.core.AppConfig.PINBOARD_USER_URL
import com.fibelatti.pinboard.core.AppMode
import com.fibelatti.pinboard.core.AppModeProvider
import com.fibelatti.pinboard.core.android.SelectionDialog
import com.fibelatti.pinboard.core.android.base.BaseFragment
import com.fibelatti.pinboard.core.extension.applySecureFlag
Expand All @@ -28,6 +30,8 @@ import com.fibelatti.pinboard.features.appstate.All
import com.fibelatti.pinboard.features.appstate.AppStateViewModel
import com.fibelatti.pinboard.features.appstate.ByDateAddedNewestFirst
import com.fibelatti.pinboard.features.appstate.ByDateAddedOldestFirst
import com.fibelatti.pinboard.features.appstate.ByDateModifiedNewestFirst
import com.fibelatti.pinboard.features.appstate.ByDateModifiedOldestFirst
import com.fibelatti.pinboard.features.appstate.ByTitleAlphabetical
import com.fibelatti.pinboard.features.appstate.ByTitleAlphabeticalReverse
import com.fibelatti.pinboard.features.appstate.EditPost
Expand Down Expand Up @@ -58,6 +62,7 @@ import kotlinx.coroutines.flow.onEach
@AndroidEntryPoint
class PostListFragment @Inject constructor(
private val userRepository: UserRepository,
private val appModeProvider: AppModeProvider,
) : BaseFragment() {

private val appStateViewModel: AppStateViewModel by activityViewModels()
Expand Down Expand Up @@ -298,6 +303,8 @@ class PostListFragment @Inject constructor(
when (sortType) {
is ByDateAddedNewestFirst -> R.string.posts_sorting_newest_first
is ByDateAddedOldestFirst -> R.string.posts_sorting_oldest_first
is ByDateModifiedNewestFirst -> R.string.posts_sorting_newest_first
is ByDateModifiedOldestFirst -> R.string.posts_sorting_oldest_first
is ByTitleAlphabetical -> R.string.posts_sorting_alphabetical
is ByTitleAlphabeticalReverse -> R.string.posts_sorting_alphabetical_reverse
},
Expand All @@ -309,16 +316,24 @@ class PostListFragment @Inject constructor(
SelectionDialog.show(
context = requireContext(),
title = getString(R.string.menu_main_sorting),
options = listOf(
ByDateAddedNewestFirst,
ByDateAddedOldestFirst,
ByTitleAlphabetical,
ByTitleAlphabeticalReverse,
),
options = buildList {
add(ByDateAddedNewestFirst)
add(ByDateAddedOldestFirst)

if (AppMode.LINKDING == appModeProvider.appMode.value) {
add(ByDateModifiedNewestFirst)
add(ByDateModifiedOldestFirst)
}

add(ByTitleAlphabetical)
add(ByTitleAlphabeticalReverse)
},
optionName = { option ->
when (option) {
is ByDateAddedNewestFirst -> getString(R.string.sorting_by_date_added_newest_first)
is ByDateAddedOldestFirst -> getString(R.string.sorting_by_date_added_oldest_first)
is ByDateModifiedNewestFirst -> getString(R.string.sorting_by_date_modified_newest_first)
is ByDateModifiedOldestFirst -> getString(R.string.sorting_by_date_modified_oldest_first)
is ByTitleAlphabetical -> getString(R.string.sorting_by_title_alphabetical)
is ByTitleAlphabeticalReverse -> getString(R.string.sorting_by_title_alphabetical_reverse)
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@
<!-- Sorting -->
<string name="sorting_by_date_added_newest_first">By date added (Newest first)</string>
<string name="sorting_by_date_added_oldest_first">By date added (Oldest first)</string>
<string name="sorting_by_date_modified_newest_first">By date modified (Newest first)</string>
<string name="sorting_by_date_modified_oldest_first">By date modified (Oldest first)</string>
<string name="sorting_by_title_alphabetical">By title (A to Z)</string>
<string name="sorting_by_title_alphabetical_reverse">By title (Z to A)</string>

Expand Down

0 comments on commit 4d70f14

Please sign in to comment.