wip: ugh why cant coroutines be documented
This commit is contained in:
parent
56224a0dd7
commit
3d955d9446
8 changed files with 111 additions and 3 deletions
|
@ -58,9 +58,10 @@ dependencies {
|
|||
implementation(libs.androidx.material3)
|
||||
implementation(libs.androidx.material.icons.extended)
|
||||
implementation(libs.androidx.datastore)
|
||||
implementation(libs.androidx.datastore.preferences)
|
||||
|
||||
implementation(libs.dagger.hilt.android)
|
||||
kapt(libs.hilt.android.compiler)
|
||||
kapt(libs.hilt.compiler)
|
||||
|
||||
testImplementation(libs.junit)
|
||||
androidTestImplementation(libs.androidx.junit)
|
||||
|
|
|
@ -13,11 +13,13 @@ import androidx.navigation.compose.NavHost
|
|||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import pet.catcomm.ui.compose.BottomBar
|
||||
import pet.catcomm.ui.compose.Peers
|
||||
import pet.catcomm.ui.compose.TopBar
|
||||
import pet.catcomm.ui.theme.CatCommTheme
|
||||
|
||||
@AndroidEntryPoint
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
|
|
@ -1,6 +1,22 @@
|
|||
package pet.catcomm.store
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import pet.catcomm.store.di.DefaultDispatcher
|
||||
import pet.catcomm.store.repository.SettingsRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class CatCommViewModel
|
||||
class CatCommViewModel @Inject constructor(
|
||||
private val settingsRepository: SettingsRepository,
|
||||
@DefaultDispatcher private val defaultDispatcher: CoroutineDispatcher
|
||||
) : ViewModel() {
|
||||
val usernameFlow: Flow<String> = flowOf("Woof")
|
||||
|
||||
init {
|
||||
println("woof") // why are you saying the method doesnt exist, android
|
||||
}
|
||||
}
|
||||
|
|
45
app/src/main/java/pet/catcomm/store/di/DataStoreModule.kt
Normal file
45
app/src/main/java/pet/catcomm/store/di/DataStoreModule.kt
Normal file
|
@ -0,0 +1,45 @@
|
|||
package pet.catcomm.store.di
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
|
||||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.emptyPreferences
|
||||
import androidx.datastore.preferences.preferencesDataStoreFile
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import javax.inject.Singleton
|
||||
|
||||
private const val PREFERENCES = "settings"
|
||||
|
||||
@InstallIn(SingletonComponent::class)
|
||||
@Module
|
||||
object DataStoreModule {
|
||||
@Singleton
|
||||
@Provides
|
||||
fun providePreferencesDataStore(@ApplicationContext appCtx: Context): DataStore<Preferences> =
|
||||
PreferenceDataStoreFactory.create(
|
||||
scope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
|
||||
produceFile = { appCtx.preferencesDataStoreFile(PREFERENCES) },
|
||||
corruptionHandler = ReplaceFileCorruptionHandler { emptyPreferences() }
|
||||
)
|
||||
}
|
||||
|
||||
//val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
|
||||
//
|
||||
//const val SETTINGS_USERNAME_KEY = "cat_comm_username"
|
||||
//val SETTINGS_USERNAME = stringPreferencesKey(SETTINGS_USERNAME_KEY)
|
||||
//
|
||||
//
|
||||
//class SettingsRepository @Inject constructor(@ApplicationContext private val context: Context) {
|
||||
// val username: Flow<String> = context.dataStore.data.map { preferences ->
|
||||
// preferences[SETTINGS_USERNAME] ?: ""
|
||||
// }
|
||||
//}
|
24
app/src/main/java/pet/catcomm/store/di/DispatchersModule.kt
Normal file
24
app/src/main/java/pet/catcomm/store/di/DispatchersModule.kt
Normal file
|
@ -0,0 +1,24 @@
|
|||
package pet.catcomm.store.di
|
||||
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import javax.inject.Qualifier
|
||||
|
||||
/**
|
||||
* module class to wrap the coroutine dispatchers in something the dependency injector can catch
|
||||
*/
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
class DispatchersModule {
|
||||
@Provides
|
||||
@DefaultDispatcher
|
||||
fun provideDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
|
||||
}
|
||||
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
@Qualifier
|
||||
annotation class DefaultDispatcher
|
|
@ -0,0 +1,9 @@
|
|||
package pet.catcomm.store.repository
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import javax.inject.Inject
|
||||
|
||||
class SettingsRepository @Inject constructor(
|
||||
private val dataStore: DataStore<Preferences>
|
||||
)
|
|
@ -10,9 +10,13 @@ import androidx.compose.material3.CardDefaults
|
|||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import pet.catcomm.store.CatCommViewModel
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import pet.catcomm.ui.theme.CatCommTheme
|
||||
|
||||
@Composable
|
||||
|
@ -38,7 +42,12 @@ fun PeerPreview() {
|
|||
}
|
||||
|
||||
@Composable
|
||||
fun Peers(modifier: Modifier = Modifier) {
|
||||
fun Peers(
|
||||
modifier: Modifier = Modifier,
|
||||
viewModel: CatCommViewModel = viewModel(),
|
||||
) {
|
||||
// val username by viewModel.usernameFlow.collectAsStateWithLifecycle("woof")
|
||||
|
||||
LazyColumn(modifier = modifier.padding(vertical = 4.dp)) {
|
||||
items(items = listOf("nyx", "1686a", "8098")) {
|
||||
name -> Peer(name = name)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
[versions]
|
||||
agp = "8.8.2"
|
||||
datastore = "1.1.3"
|
||||
datastorePreferences = "1.1.3"
|
||||
hiltPlugin = "2.51.1"
|
||||
hiltAndroidVersion = "2.55"
|
||||
hiltCompiler = "2.55"
|
||||
|
@ -17,6 +18,7 @@ navigationCompose = "2.8.8"
|
|||
[libraries]
|
||||
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
|
||||
androidx-datastore = { module = "androidx.datastore:datastore", version.ref = "datastore" }
|
||||
androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastorePreferences" }
|
||||
androidx-material-icons-extended = { module = "androidx.compose.material:material-icons-extended" }
|
||||
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigationCompose" }
|
||||
dagger-hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroidVersion" }
|
||||
|
|
Loading…
Add table
Reference in a new issue