Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Accounts refactor #586

Merged
merged 20 commits into from
Jun 20, 2024
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 @@ -91,6 +91,7 @@ dependencies {
implementation(project(":feature:followedHashTags"))
implementation(project(":feature:bookmarks"))
implementation(project(":core:ui:chooseAccount"))
implementation(project(":core:accounts"))

implementation(kotlin("reflect"))

Expand Down
33 changes: 19 additions & 14 deletions app/src/main/kotlin/social/firefly/IntentHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,34 @@ package social.firefly
import android.content.Intent
import android.net.Uri
import android.os.Parcelable
import social.firefly.core.datastore.UserPreferencesDatastoreManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import social.firefly.core.accounts.AccountsManager
import social.firefly.core.navigation.Event
import social.firefly.core.navigation.EventRelay
import social.firefly.core.share.ShareInfo

class IntentHandler(
private val eventRelay: EventRelay,
private val userPreferencesDatastoreManager: UserPreferencesDatastoreManager,
private val accountsManager: AccountsManager,
) {

fun handleIntent(intent: Intent) {
if (!userPreferencesDatastoreManager.isLoggedInToAtLeastOneAccount) return
when {
intent.action == Intent.ACTION_SEND -> {
when {
intent.type == "text/plain" -> {
handleSendTextIntentReceived(intent)
}
intent.type?.contains("image") == true -> {
handleSendImageIntentReceived(intent)
}
intent.type?.contains("video") == true -> {
handleSendVideoIntentReceived(intent)
CoroutineScope(Dispatchers.Default).launch {
if (accountsManager.getAllAccounts().isEmpty()) return@launch
when {
intent.action == Intent.ACTION_SEND -> {
when {
intent.type == "text/plain" -> {
handleSendTextIntentReceived(intent)
}
intent.type?.contains("image") == true -> {
handleSendImageIntentReceived(intent)
}
intent.type?.contains("video") == true -> {
handleSendVideoIntentReceived(intent)
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/kotlin/social/firefly/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.koin.androidx.workmanager.koin.workManagerFactory
import org.koin.core.context.startKoin
import org.koin.dsl.module
import social.firefly.common.Version
import social.firefly.core.accounts.accountsModule
import social.firefly.core.analytics.AppAnalytics
import social.firefly.core.repository.mastodon.AuthCredentialObserver
import social.firefly.core.workmanager.workManagerModule
Expand Down Expand Up @@ -102,6 +103,7 @@ class MainApplication : Application(), ImageLoaderFactory {
workManagerModule,
pushModule,
chooseAccountModule,
accountsModule,
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/kotlin/social/firefly/splash/SplashViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import social.firefly.IntentHandler
import social.firefly.core.datastore.UserPreferencesDatastoreManager
import social.firefly.core.accounts.AccountsManager
import social.firefly.core.navigation.NavigationDestination
import social.firefly.core.navigation.usecases.NavigateTo
import social.firefly.core.repository.mastodon.TimelineRepository
Expand All @@ -14,10 +14,10 @@ import social.firefly.ui.AppState

class SplashViewModel(
private val navigateTo: NavigateTo,
private val userPreferencesDatastoreManager: UserPreferencesDatastoreManager,
private val timelineRepository: TimelineRepository,
private val updateAllLoggedInAccounts: UpdateAllLoggedInAccounts,
private val intentHandler: IntentHandler,
private val accountsManager: AccountsManager,
) : ViewModel() {

fun initialize(intent: Intent?) {
Expand All @@ -27,7 +27,7 @@ class SplashViewModel(

AppState.navigationCollectionCompletable.await()

if (userPreferencesDatastoreManager.isLoggedInToAtLeastOneAccount) {
if (accountsManager.getAllAccounts().isNotEmpty()) {
navigateTo(NavigationDestination.Tabs)
intent?.let { intentHandler.handleIntent(intent) }
updateAllLoggedInAccounts()
Expand Down
1 change: 1 addition & 0 deletions core/accounts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
27 changes: 27 additions & 0 deletions core/accounts/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id("social.firefly.android.library")
alias(libs.plugins.kotlin.ksp)
alias(libs.plugins.kotlinx.serialization)
}

android {
namespace = "social.firefly.core.accounts"

defaultConfig {
ksp {
arg("room.schemaLocation", "$projectDir/schemas")
}
}
}

dependencies {
implementation(project(":core:model"))

implementation(libs.koin.android)

implementation(libs.androidx.room.runtime)
ksp(libs.androidx.room.compiler)
implementation(libs.androidx.room)

implementation(libs.kotlinx.serialization.json)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "fa0fd12964a2830e2fdd786ebac516aa",
"entities": [
{
"tableName": "mastodonAccounts",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`accessToken` TEXT NOT NULL, `accountId` TEXT NOT NULL, `domain` TEXT NOT NULL, `avatarUrl` TEXT NOT NULL, `userName` TEXT NOT NULL, `defaultLanguage` TEXT NOT NULL, `serializedPushKeys` TEXT, `lastSeenHomeStatusId` TEXT, PRIMARY KEY(`accountId`))",
"fields": [
{
"fieldPath": "accessToken",
"columnName": "accessToken",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "accountId",
"columnName": "accountId",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "domain",
"columnName": "domain",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "avatarUrl",
"columnName": "avatarUrl",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "userName",
"columnName": "userName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "defaultLanguage",
"columnName": "defaultLanguage",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "serializedPushKeys",
"columnName": "serializedPushKeys",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "lastSeenHomeStatusId",
"columnName": "lastSeenHomeStatusId",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"accountId"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "activeAccount",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`key` INTEGER NOT NULL, `accountType` TEXT NOT NULL, `accountId` TEXT NOT NULL, PRIMARY KEY(`key`), FOREIGN KEY(`accountId`) REFERENCES `mastodonAccounts`(`accountId`) ON UPDATE CASCADE ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "key",
"columnName": "key",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "accountType",
"columnName": "accountType",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "accountId",
"columnName": "accountId",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"key"
]
},
"indices": [],
"foreignKeys": [
{
"table": "mastodonAccounts",
"onDelete": "CASCADE",
"onUpdate": "CASCADE",
"columns": [
"accountId"
],
"referencedColumns": [
"accountId"
]
}
]
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'fa0fd12964a2830e2fdd786ebac516aa')"
]
}
}
4 changes: 4 additions & 0 deletions core/accounts/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package social.firefly.core.accounts

import androidx.room.Database
import androidx.room.RoomDatabase
import social.firefly.core.accounts.dao.ActiveAccountDao
import social.firefly.core.accounts.dao.MastodonAccountsDao
import social.firefly.core.accounts.model.ActiveAccount
import social.firefly.core.accounts.model.MastodonAccount

@Database(
entities = [
MastodonAccount::class,
ActiveAccount::class,
],
version = 1,
autoMigrations = [

],
exportSchema = true
)
internal abstract class AccountsDatabase : RoomDatabase() {
abstract fun mastodonAccountsDao(): MastodonAccountsDao
abstract fun activeAccountsDao(): ActiveAccountDao
}
Loading