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

Refactor sort type handling, and fetching initData/Posts, fixes default sort in community #1166

Merged
merged 8 commits into from
Aug 15, 2023
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
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ dependencies {
// LiveData
implementation("androidx.compose.runtime:runtime-livedata:1.5.0")
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.1")
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.1")

// Images
implementation("io.coil-kt:coil-compose:2.4.0")
Expand Down
13 changes: 1 addition & 12 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import androidx.compose.animation.slideOutHorizontally
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -56,9 +55,7 @@ import com.jerboa.ui.components.common.MarkdownHelper
import com.jerboa.ui.components.common.Route
import com.jerboa.ui.components.common.ShowChangelog
import com.jerboa.ui.components.common.ShowOutdatedServerDialog
import com.jerboa.ui.components.common.SpecialAccount
import com.jerboa.ui.components.common.SwipeToNavigateBack
import com.jerboa.ui.components.common.getSpecialCurrentAccount
import com.jerboa.ui.components.community.CommunityActivity
import com.jerboa.ui.components.community.list.CommunityListActivity
import com.jerboa.ui.components.community.sidebar.CommunitySidebarActivity
Expand All @@ -83,7 +80,7 @@ import com.jerboa.ui.components.settings.lookandfeel.LookAndFeelActivity
import com.jerboa.ui.theme.JerboaTheme

class MainActivity : AppCompatActivity() {
val siteViewModel by viewModels<SiteViewModel>()
val siteViewModel by viewModels<SiteViewModel>(factoryProducer = { SiteViewModel.Factory })
val accountViewModel by viewModels<AccountViewModel>(factoryProducer = { AccountViewModelFactory.Factory })
private val appSettingsViewModel by viewModels<AppSettingsViewModel>(factoryProducer = { AppSettingsViewModelFactory.Factory })
private val accountSettingsViewModel by viewModels<AccountSettingsViewModel>(factoryProducer = { AccountSettingsViewModelFactory.Factory })
Expand All @@ -106,14 +103,6 @@ class MainActivity : AppCompatActivity() {
null
}

val account = getSpecialCurrentAccount(accountViewModel)

LaunchedEffect(account) {
if (account !== SpecialAccount) {
fetchInitialData(account, siteViewModel)
}
}

val appSettings by appSettingsViewModel.appSettings.observeAsState(APP_SETTINGS_DEFAULT)

@Suppress("SENSELESS_COMPARISON")
Expand Down
52 changes: 5 additions & 47 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,9 @@ import com.google.gson.reflect.TypeToken
import com.jerboa.api.API
import com.jerboa.api.API.Companion.checkIfLemmyInstance
import com.jerboa.api.ApiState
import com.jerboa.api.DEFAULT_INSTANCE
import com.jerboa.datatypes.types.*
import com.jerboa.db.APP_SETTINGS_DEFAULT
import com.jerboa.db.entity.Account
import com.jerboa.db.entity.AppSettings
import com.jerboa.db.entity.isAnon
import com.jerboa.model.HomeViewModel
import com.jerboa.model.SiteViewModel
import com.jerboa.ui.components.common.Route
import com.jerboa.ui.components.inbox.InboxTab
import com.jerboa.ui.components.person.UserTab
Expand Down Expand Up @@ -753,48 +748,6 @@ fun siFormat(num: Int): String {
formattedNumber
}
}

fun fetchInitialData(
account: Account,
siteViewModel: SiteViewModel,
) {
if (!account.isAnon()) {
API.changeLemmyInstance(account.instance)
siteViewModel.fetchUnreadCounts(GetUnreadCount(auth = account.jwt))
} else {
API.changeLemmyInstance(DEFAULT_INSTANCE)
}

siteViewModel.getSite(
GetSite(
auth = account.jwt.ifEmpty { null },
),
)
}

fun fetchHomePosts(account: Account, homeViewModel: HomeViewModel) {
if (!account.isAnon()) {
homeViewModel.updateFromAccount(account)
homeViewModel.resetPage()
homeViewModel.getPosts(
GetPosts(
type_ = homeViewModel.listingType,
sort = homeViewModel.sortType,
auth = account.jwt,
),
)
} else {
Log.d("jerboa", "Fetching posts for anonymous user")
homeViewModel.resetPage()
homeViewModel.getPosts(
GetPosts(
type_ = ListingType.Local,
sort = SortType.Active,
),
)
}
}

fun imageInputStreamFromUri(ctx: Context, uri: Uri): InputStream {
return ctx.contentResolver.openInputStream(uri)!!
}
Expand Down Expand Up @@ -1505,6 +1458,11 @@ inline fun <reified T : Enum<T>> Int.toEnum(): T {
return enumValues<T>()[this]
}

inline fun <reified T : Enum<T>> Int.toEnumSafe(): T {
val vals = enumValues<T>()
return if (vals.size >= this) vals[this] else vals[0]
}

fun matchLoginErrorMsgToStringRes(ctx: Context, e: Throwable): String {
return when (e.message) {
"incorrect_login" -> ctx.getString(R.string.login_view_model_incorrect_login)
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/jerboa/db/entity/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ val AnonAccount = Account(
"",
"Anonymous",
"",
0,
1,
0,
verificationState = 0,
)
Expand All @@ -47,3 +47,7 @@ fun Account.isAnon(): Boolean {
fun Account.isReady(): Boolean {
return this.verificationState == AccountVerificationState.CHECKS_COMPLETE.ordinal
}

fun Account.getJWT(): String? {
return if (isAnon()) null else this.jwt
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,12 @@ suspend fun Account.isReadyAndIfNotDisplayInfo(
}

AccountVerificationState.ACCOUNT_DELETED to CheckState.Failed -> {
accountVM.deleteAccountAndSwapCurrent(this, siteVM, swapToAnon = true)
accountVM.deleteAccountAndSwapCurrent(this, swapToAnon = true)
appState.toHome()
}

AccountVerificationState.JWT_VERIFIED to CheckState.Failed -> {
accountVM.deleteAccountAndSwapCurrent(this, siteVM, swapToAnon = true)
accountVM.deleteAccountAndSwapCurrent(this, swapToAnon = true)
appState.toLogin()
}

Expand Down
21 changes: 16 additions & 5 deletions app/src/main/java/com/jerboa/model/AccountSettingsViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jerboa.model

import android.content.Context
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -17,6 +18,7 @@ import com.jerboa.datatypes.types.SaveUserSettings
import com.jerboa.db.entity.Account
import com.jerboa.db.repository.AccountRepository
import com.jerboa.jerboaApplication
import com.jerboa.ui.components.common.apiErrorToast
import kotlinx.coroutines.launch

@Stable
Expand All @@ -30,18 +32,27 @@ class AccountSettingsViewModel(
form: SaveUserSettings,
siteViewModel: SiteViewModel,
account: Account,
ctx: Context,
) {
viewModelScope.launch {
saveUserSettingsRes = ApiState.Loading
saveUserSettingsRes = apiWrapper(API.getInstance().saveUserSettings(form))

siteViewModel.getSite(
GetSite(auth = account.jwt),
)
when (val res = saveUserSettingsRes) {
is ApiState.Success -> {
siteViewModel.getSite(
GetSite(auth = account.jwt),
)

val newAccount = maybeUpdateAccountSettings(account, form)
maybeUpdateAccountSettings(account, form)
}

siteViewModel.updateFromAccount(newAccount)
is ApiState.Failure -> {
apiErrorToast(ctx, res.msg)
}

else -> {}
}
}
}

Expand Down
15 changes: 1 addition & 14 deletions app/src/main/java/com/jerboa/model/AccountViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.jerboa.db.entity.Account
import com.jerboa.db.entity.AnonAccount
import com.jerboa.db.entity.isAnon
import com.jerboa.db.entity.isReady
import com.jerboa.db.repository.AccountRepository
import com.jerboa.feat.AccountVerificationState
import com.jerboa.fetchHomePosts
import com.jerboa.fetchInitialData
import com.jerboa.jerboaApplication
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -54,27 +51,17 @@ class AccountViewModel(private val repository: AccountRepository) : ViewModel()

fun deleteAccountAndSwapCurrent(
account: Account,
siteViewModel: SiteViewModel,
homeViewModel: HomeViewModel? = null,
swapToAnon: Boolean = false,
) = viewModelScope.launch {
if (account.isAnon()) return@launch

repository.delete(account)

val accounts = repository.allAccounts.value
var nextAcc = accounts?.firstOrNull { it.id != account.id }
val nextAcc = accounts?.firstOrNull { it.id != account.id }

if (!swapToAnon && nextAcc != null) {
repository.setCurrent(nextAcc.id)
} else {
nextAcc = AnonAccount
}

fetchInitialData(nextAcc, siteViewModel)

if (homeViewModel != null) {
fetchHomePosts(nextAcc, homeViewModel)
}
}
}
Expand Down
Loading