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

Fix default sort and listing type to read from db #854

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import com.jerboa.datatypes.types.GetPersonMentions
import com.jerboa.datatypes.types.GetPosts
import com.jerboa.datatypes.types.GetPrivateMessages
import com.jerboa.datatypes.types.GetReplies
import com.jerboa.datatypes.types.ListingType
import com.jerboa.datatypes.types.SortType
import com.jerboa.db.AccountRepository
import com.jerboa.db.AccountViewModel
Expand Down Expand Up @@ -261,8 +260,8 @@ class MainActivity : ComponentActivity() {
communityViewModel.getPosts(
GetPosts(
community_name = name,
type_ = ListingType.values()[account?.defaultListingType ?: 1],
sort = SortType.values()[account?.defaultSortType ?: 0],
type_ = homeViewModel.listingType,
sort = homeViewModel.sortType,
auth = account?.jwt,
),
)
Expand Down Expand Up @@ -669,6 +668,7 @@ class MainActivity : ComponentActivity() {
accountViewModel = accountViewModel,
siteViewModel = siteViewModel,
accountSettingsViewModel = accountSettingsViewModel,
homeViewModel = homeViewModel,
)
}
composable(
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,12 @@ fun fetchInitialData(
) {
if (account != null) {
API.changeLemmyInstance(account.instance)
homeViewModel.updateFromAccount(account)
homeViewModel.resetPage()
homeViewModel.getPosts(
GetPosts(
type_ = ListingType.values()[account.defaultListingType],
sort = SortType.values()[account.defaultSortType],
type_ = homeViewModel.listingType,
sort = homeViewModel.sortType,
auth = account.jwt,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.jerboa.datatypes.types.PostResponse
import com.jerboa.datatypes.types.PostView
import com.jerboa.datatypes.types.SavePost
import com.jerboa.datatypes.types.SortType
import com.jerboa.db.Account
import com.jerboa.dedupePosts
import com.jerboa.findAndUpdatePost
import com.jerboa.serializeToMap
Expand Down Expand Up @@ -165,6 +166,11 @@ class HomeViewModel : ViewModel() {
}
}

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

fun updatePost(postView: PostView) {
when (val existing = postsRes) {
is ApiState.Success -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ class LoginViewModel : ViewModel() {
)

homeViewModel.resetPage()
homeViewModel.updateFromAccount(account)
homeViewModel.getPosts(
GetPosts(
type_ = luv.local_user.default_listing_type,
sort = luv.local_user.default_sort_type,
type_ = homeViewModel.listingType,
sort = homeViewModel.sortType,
page = homeViewModel.page,
auth = account.jwt,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fun ImageWithClose(
fun SettingsForm(
accountSettingsViewModel: AccountSettingsViewModel,
siteViewModel: SiteViewModel,
account: Account?,
account: Account,
onClickSave: (form: SaveUserSettings) -> Unit,
padding: PaddingValues,
) {
Expand Down Expand Up @@ -126,7 +126,7 @@ fun SettingsForm(
display_name = displayName,
bio = bio.text,
email = email,
auth = account?.jwt ?: "",
auth = account.jwt,
avatar = avatar,
banner = banner,
matrix_user_id = matrixUserId,
Expand Down Expand Up @@ -191,9 +191,7 @@ fun SettingsForm(
PickImage(onPickedImage = { uri ->
val imageIs = imageInputStreamFromUri(ctx, uri)
scope.launch {
account?.also { acct ->
avatar = uploadPictrsImage(acct, imageIs, ctx).orEmpty()
}
avatar = uploadPictrsImage(account, imageIs, ctx).orEmpty()
}
}, showImage = false)
}
Expand All @@ -208,9 +206,7 @@ fun SettingsForm(
PickImage(onPickedImage = { uri ->
val imageIs = imageInputStreamFromUri(ctx, uri)
scope.launch {
account?.also { acct ->
banner = uploadPictrsImage(acct, imageIs, ctx).orEmpty()
}
banner = uploadPictrsImage(account, imageIs, ctx).orEmpty()
}
}, showImage = false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.jerboa.R
import com.jerboa.db.AccountViewModel
import com.jerboa.ui.components.common.SimpleTopAppBar
import com.jerboa.ui.components.common.getCurrentAccount
import com.jerboa.ui.components.home.HomeViewModel
import com.jerboa.ui.components.home.SiteViewModel

@OptIn(ExperimentalMaterial3Api::class)
Expand All @@ -23,10 +24,11 @@ fun AccountSettingsActivity(
accountSettingsViewModel: AccountSettingsViewModel,
accountViewModel: AccountViewModel,
siteViewModel: SiteViewModel,
homeViewModel: HomeViewModel,
) {
Log.d("jerboa", "Got to settings activity")

val account = getCurrentAccount(accountViewModel = accountViewModel)
val account = getCurrentAccount(accountViewModel = accountViewModel)!!
val snackbarHostState = remember { SnackbarHostState() }

Scaffold(
Expand All @@ -43,6 +45,7 @@ fun AccountSettingsActivity(
form,
siteViewModel = siteViewModel,
account = account,
homeViewModel = homeViewModel,
)
},
siteViewModel = siteViewModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,47 @@ import com.jerboa.datatypes.types.LoginResponse
import com.jerboa.datatypes.types.SaveUserSettings
import com.jerboa.db.Account
import com.jerboa.db.AccountRepository
import com.jerboa.ui.components.home.HomeViewModel
import com.jerboa.ui.components.home.SiteViewModel
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class AccountSettingsViewModel(
private val accountRepository: AccountRepository,
) : ViewModel() {

var saveUserSettingsRes: ApiState<LoginResponse> by mutableStateOf(ApiState.Empty)
private set

fun saveSettings(
form: SaveUserSettings,
siteViewModel: SiteViewModel,
account: Account?,
homeViewModel: HomeViewModel,
account: Account,
) {
viewModelScope.launch {
saveUserSettingsRes = ApiState.Loading
saveUserSettingsRes = apiWrapper(API.getInstance().saveUserSettings(form))

siteViewModel.getSite(
GetSite(
auth = account?.jwt,
),
GetSite(auth = account.jwt),
)

val newAccount = async { maybeUpdateAccountSettings(account, form) }.await()

homeViewModel.updateFromAccount(newAccount)
}
}

// TODO Where is this used??
private suspend fun maybeUpdateAccountSettings(account: Account, form: SaveUserSettings) {
private suspend fun maybeUpdateAccountSettings(account: Account, form: SaveUserSettings): Account {
val newAccount = account.copy(
defaultListingType = form.default_listing_type?.ordinal ?: account.defaultListingType,
defaultSortType = form.default_sort_type?.ordinal ?: account.defaultSortType,
)
if (newAccount != account) {
accountRepository.update(newAccount)
}
return newAccount
}
}
class AccountSettingsViewModelFactory(
Expand Down