-
Notifications
You must be signed in to change notification settings - Fork 169
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
db refactor #973
db refactor #973
Changes from all commits
9299528
52b5cf9
71fda4f
2268b6f
4127be2
8db1842
5c0b689
d0c7f3c
2ad2dbc
bf13d05
076491d
7bac450
010eefd
5f3ab5a
d5d7c6b
5970f59
df94188
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ import androidx.compose.runtime.livedata.observeAsState | |
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewmodel.CreationExtras | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
|
@@ -36,15 +38,13 @@ import com.jerboa.api.API | |
import com.jerboa.api.ApiState | ||
import com.jerboa.api.MINIMUM_API_VERSION | ||
import com.jerboa.db.APP_SETTINGS_DEFAULT | ||
import com.jerboa.db.AccountRepository | ||
import com.jerboa.db.AccountViewModel | ||
import com.jerboa.db.AccountViewModelFactory | ||
import com.jerboa.db.AppDB | ||
import com.jerboa.db.AppSettingsRepository | ||
import com.jerboa.db.AppSettingsViewModel | ||
import com.jerboa.db.AppSettingsViewModelFactory | ||
import com.jerboa.db.AppDBContainer | ||
import com.jerboa.model.AccountSettingsViewModel | ||
import com.jerboa.model.AccountSettingsViewModelFactory | ||
import com.jerboa.model.AccountViewModel | ||
import com.jerboa.model.AccountViewModelFactory | ||
import com.jerboa.model.AppSettingsViewModel | ||
import com.jerboa.model.AppSettingsViewModelFactory | ||
import com.jerboa.model.CommunityViewModel | ||
import com.jerboa.model.ReplyItem | ||
import com.jerboa.model.SiteViewModel | ||
|
@@ -58,7 +58,7 @@ 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.SwipeToNavigateBack | ||
import com.jerboa.ui.components.common.getCurrentAccountSync | ||
import com.jerboa.ui.components.common.getCurrentAccount | ||
import com.jerboa.ui.components.common.takeDepsFromRoot | ||
import com.jerboa.ui.components.common.toView | ||
import com.jerboa.ui.components.community.CommunityActivity | ||
|
@@ -88,30 +88,28 @@ import com.jerboa.util.BackConfirmationMode | |
import com.jerboa.util.ShowConfirmationDialog | ||
|
||
class JerboaApplication : Application() { | ||
private val database by lazy { AppDB.getDatabase(this) } | ||
val accountRepository by lazy { AccountRepository(database.accountDao()) } | ||
val appSettingsRepository by lazy { AppSettingsRepository(database.appSettingsDao()) } | ||
lateinit var container: AppDBContainer | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
container = AppDBContainer(this) | ||
} | ||
} | ||
|
||
fun CreationExtras.jerboaApplication(): JerboaApplication = | ||
(this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as JerboaApplication) | ||
|
||
class MainActivity : AppCompatActivity() { | ||
private val siteViewModel by viewModels<SiteViewModel>() | ||
private val accountSettingsViewModel by viewModels<AccountSettingsViewModel> { | ||
AccountSettingsViewModelFactory((application as JerboaApplication).accountRepository) | ||
} | ||
private val accountViewModel: AccountViewModel by viewModels { | ||
AccountViewModelFactory((application as JerboaApplication).accountRepository) | ||
} | ||
private val appSettingsViewModel: AppSettingsViewModel by viewModels { | ||
AppSettingsViewModelFactory((application as JerboaApplication).appSettingsRepository) | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val accountSync = getCurrentAccountSync(accountViewModel) | ||
|
||
setContent { | ||
val ctx = LocalContext.current | ||
val accountViewModel: AccountViewModel = viewModel(factory = AccountViewModelFactory.Factory) | ||
val appSettingsViewModel: AppSettingsViewModel = viewModel(factory = AppSettingsViewModelFactory.Factory) | ||
val accountSettingsViewModel: AccountSettingsViewModel = viewModel(factory = AccountSettingsViewModelFactory.Factory) | ||
|
||
API.errorHandler = { | ||
Log.e("jerboa", it.toString()) | ||
|
@@ -125,8 +123,12 @@ class MainActivity : AppCompatActivity() { | |
null | ||
} | ||
|
||
LaunchedEffect(Unit) { | ||
fetchInitialData(accountSync, siteViewModel) | ||
val account = getCurrentAccount(accountViewModel) | ||
|
||
LaunchedEffect(account) { | ||
if (account == null || account.id != -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was an occasional race condition type issue where As a solution to this, I set an initial state value in Let me know if there's a better way to handle something like this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems fine to me. |
||
fetchInitialData(account, siteViewModel) | ||
} | ||
} | ||
|
||
val appSettings by appSettingsViewModel.appSettings.observeAsState(APP_SETTINGS_DEFAULT) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is referenced in view model factories here https://github.com/dessalines/jerboa/pull/973/files#diff-41754fa505a06441b96271ba020ae2e014472578466e14a44fd7ce01a6c578f4R37, based on the compose room workshop again https://github.com/google-developer-training/basic-android-kotlin-compose-training-inventory-app/blob/room/app/src/main/java/com/example/inventory/ui/AppViewModelProvider.kt#L65