For #21593 - Persist stories categories selections in a Proto DataStore
A fast and easy solution with all the ACID requirements. Also supports easy migrations if later the data we need persisted changes.upstream-sync
parent
565beb88c9
commit
e4489b8d7d
@ -0,0 +1,17 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.datastore
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.dataStore
|
||||
|
||||
/**
|
||||
* Application / process unique [DataStore] for IO operations related to Pocket recommended stories selected categories.
|
||||
*/
|
||||
internal val Context.pocketStoriesSelectedCategoriesDataStore: DataStore<SelectedPocketStoriesCategories> by dataStore(
|
||||
fileName = "pocket_recommendations_selected_categories.pb",
|
||||
serializer = SelectedPocketStoriesCategorySerializer
|
||||
)
|
@ -0,0 +1,25 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.datastore
|
||||
|
||||
import androidx.datastore.core.Serializer
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
/**
|
||||
* Serializer for [SelectedPocketStoriesCategories] defined in selected_pocket_stories_categories.proto.
|
||||
*/
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
object SelectedPocketStoriesCategorySerializer : Serializer<SelectedPocketStoriesCategories> {
|
||||
override val defaultValue: SelectedPocketStoriesCategories = SelectedPocketStoriesCategories.getDefaultInstance()
|
||||
|
||||
override suspend fun readFrom(input: InputStream): SelectedPocketStoriesCategories {
|
||||
return SelectedPocketStoriesCategories.parseFrom(input)
|
||||
}
|
||||
|
||||
override suspend fun writeTo(t: SelectedPocketStoriesCategories, output: OutputStream) {
|
||||
t.writeTo(output)
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
package proto;
|
||||
|
||||
option java_package = "org.mozilla.fenix.datastore";
|
||||
option java_multiple_files = true;
|
||||
|
||||
// List of currently selected Pocket recommended stories categories.
|
||||
message SelectedPocketStoriesCategories {
|
||||
|
||||
// Details about a selected Pocket recommended stories category.
|
||||
// See [org.mozilla.fenix.home.sessioncontrol.viewholders.pocket.PocketRecommendedStoriesSelectedCategory]
|
||||
message SelectedPocketStoriesCategory {
|
||||
// Name of this category.
|
||||
string name = 1;
|
||||
// Timestamp for when this category was selected.
|
||||
int64 selectionTimestamp = 2;
|
||||
}
|
||||
|
||||
// Currently selected Pocket stories categories.
|
||||
repeated SelectedPocketStoriesCategory values = 1;
|
||||
}
|
Loading…
Reference in New Issue