Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add swiping between posts in PostActivity #872

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
0aa46c1
Add swiping between posts in PostActivity
Jun 24, 2023
97916b1
Merge branch 'dessalines:main' into main
kensand Jun 24, 2023
d62fff9
Fix formatting
Jun 24, 2023
e30c000
Merge branch 'main' into main
kensand Jun 25, 2023
074df46
Refactor SwipeBetweenPosts, add setting
Jun 25, 2023
c5afa8f
Fix formatting
Jun 25, 2023
ca6aea8
Merge branch 'main' into main
twizmwazin Jun 25, 2023
46a0875
Merge branch 'dessalines:main' into main
kensand Jun 25, 2023
c3bd3dd
Localize strings and cleanup styling in SwipeBetweenPosts
Jun 25, 2023
8fc900e
Rename setting to 'Allow swipe between posts'
Jun 25, 2023
4c016d7
Fix formatting
Jun 25, 2023
23f6adf
Revert extraneous formatting changes to HomeViewModel.kt
Jun 25, 2023
dc6abcf
Merge branch 'main' into main
kensand Jun 25, 2023
68e9584
Merge branch 'main' into main
kensand Jun 26, 2023
84d0121
Merge branch 'main' into main
kensand Jun 26, 2023
a5ebaad
Merge branch 'main' into main
kensand Jun 27, 2023
2f5f5ce
Fix typos from merge
kensand Jun 27, 2023
0362eec
Fix issues introduced after merging
Jun 27, 2023
6dc3bcf
Fix formatting
Jun 27, 2023
9c01da6
Merge remote-tracking branch 'upstream/main'
Jun 28, 2023
0b01fba
Merge changes from upstream
Jun 28, 2023
0e3db7a
Merge branch 'main' into main
kensand Jun 28, 2023
bcce67e
Merge branch 'main' into main
kensand Jun 30, 2023
fcc4fff
Use navigation
Jun 30, 2023
d92252d
Merge branch 'main' into main
kensand Jul 1, 2023
a01eaff
Rename Allow swiping between posts
Jul 1, 2023
484504c
Merge branch 'main' into main
kensand Jul 1, 2023
f7e6c52
Merge branch 'main' into main
kensand Jul 2, 2023
87f0409
Merge branch 'main' of github.com:dessalines/jerboa
Jul 9, 2023
1843ca8
Fix formatting
Jul 9, 2023
b6495ce
Merge branch 'main' of github.com:dessalines/jerboa
Jul 10, 2023
14a3919
Merge remote-tracking branch 'upstream/main'
Sep 17, 2023
4588367
Merge branch 'upstream-main'
Sep 17, 2023
5474990
Appease linter
Sep 17, 2023
860ec9c
Revert outdated changes against upstream
Sep 18, 2023
91b8098
Revert Schema 18
Sep 18, 2023
79776c0
Use POST_BUFFER_COUNT in HomeViewModel and add documentation
Sep 18, 2023
5a29245
Remove fetchingMore mutableState in CommunityViewModel
Sep 18, 2023
d02cebc
Revert formatting
Sep 18, 2023
15f383e
Use isLoading helper function underneath isFetchingMore
Sep 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ dependencies {
implementation("net.engawapg.lib:zoomable:1.4.3")
implementation("androidx.browser:browser:1.5.0")

implementation("me.saket.swipe:swipe:1.2.0")

implementation("androidx.profileinstaller:profileinstaller:1.3.1")
baselineProfile(project(":benchmarks"))
}
10 changes: 8 additions & 2 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ import com.jerboa.ui.components.comment.edit.CommentEditViewModel
import com.jerboa.ui.components.comment.reply.CommentReplyActivity
import com.jerboa.ui.components.comment.reply.CommentReplyViewModel
import com.jerboa.ui.components.common.MarkdownHelper
import com.jerboa.ui.components.common.PostSwipeWrapper
import com.jerboa.ui.components.common.ShowChangelog
import com.jerboa.ui.components.common.ShowOutdatedServerDialog
import com.jerboa.ui.components.common.SwipeToNavigateBack
import com.jerboa.ui.components.common.getCurrentAccount
import com.jerboa.ui.components.common.getCurrentAccountSync
import com.jerboa.ui.components.community.CommunityActivity
Expand Down Expand Up @@ -501,7 +501,13 @@ class MainActivity : ComponentActivity() {
postViewModel.initialize(id = Either.Left(postId))
postViewModel.getData(account)
}
SwipeToNavigateBack(navController = navController) {
PostSwipeWrapper(
navController = navController,
account = account,
homeViewModel = homeViewModel,
postViewModel = postViewModel,
appSettingsViewModel = appSettingsViewModel,
) {
PostActivity(
postViewModel = postViewModel,
accountViewModel = accountViewModel,
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/java/com/jerboa/db/AppDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ data class AppSettings(
defaultValue = "0",
)
val secureWindow: Boolean,
@ColumnInfo(
name = "swipe_between_posts",
defaultValue = "0",
)
val swipeBetweenPosts: Boolean,
)

@Dao
Expand Down Expand Up @@ -436,8 +441,17 @@ val MIGRATION_15_16 = object : Migration(15, 16) {
}
}

val MIGRATION_16_17 = object : Migration(16, 17) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(UPDATE_APP_CHANGELOG_UNVIEWED)
database.execSQL(
"ALTER TABLE AppSettings add column swipe_between_posts INTEGER NOT NULL default 0",
)
}
}

@Database(
version = 16,
version = 17,
entities = [Account::class, AppSettings::class],
exportSchema = true,
)
Expand Down Expand Up @@ -477,6 +491,7 @@ abstract class AppDB : RoomDatabase() {
MIGRATION_13_14,
MIGRATION_14_15,
MIGRATION_15_16,
MIGRATION_16_17,
)
// Necessary because it can't insert data on creation
.addCallback(object : Callback() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.jerboa.ui.components.common

import androidx.compose.runtime.Composable
import androidx.navigation.NavController
import com.jerboa.db.Account
import com.jerboa.db.AppSettingsViewModel
import com.jerboa.ui.components.home.HomeViewModel
import com.jerboa.ui.components.post.PostViewModel

@Composable
fun PostSwipeWrapper(
navController: NavController,
homeViewModel: HomeViewModel,
postViewModel: PostViewModel,
appSettingsViewModel: AppSettingsViewModel,
account: Account?,
content: @Composable () -> Unit,
) {
if (appSettingsViewModel.appSettings.value?.swipeBetweenPosts == true) {
SwipeBetweenPosts(
homeViewModel = homeViewModel,
postViewModel = postViewModel,
account = account,
content = content,
)
} else {
SwipeToNavigateBack(navController = navController, content = content)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.jerboa.ui.components.common

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import arrow.core.left
import com.jerboa.api.ApiState
import com.jerboa.datatypes.types.GetPosts
import com.jerboa.db.Account
import com.jerboa.ui.components.home.HomeViewModel
import com.jerboa.ui.components.post.PostViewModel
import me.saket.swipe.SwipeAction
import me.saket.swipe.SwipeableActionsBox

@Composable
fun SwipeBetweenPosts(
homeViewModel: HomeViewModel,
postViewModel: PostViewModel,
account: Account?,
content: @Composable () -> Unit,
) {
val forward = SwipeAction(
icon = { Text("Next") },
kensand marked this conversation as resolved.
Show resolved Hide resolved
onSwipe = {
val res = homeViewModel.postsRes
if (res is ApiState.Success) {
res.data.posts
.mapIndexed { index, postView -> index to postView }
.firstOrNull { it.second.post.id == postViewModel.id?.swap()?.getOrNull() }
?.first?.let { currIndex ->
if (currIndex + 1 >= res.data.posts.size - 1) {
val nextIndex = res.data.posts.size
homeViewModel.nextPage()
homeViewModel.appendPosts(
GetPosts(
page = homeViewModel.page,
sort = homeViewModel.sortType,
type_ = homeViewModel.listingType,
auth = account?.jwt,
),
).invokeOnCompletion {
val newRes = homeViewModel.postsRes
if (newRes is ApiState.Success && newRes.data.posts.size > nextIndex) {
postViewModel.initialize(newRes.data.posts[nextIndex].post.id.left())
postViewModel.getData(account)
}
}
} else if (currIndex >= 0) {
postViewModel.initialize(res.data.posts[currIndex + 1].post.id.left())
postViewModel.getData(account)
}
Unit
}
}
},
background = Color.Transparent,
kensand marked this conversation as resolved.
Show resolved Hide resolved
)

val backward = SwipeAction(
icon = { Text("Next") },
kensand marked this conversation as resolved.
Show resolved Hide resolved
onSwipe = {
val res = homeViewModel.postsRes
if (res is ApiState.Success) {
val currIndex = res.data.posts
.mapIndexed { index, postView -> index to postView }
.firstOrNull { it.second.post.id == postViewModel.id?.swap()?.getOrNull() }
?.first
if (currIndex != null && currIndex > 0 && currIndex < res.data.posts.size) {
val nextId = res.data.posts[currIndex - 1].post.id
postViewModel.initialize(nextId.left())
postViewModel.getData(account)
}
}
},
background = Color.Transparent,
)

SwipeableActionsBox(
startActions = listOf(backward),
endActions = listOf(forward),
) {
content()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class HomeViewModel : ViewModel() {
}
}

fun appendPosts(form: GetPosts) {
fun appendPosts(form: GetPosts) =
kensand marked this conversation as resolved.
Show resolved Hide resolved
viewModelScope.launch {
fetchingMore = true
val more = apiWrapper(API.getInstance().getPosts(form.serializeToMap()))
Expand All @@ -104,7 +104,6 @@ class HomeViewModel : ViewModel() {
else -> {}
}
}
}

fun likePost(form: CreatePostLike) {
viewModelScope.launch {
Expand Down Expand Up @@ -168,7 +167,9 @@ class HomeViewModel : ViewModel() {

fun updateFromAccount(account: Account) {
updateSortType(SortType.values().getOrElse(account.defaultSortType) { sortType })
updateListingType(ListingType.values().getOrElse(account.defaultListingType) { listingType })
updateListingType(
ListingType.values().getOrElse(account.defaultListingType) { listingType },
)
}

fun updatePost(postView: PostView) {
Expand All @@ -178,6 +179,7 @@ class HomeViewModel : ViewModel() {
val newRes = ApiState.Success(existing.data.copy(posts = newPosts))
postsRes = newRes
}

else -> {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ fun LookAndFeelActivity(
val useCustomTabsState = rememberBooleanSettingState(settings?.useCustomTabs ?: true)
val usePrivateTabsState = rememberBooleanSettingState(settings?.usePrivateTabs ?: false)

val swipeBetweenPosts = rememberBooleanSettingState(settings?.swipeBetweenPosts ?: false)

val secureWindowState = rememberBooleanSettingState(settings?.secureWindow ?: false)

val snackbarHostState = remember { SnackbarHostState() }
Expand All @@ -92,6 +94,7 @@ fun LookAndFeelActivity(
useCustomTabs = useCustomTabsState.value,
usePrivateTabs = usePrivateTabsState.value,
secureWindow = secureWindowState.value,
swipeBetweenPosts = swipeBetweenPosts.value,
),
)
}
Expand Down Expand Up @@ -241,6 +244,13 @@ fun LookAndFeelActivity(
},
onCheckedChange = { updateAppSettings() },
)
SettingsCheckbox(
state = swipeBetweenPosts,
title = {
Text(text = stringResource(id = R.string.look_and_feel_swipe_between_posts))
},
onCheckedChange = { updateAppSettings() },
)
}
},
)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
<string name="look_and_feel_theme_color_pink">Pink</string>
<string name="look_and_feel_theme_color_purple">Purple</string>
<string name="look_and_feel_theme_color_woodland">Woodland</string>
<string name="look_and_feel_swipe_between_posts">Swipe between posts</string>
kensand marked this conversation as resolved.
Show resolved Hide resolved
<string name="look_and_feel_use_custom_tabs">Use custom tabs</string>
<string name="look_and_feel_use_private_tabs">Use private custom tabs if available</string>
<string name="markRead">Mark as read</string>
Expand Down