Skip to content

Commit

Permalink
Document domain package
Browse files Browse the repository at this point in the history
  • Loading branch information
wingio committed Dec 8, 2023
1 parent c5bae31 commit 2c5f570
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/xyz/wingio/dimett/domain/db/AccountsDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import androidx.room.Query
import androidx.room.Update
import xyz.wingio.dimett.domain.db.entities.Account

/**
* Used to manage saved [Account]s
*/
@Dao
interface AccountsDao {

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/xyz/wingio/dimett/domain/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import androidx.room.TypeConverters
import xyz.wingio.dimett.domain.db.entities.Account
import xyz.wingio.dimett.domain.db.entities.Instance

/**
* Manages complex persistent data (Such as accounts and instances)
*/
@Database(
entities = [Account::class, Instance::class],
version = 1
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/xyz/wingio/dimett/domain/db/Converters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import kotlinx.serialization.json.Json
import xyz.wingio.dimett.rest.dto.CustomEmoji
import xyz.wingio.dimett.rest.dto.user.Field

/**
* Contains methods to convert any non-primitive data types
*/
@ProvidedTypeConverter
class Converters(
private val json: Json
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/xyz/wingio/dimett/domain/db/InstancesDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import xyz.wingio.dimett.domain.db.entities.Account
import xyz.wingio.dimett.domain.db.entities.Instance

/**
* Used to manage saved [Instance]s
*/
@Dao
interface InstancesDao {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import androidx.room.PrimaryKey
import xyz.wingio.dimett.rest.dto.CustomEmoji
import xyz.wingio.dimett.rest.dto.user.Field

/**
* Represents an account that can be logged into
*
* @see xyz.wingio.dimett.rest.dto.user.CredentialUser
*/
@Entity(
foreignKeys = [ForeignKey(
entity = Instance::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

/**
* Represents a Mastodon compatible instance with an existing OAuth application
*/
@Entity
data class Instance(
@PrimaryKey val url: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,55 @@ import xyz.wingio.dimett.domain.db.entities.Account
import xyz.wingio.dimett.rest.dto.user.CredentialUser
import xyz.wingio.dimett.utils.mainThread

/**
* Manage all stored accounts (adding, switching, deleting)
*
* @param preferenceManager Used for obtaining the logged in id
*/
class AccountManager(
db: AppDatabase,
private val preferenceManager: PreferenceManager
) {

// All db accesses need to be done on another thread
private val managerScope = CoroutineScope(Dispatchers.IO)
private val dao = db.accountsDao()

/**
* Whether or not all accounts have been loaded yet
*/
var isInitialized by mutableStateOf(false)
private set

/**
* Cached version of the accounts table for fast synchronous account fetching
*/
var accounts: MutableList<Account> = mutableStateListOf()
private set

/**
* Currently logged on account, null when not logged in to any account
*/
val current: Account?
get() = get(preferenceManager.currentAccount)

init {
managerScope.launch {
val accs = dao.listAccounts()
mainThread {
accounts += accs
isInitialized = true
val accs = dao.listAccounts() // Fetch all accounts from the database
mainThread { // Compose state needs to be altered on the main thread
accounts += accs // Cache them all in memory
isInitialized = true // Mark us as ready
}
}
}

/**
* Adds an account using a [CredentialUser]
*
* @param user User to add as an account
* @param token The token for the [user]
* @param instance Instance the [user] is on
*/
fun addAccount(user: CredentialUser, token: String, instance: String) {
managerScope.launch {
val acct = with(user) {
Expand Down Expand Up @@ -77,6 +99,9 @@ class AccountManager(
}
}

/**
* Updates an account
*/
fun updateAccount(account: Account) {
managerScope.launch {
if (account.id.isNotBlank()) {
Expand All @@ -85,12 +110,18 @@ class AccountManager(
}
}

/**
* Switches to another account
*/
fun switchAccount(id: String) {
val otherAccount = accounts.find { it.id == id } ?: return

preferenceManager.currentAccount = otherAccount.id
}

/**
* Retrieves an account by its id or null if one doesn't exist
*/
operator fun get(id: String): Account? {
return accounts.find { it.id == id }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import xyz.wingio.dimett.domain.db.AppDatabase
import xyz.wingio.dimett.domain.db.entities.Instance
import xyz.wingio.dimett.utils.mainThread

class InstanceManager(database: AppDatabase, val accountManager: AccountManager) {
private val dao = database.instancesDao()
/**
* Managed saved instances and associated OAuth apps
*/
class InstanceManager(
database: AppDatabase,
val accountManager: AccountManager
) {

// All db accesses need to be done on another thread
private val managerScope = CoroutineScope(Dispatchers.IO)
private val dao = database.instancesDao()

/**
* Cached version of the instances table for fast synchronous instance fetching
*/
var instances: MutableList<Instance> = mutableListOf()
private set

/**
* Gets the instance for the currently logged on account
*/
val current: Instance?
get() {
val account = accountManager.current ?: return null
Expand All @@ -22,10 +37,21 @@ class InstanceManager(database: AppDatabase, val accountManager: AccountManager)

init {
managerScope.launch {
instances = dao.listInstances().toMutableList()
val _instances = dao.listInstances() // Fetch all accounts from the database
mainThread { // Compose state needs to be altered on the main thread
instances += _instances // Cache them all in memory
}
}
}

/**
* Stores an [Instance]
*
* @param url Base url for the instance
* @param clientId Client id for the associated OAuth app
* @param clientSecret Client secret for the associated OAuth app
* @param features Supported features that this instance has (Ex. reactions)
*/
fun addInstance(
url: String,
clientId: String,
Expand All @@ -40,10 +66,16 @@ class InstanceManager(database: AppDatabase, val accountManager: AccountManager)
return i
}

/**
* Checks if the instance with the given [url] already has an OAuth app we could use
*/
fun exists(url: String): Boolean {
return instances.find { it.url == url } != null
}

/**
* Obtains the instance with the given [url]
*/
operator fun get(url: String) = instances.find { it.url == url }

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package xyz.wingio.dimett.domain.manager
import android.content.Context
import xyz.wingio.dimett.domain.manager.base.BasePreferenceManager

/**
* Manage general app preferences
*/
class PreferenceManager(context: Context) :
BasePreferenceManager(context.getSharedPreferences("prefs", Context.MODE_PRIVATE)) {

/**
* Id for the currently logged in account
*/
var currentAccount by stringPreference("current_account")

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import androidx.compose.ui.graphics.Color
import androidx.core.content.edit
import kotlin.reflect.KProperty

/**
* Utility for managing [SharedPreferences] with Compose state
*
* @param prefs [SharedPreferences] instance to be managed
*/
abstract class BasePreferenceManager(
private val prefs: SharedPreferences
) {
Expand Down Expand Up @@ -35,6 +40,9 @@ abstract class BasePreferenceManager(
protected inline fun <reified E : Enum<E>> putEnum(key: String, value: E) =
putString(key, value.name)

/**
* Delegate that wraps around a preference to give it state
*/
protected class Preference<T>(
private val key: String,
defaultValue: T,
Expand All @@ -52,6 +60,12 @@ abstract class BasePreferenceManager(
}
}

/**
* Delegate for a [String] based preference
*
* @param key The preferences name
* @param defaultValue Starting value for this preference
*/
protected fun stringPreference(
key: String,
defaultValue: String = ""
Expand All @@ -62,6 +76,12 @@ abstract class BasePreferenceManager(
setter = ::putString
)

/**
* Delegate for a [Boolean] based preference
*
* @param key The preferences name
* @param defaultValue Starting value for this preference
*/
protected fun booleanPreference(
key: String,
defaultValue: Boolean
Expand All @@ -72,6 +92,12 @@ abstract class BasePreferenceManager(
setter = ::putBoolean
)

/**
* Delegate for an [Int] based preference
*
* @param key The preferences name
* @param defaultValue Starting value for this preference
*/
protected fun intPreference(
key: String,
defaultValue: Int
Expand All @@ -82,6 +108,12 @@ abstract class BasePreferenceManager(
setter = ::putInt
)

/**
* Delegate for a [Float] based preference
*
* @param key The preferences name
* @param defaultValue Starting value for this preference
*/
protected fun floatPreference(
key: String,
defaultValue: Float
Expand All @@ -92,6 +124,12 @@ abstract class BasePreferenceManager(
setter = ::putFloat
)

/**
* Delegate for a [Color] based preference
*
* @param key The preferences name
* @param defaultValue Starting value for this preference
*/
protected fun colorPreference(
key: String,
defaultValue: Color
Expand All @@ -102,7 +140,12 @@ abstract class BasePreferenceManager(
setter = ::putColor
)


/**
* Delegate for an [Enum] based preference
*
* @param key The preferences name
* @param defaultValue Starting value for this preference
*/
protected inline fun <reified E : Enum<E>> enumPreference(
key: String,
defaultValue: E
Expand Down

0 comments on commit 2c5f570

Please sign in to comment.