Basics for user prefs, logging in, etc

This commit is contained in:
Damontecres 2025-09-21 19:56:40 -04:00
parent 763521aabf
commit 76d78246db
No known key found for this signature in database
15 changed files with 613 additions and 35 deletions

View file

@ -0,0 +1,53 @@
package com.github.damontecres.dolphin.hilt
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.DataStoreFactory
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.dataStoreFile
import androidx.room.Room
import com.github.damontecres.dolphin.data.DolphinDatabase
import com.github.damontecres.dolphin.data.JellyfinServerDao
import com.github.damontecres.dolphin.preferences.UserPreferences
import com.github.damontecres.dolphin.preferences.UserPreferencesSerializer
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object DatabaseModule {
@Provides
@Singleton
fun database(
@ApplicationContext context: Context,
): DolphinDatabase =
Room
.databaseBuilder(
context,
DolphinDatabase::class.java,
"dolphin",
).build()
@Provides
@Singleton
fun serverDao(db: DolphinDatabase): JellyfinServerDao = db.serverDao()
@Provides
@Singleton
fun userPreferencesDataStore(
@ApplicationContext context: Context,
userPreferencesSerializer: UserPreferencesSerializer,
): DataStore<UserPreferences> =
DataStoreFactory.create(
serializer = userPreferencesSerializer,
produceFile = { context.dataStoreFile("user_preferences.pb") },
corruptionHandler =
ReplaceFileCorruptionHandler(
produceNewData = { UserPreferences.getDefaultInstance() },
),
)
}

View file

@ -0,0 +1,57 @@
package com.github.damontecres.dolphin.hilt
import android.annotation.SuppressLint
import android.content.Context
import android.provider.Settings
import com.github.damontecres.dolphin.BuildConfig
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import org.jellyfin.sdk.Jellyfin
import org.jellyfin.sdk.api.okhttp.OkHttpFactory
import org.jellyfin.sdk.createJellyfin
import org.jellyfin.sdk.model.ClientInfo
import org.jellyfin.sdk.model.DeviceInfo
@Module
@InstallIn(SingletonComponent::class)
object DolphinModule {
@Provides
fun okHttpClient() =
OkHttpClient
.Builder()
.apply {
// TODO user agent, timeouts, logging, etc
}.build()
@Provides
fun okHttpFactory(okHttpClient: OkHttpClient) = OkHttpFactory(okHttpClient)
@Provides
fun jellyfin(
okHttpFactory: OkHttpFactory,
@ApplicationContext context: Context,
): Jellyfin =
createJellyfin {
this.context = context
clientInfo =
ClientInfo(
name = "Dolphin",
version = BuildConfig.VERSION_NAME,
)
deviceInfo =
DeviceInfo(
id = @SuppressLint("HardwareIds") Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID),
name = Settings.Global.getString(context.contentResolver, Settings.Global.DEVICE_NAME),
)
apiClientFactory = okHttpFactory
socketConnectionFactory = okHttpFactory
minimumServerVersion = Jellyfin.minimumVersion
}
@Provides
fun apiClient(jellyfin: Jellyfin) = jellyfin.createApi()
}