forked from sduduzog/slim-launcher
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor preferences into a new core_preferences proto file (#122)
- Loading branch information
Showing
17 changed files
with
321 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...src/main/java/com/sduduzog/slimlauncher/datasource/coreprefs/CorePreferencesRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.sduduzog.slimlauncher.datasource.coreprefs | ||
|
||
import android.util.Log | ||
import androidx.datastore.core.DataStore | ||
import androidx.lifecycle.LifecycleCoroutineScope | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.asLiveData | ||
import com.jkuester.unlauncher.datastore.CorePreferences | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import java.io.IOException | ||
|
||
class CorePreferencesRepository( | ||
private val corePreferencesStore: DataStore<CorePreferences>, | ||
private val lifecycleScope: LifecycleCoroutineScope | ||
) { | ||
private val corereferencesFlow: Flow<CorePreferences> = | ||
corePreferencesStore.data | ||
.catch { exception -> | ||
if (exception is IOException) { | ||
Log.e( | ||
"CorePrefRepo", | ||
"Error reading core preferences.", | ||
exception | ||
) | ||
emit(CorePreferences.getDefaultInstance()) | ||
} else { | ||
throw exception | ||
} | ||
} | ||
|
||
fun liveData(): LiveData<CorePreferences> { | ||
return corereferencesFlow.asLiveData() | ||
} | ||
|
||
fun get(): CorePreferences { | ||
return runBlocking { | ||
corereferencesFlow.first() | ||
} | ||
} | ||
|
||
fun updateActivateKeyboardInDrawer(activateKeyboardInDrawer: Boolean) { | ||
lifecycleScope.launch { | ||
corePreferencesStore.updateData { | ||
it.toBuilder().setActivateKeyboardInDrawer(activateKeyboardInDrawer).build() | ||
} | ||
} | ||
} | ||
|
||
fun updateSetAutomaticDeviceWallpaper(setDeviceWallpaper: Boolean) { | ||
lifecycleScope.launch { | ||
corePreferencesStore.updateData { | ||
it.toBuilder().setSetThemeWallpaper(setDeviceWallpaper).build() | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...src/main/java/com/sduduzog/slimlauncher/datasource/coreprefs/CorePreferencesSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.sduduzog.slimlauncher.datasource.coreprefs | ||
|
||
import androidx.datastore.core.CorruptionException | ||
import androidx.datastore.core.Serializer | ||
import com.google.protobuf.InvalidProtocolBufferException | ||
import com.jkuester.unlauncher.datastore.CorePreferences | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
|
||
/** | ||
* Serializer for the [CorePreferences] object defined in core_preferences.proto. | ||
*/ | ||
object CorePreferencesSerializer : Serializer<CorePreferences> { | ||
override val defaultValue: CorePreferences = CorePreferences.getDefaultInstance() | ||
|
||
@Suppress("BlockingMethodInNonBlockingContext") | ||
override suspend fun readFrom(input: InputStream): CorePreferences { | ||
try { | ||
return CorePreferences.parseFrom(input) | ||
} catch (exception: InvalidProtocolBufferException) { | ||
throw CorruptionException("Cannot read proto.", exception) | ||
} | ||
} | ||
|
||
@Suppress("BlockingMethodInNonBlockingContext") | ||
override suspend fun writeTo(t: CorePreferences, output: OutputStream) = | ||
t.writeTo(output) | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/sduduzog/slimlauncher/di/ActivityModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.sduduzog.slimlauncher.di | ||
|
||
import android.app.Activity | ||
import androidx.core.app.ComponentActivity | ||
import androidx.lifecycle.LifecycleCoroutineScope | ||
import androidx.lifecycle.lifecycleScope | ||
import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ActivityComponent | ||
import dagger.hilt.android.scopes.ActivityScoped | ||
|
||
@Module | ||
@InstallIn(ActivityComponent::class) | ||
class ActivityModule { | ||
@Provides | ||
fun provideLifecycleCoroutineScope(activity: Activity): LifecycleCoroutineScope = | ||
(activity as ComponentActivity).lifecycleScope | ||
|
||
@Provides | ||
@ActivityScoped | ||
fun providesUnlauncherDataSource( | ||
activity: Activity, | ||
lifecycleCoroutineScope: LifecycleCoroutineScope | ||
) = UnlauncherDataSource(activity, lifecycleCoroutineScope) | ||
} |
Oops, something went wrong.