Skip to content

Commit

Permalink
Fix login screen appearing before accounts could load
Browse files Browse the repository at this point in the history
  • Loading branch information
wingio committed Nov 26, 2023
1 parent 3db4344 commit fb2351e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package xyz.wingio.dimett.domain.manager

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import xyz.wingio.dimett.domain.db.AppDatabase
import xyz.wingio.dimett.domain.db.entities.Account
import xyz.wingio.dimett.rest.dto.user.CredentialUser
import xyz.wingio.dimett.utils.mainThread

class AccountManager(
db: AppDatabase,
Expand All @@ -15,15 +20,22 @@ class AccountManager(
private val managerScope = CoroutineScope(Dispatchers.IO)
private val dao = db.accountsDao()

var accounts: MutableList<Account> = mutableListOf()
var isInitialized by mutableStateOf(false)
private set

var accounts: MutableList<Account> = mutableStateListOf()
private set

val current: Account?
get() = get(preferenceManager.currentAccount)

init {
managerScope.launch {
accounts = dao.listAccounts().toMutableList()
val accs = dao.listAccounts()
mainThread {
accounts += accs
isInitialized = true
}
}
}

Expand Down
29 changes: 16 additions & 13 deletions app/src/main/java/xyz/wingio/dimett/ui/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat
import androidx.lifecycle.lifecycleScope
import cafe.adriel.voyager.navigator.Navigator
Expand All @@ -22,8 +24,6 @@ import xyz.wingio.dimett.ui.screens.auth.LoginScreen
import xyz.wingio.dimett.ui.screens.main.MainScreen
import xyz.wingio.dimett.ui.theme.DimettTheme
import xyz.wingio.dimett.ui.viewmodels.auth.instanceUrl
import xyz.wingio.dimett.utils.EmojiUtils
import xyz.wingio.dimett.utils.getLogger

class MainActivity : ComponentActivity(), KoinComponent {

Expand All @@ -34,23 +34,25 @@ class MainActivity : ComponentActivity(), KoinComponent {

private lateinit var navigator: Navigator

@OptIn(ExperimentalAnimationApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

WindowCompat.setDecorFitsSystemWindows(window, false)
val isSignedIn = accountManager.current != null
val startScreen = if (isSignedIn) MainScreen() else LoginScreen()

setContent {
val logger = getLogger()

logger.debug(EmojiUtils.regex)

DimettTheme {
Navigator(startScreen) {
SlideTransition(it)
navigator = it
Surface(
modifier = Modifier.fillMaxSize()
) {
if (accountManager.isInitialized) {
val isSignedIn = accountManager.current != null
val startScreen = if (isSignedIn) MainScreen() else LoginScreen()

Navigator(startScreen) {
SlideTransition(it)
navigator = it
}
}
}
}
}
Expand Down Expand Up @@ -90,4 +92,5 @@ class MainActivity : ComponentActivity(), KoinComponent {
}
}
}

}
22 changes: 17 additions & 5 deletions app/src/main/java/xyz/wingio/dimett/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ package xyz.wingio.dimett.utils
import android.content.Context
import android.content.Intent
import android.icu.text.CompactDecimalFormat
import androidx.compose.runtime.Composable
import android.os.Handler
import android.os.Looper
import androidx.compose.ui.text.AnnotatedString
import androidx.core.text.parseAsHtml
import org.koin.androidx.compose.get
import org.koin.core.context.GlobalContext
import org.koin.core.qualifier.named
import xyz.wingio.dimett.BuildConfig
import xyz.wingio.dimett.R
import xyz.wingio.dimett.rest.dto.CustomEmoji
import xyz.wingio.dimett.rest.dto.post.Mention
import xyz.wingio.dimett.rest.dto.post.Post
import java.util.Locale
import kotlin.collections.List
import kotlin.collections.Map
import kotlin.collections.firstOrNull
import kotlin.collections.forEach
import kotlin.collections.mutableMapOf
import kotlin.collections.set

val String.plain: String
get() = replace("<br> ", "<br>&nbsp;")
Expand All @@ -40,8 +47,7 @@ fun List<Mention>.toMentionMap(): Map<String, String> {
return map
}

@Composable
fun getLogger(): Logger = get(named("default"))
fun getLogger(): Logger = GlobalContext.get().get(named("default"))

fun String.toAnnotatedString() = AnnotatedString(this)

Expand All @@ -57,10 +63,16 @@ fun processPostContent(post: Post): String {
fun Context.getResId(emojiCode: String) =
resources.getIdentifier(emojiCode, "raw", BuildConfig.APPLICATION_ID)

fun formatNumber(number: Int): String = CompactDecimalFormat.getInstance(Locale.getDefault(), CompactDecimalFormat.CompactStyle.SHORT).format(number)
fun formatNumber(number: Int): String =
CompactDecimalFormat.getInstance(Locale.getDefault(), CompactDecimalFormat.CompactStyle.SHORT)
.format(number)

fun Context.shareText(string: String) = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, string)
startActivity(Intent.createChooser(this, getString(R.string.action_share)))
}

fun mainThread(block: () -> Unit) {
Handler(Looper.getMainLooper()).post(block)
}

0 comments on commit fb2351e

Please sign in to comment.