removed DI and did it "the manual way" and now it works
This commit is contained in:
parent
3d955d9446
commit
ac28926e6e
9 changed files with 121 additions and 108 deletions
57
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
57
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
|
@ -0,0 +1,57 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="ComposePreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="ComposePreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="ComposePreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="ComposePreviewNotSupportedInUnitTestFiles" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="GlancePreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="GlancePreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="GlancePreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="GlancePreviewNotSupportedInUnitTestFiles" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewAnnotationInFunctionWithParameters" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewApiLevelMustBeValid" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewDeviceShouldUseNewSpec" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewFontScaleMustBeGreaterThanZero" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewMultipleParameterProviders" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="PreviewPickerAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
|
||||
<option name="composableFile" value="true" />
|
||||
<option name="previewFile" value="true" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
|
@ -1,8 +1,6 @@
|
|||
package pet.catcomm
|
||||
|
||||
import android.app.Application
|
||||
import dagger.hilt.android.HiltAndroidApp
|
||||
|
||||
@HiltAndroidApp
|
||||
class CatCommApplication : Application() {
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@ 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,22 +0,0 @@
|
|||
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 @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
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
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] ?: ""
|
||||
// }
|
||||
//}
|
|
@ -1,24 +0,0 @@
|
|||
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
|
|
@ -1,9 +0,0 @@
|
|||
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,14 +10,17 @@ 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.runtime.LaunchedEffect
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
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 kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.map
|
||||
import pet.catcomm.ui.theme.CatCommTheme
|
||||
import pet.catcomm.ui.viewmodel.DataStoreViewModel
|
||||
|
||||
@Composable
|
||||
fun Peer(name: String, modifier: Modifier = Modifier) {
|
||||
|
@ -44,12 +47,18 @@ fun PeerPreview() {
|
|||
@Composable
|
||||
fun Peers(
|
||||
modifier: Modifier = Modifier,
|
||||
viewModel: CatCommViewModel = viewModel(),
|
||||
dataStoreViewModel: DataStoreViewModel = viewModel(factory = DataStoreViewModel.Factory),
|
||||
) {
|
||||
// val username by viewModel.usernameFlow.collectAsStateWithLifecycle("woof")
|
||||
val username = dataStoreViewModel.usernameSetting.collectAsStateWithLifecycle("")
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
// Test: it'll change the username setting after 2s and this should refresh the UI as expected
|
||||
delay(2000)
|
||||
dataStoreViewModel.changeUsername("woof")
|
||||
}
|
||||
|
||||
LazyColumn(modifier = modifier.padding(vertical = 4.dp)) {
|
||||
items(items = listOf("nyx", "1686a", "8098")) {
|
||||
items(items = listOf("nyx", "1686a", "8098", username.value)) {
|
||||
name -> Peer(name = name)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package pet.catcomm.ui.viewmodel
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import kotlinx.coroutines.flow.map
|
||||
import pet.catcomm.CatCommApplication
|
||||
|
||||
private const val PREFERENCES = "settings"
|
||||
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = PREFERENCES)
|
||||
|
||||
const val SETTINGS_USERNAME_KEY = "cat_comm_username"
|
||||
val SETTINGS_USERNAME = stringPreferencesKey(SETTINGS_USERNAME_KEY)
|
||||
|
||||
class DataStoreViewModel(
|
||||
private val settingsDataStore: DataStore<Preferences>,
|
||||
) : ViewModel() {
|
||||
val usernameSetting = settingsDataStore.data.map { preferences ->
|
||||
preferences[SETTINGS_USERNAME] ?: ""
|
||||
}
|
||||
|
||||
suspend fun changeUsername(new: String) {
|
||||
settingsDataStore.edit { settings ->
|
||||
settings[SETTINGS_USERNAME] = new
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val Factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel> create(
|
||||
modelClass: Class<T>,
|
||||
extras: CreationExtras,
|
||||
): T {
|
||||
val app =
|
||||
checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as CatCommApplication
|
||||
|
||||
return DataStoreViewModel(
|
||||
settingsDataStore = app.baseContext.dataStore,
|
||||
) as T
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue