Skip to content

Commit b7a0da5

Browse files
author
Ralston Da Silva
committed
Add a new project for DataStore
The data store details on DAC has inline code snippets https://developer.android.com/topic/libraries/architecture/datastore This PR adds code samples that can be used as snippets on DAC.
1 parent 8a28727 commit b7a0da5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1181
-3
lines changed

datastore/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

datastore/build.gradle.kts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
alias(libs.plugins.kotlin.android)
4+
alias(libs.plugins.compose.compiler)
5+
6+
// Needed for proto data store.
7+
alias(libs.plugins.google.protobuf)
8+
9+
// Needed for json data store.
10+
alias(libs.plugins.kotlin.serialization)
11+
}
12+
13+
android {
14+
namespace = "com.example.datastore.snippets"
15+
compileSdk = 36
16+
17+
defaultConfig {
18+
applicationId = "com.example.datastore.snippets"
19+
minSdk = 23
20+
targetSdk = 36
21+
versionCode = 1
22+
versionName = "1.0"
23+
24+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
25+
}
26+
27+
buildTypes {
28+
release {
29+
isMinifyEnabled = false
30+
proguardFiles(
31+
getDefaultProguardFile("proguard-android-optimize.txt"),
32+
"proguard-rules.pro"
33+
)
34+
}
35+
}
36+
compileOptions {
37+
sourceCompatibility = JavaVersion.VERSION_11
38+
targetCompatibility = JavaVersion.VERSION_11
39+
}
40+
kotlinOptions {
41+
jvmTarget = "11"
42+
}
43+
buildFeatures {
44+
compose = true
45+
}
46+
}
47+
48+
dependencies {
49+
implementation(libs.androidx.core.ktx)
50+
implementation(libs.androidx.lifecycle.runtime)
51+
implementation(libs.androidx.activity.compose)
52+
implementation(platform(libs.androidx.compose.bom))
53+
implementation(libs.androidx.compose.ui)
54+
implementation(libs.androidx.compose.ui.graphics)
55+
implementation(libs.androidx.compose.ui.tooling.preview)
56+
implementation(libs.androidx.compose.material3)
57+
implementation(libs.androidx.datastore)
58+
implementation(libs.androidx.datastore.preferences)
59+
implementation(libs.androidx.navigation3.ui)
60+
implementation(libs.androidx.navigation3.runtime)
61+
implementation(libs.google.protobuf.kotlin.lite)
62+
implementation(libs.kotlinx.serialization.json)
63+
testImplementation(libs.junit)
64+
androidTestImplementation(libs.androidx.test.ext.junit)
65+
androidTestImplementation(libs.androidx.test.espresso.core)
66+
androidTestImplementation(platform(libs.androidx.compose.bom))
67+
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
68+
debugImplementation(libs.androidx.compose.ui.tooling)
69+
debugImplementation(libs.androidx.compose.ui.test.manifest)
70+
}
71+
72+
// [START android_datastore_proto_task]
73+
protobuf {
74+
protoc {
75+
artifact = "com.google.protobuf:protoc:4.32.1"
76+
}
77+
generateProtoTasks {
78+
all().forEach { task ->
79+
task.builtins {
80+
create("java") {
81+
option("lite")
82+
}
83+
create("kotlin")
84+
}
85+
}
86+
}
87+
}
88+
// [END android_datastore_proto_task]

datastore/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.datastore.snippets
2+
3+
import androidx.compose.ui.test.junit4.createComposeRule
4+
import androidx.compose.ui.test.onNodeWithText
5+
import androidx.test.ext.junit.runners.AndroidJUnit4
6+
import com.example.datastore.snippets.preferences.PreferencesDataStoreScreen
7+
import org.junit.Rule
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class PreferencesDataStoreTest {
18+
@get:Rule
19+
val rule = createComposeRule()
20+
21+
@Test
22+
fun launchesSuccessfully() {
23+
// Arrange.
24+
rule.setContent {
25+
PreferencesDataStoreScreen()
26+
}
27+
28+
// Assert.
29+
rule.onNodeWithText("Preferences data Store").assertExists()
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.datastore.snippets
2+
3+
import androidx.compose.ui.test.junit4.createComposeRule
4+
import androidx.compose.ui.test.onNodeWithText
5+
import androidx.test.ext.junit.runners.AndroidJUnit4
6+
import com.example.datastore.snippets.proto.ProtoDataStoreScreen
7+
import org.junit.Rule
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ProtoDataStoreTest {
18+
@get:Rule
19+
val rule = createComposeRule()
20+
21+
@Test
22+
fun launchesSuccessfully() {
23+
// Arrange.
24+
rule.setContent {
25+
ProtoDataStoreScreen()
26+
}
27+
28+
// Assert.
29+
rule.onNodeWithText("Proto data Store").assertExists()
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<application
5+
android:allowBackup="true"
6+
android:icon="@mipmap/ic_launcher"
7+
android:label="@string/app_name"
8+
android:roundIcon="@mipmap/ic_launcher_round"
9+
android:supportsRtl="true"
10+
android:theme="@style/Theme.Snippets">
11+
<service
12+
android:name=".multiprocess.TimestampUpdateService"
13+
android:process=":my_process_id" />
14+
<activity
15+
android:name=".MainActivity"
16+
android:exported="true"
17+
android:theme="@style/Theme.Snippets">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN" />
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
</manifest>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example.datastore.snippets
2+
3+
import androidx.compose.foundation.clickable
4+
import androidx.compose.foundation.layout.Box
5+
import androidx.compose.foundation.layout.Column
6+
import androidx.compose.foundation.layout.fillMaxWidth
7+
import androidx.compose.foundation.layout.padding
8+
import androidx.compose.material3.Text
9+
import androidx.compose.runtime.Composable
10+
import androidx.compose.ui.Modifier
11+
import androidx.compose.ui.unit.dp
12+
import androidx.compose.ui.unit.sp
13+
14+
@Composable
15+
internal fun Home(backStack: MutableList<MainActivity.Snippets>) {
16+
Column {
17+
Item("Preferences Data Store") {
18+
backStack.add(MainActivity.Snippets.PreferencesDataStore)
19+
}
20+
Item("Proto Data Store") {
21+
backStack.add(MainActivity.Snippets.ProtoDataStore)
22+
}
23+
Item("Json Data Store") {
24+
backStack.add(MainActivity.Snippets.JsonDataStore)
25+
}
26+
Item("Multi process Data Store") {
27+
backStack.add(MainActivity.Snippets.MultiProcessDataStore)
28+
}
29+
}
30+
}
31+
32+
@Composable
33+
private fun Item(text: String, onClick: () -> Unit) {
34+
Box(
35+
Modifier
36+
.fillMaxWidth()
37+
.clickable(onClick = onClick)
38+
.padding(10.dp)
39+
) {
40+
Text(fontSize = 30.sp, text = text)
41+
}
42+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.example.datastore.snippets
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.activity.enableEdgeToEdge
7+
import androidx.compose.foundation.layout.fillMaxSize
8+
import androidx.compose.foundation.layout.padding
9+
import androidx.compose.material3.Scaffold
10+
import androidx.compose.material3.Text
11+
import androidx.compose.runtime.Composable
12+
import androidx.compose.runtime.mutableStateListOf
13+
import androidx.compose.runtime.remember
14+
import androidx.compose.ui.Modifier
15+
import androidx.compose.ui.tooling.preview.Preview
16+
import androidx.navigation3.runtime.NavEntry
17+
import androidx.navigation3.ui.NavDisplay
18+
import com.example.datastore.snippets.MainActivity.Snippets.JsonDataStore
19+
import com.example.datastore.snippets.MainActivity.Snippets.LandingPage
20+
import com.example.datastore.snippets.MainActivity.Snippets.MultiProcessDataStore
21+
import com.example.datastore.snippets.MainActivity.Snippets.PreferencesDataStore
22+
import com.example.datastore.snippets.MainActivity.Snippets.ProtoDataStore
23+
import com.example.datastore.snippets.json.JsonDataStoreScreen
24+
import com.example.datastore.snippets.multiprocess.MultiProcessDataStoreScreen
25+
import com.example.datastore.snippets.preferences.PreferencesDataStoreScreen
26+
import com.example.datastore.snippets.proto.ProtoDataStoreScreen
27+
import com.example.datastore.snippets.ui.theme.SnippetsTheme
28+
29+
class MainActivity : ComponentActivity() {
30+
31+
internal enum class Snippets { LandingPage, PreferencesDataStore, ProtoDataStore, JsonDataStore, MultiProcessDataStore }
32+
33+
override fun onCreate(savedInstanceState: Bundle?) {
34+
super.onCreate(savedInstanceState)
35+
enableEdgeToEdge()
36+
setContent {
37+
SnippetsTheme {
38+
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
39+
val backStack = remember { mutableStateListOf(LandingPage) }
40+
NavDisplay(
41+
modifier = Modifier.padding(innerPadding),
42+
backStack = backStack
43+
) {
44+
when (it) {
45+
LandingPage -> NavEntry(LandingPage) {
46+
Home(backStack)
47+
}
48+
49+
PreferencesDataStore -> NavEntry(PreferencesDataStore) {
50+
PreferencesDataStoreScreen()
51+
}
52+
53+
ProtoDataStore -> NavEntry(ProtoDataStore) {
54+
ProtoDataStoreScreen()
55+
}
56+
57+
JsonDataStore -> NavEntry(JsonDataStore) {
58+
JsonDataStoreScreen()
59+
}
60+
61+
MultiProcessDataStore -> NavEntry(MultiProcessDataStore) {
62+
MultiProcessDataStoreScreen()
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
72+
@Composable
73+
fun Greeting(name: String, modifier: Modifier = Modifier) {
74+
Text(
75+
text = "Hello $name!",
76+
modifier = modifier
77+
)
78+
}
79+
80+
@Preview(showBackground = true)
81+
@Composable
82+
fun GreetingPreview() {
83+
SnippetsTheme {
84+
Greeting("Android")
85+
}
86+
}

0 commit comments

Comments
 (0)