|
1 | 1 | # Data Layer |
2 | 2 |
|
3 | | -This page outlines the data-related building blocks provided by AppToolkit. |
| 3 | +This page outlines how the app manages and persists data. |
4 | 4 |
|
5 | | -## HTTP client |
| 5 | +## Repositories |
6 | 6 |
|
7 | | -`apptoolkit/data/client` exposes **KtorClient**, a factory for a preconfigured Ktor `HttpClient`. It installs JSON content negotiation, request timeouts, default headers and optional logging so callers only need to supply their own endpoints. |
| 7 | +Repositories expose data to the rest of the app and hide the underlying storage. |
8 | 8 |
|
9 | | -### Setup |
10 | | - |
11 | | -```kotlin |
12 | | -val client = KtorClient().createClient(enableLogging = true) |
| 9 | +```java |
| 10 | +public interface MainRepository { |
| 11 | + boolean shouldShowStartupScreen(); |
| 12 | + void markStartupScreenShown(); |
| 13 | +} |
13 | 14 | ``` |
14 | 15 |
|
15 | | -## DataStore |
16 | | - |
17 | | -The `apptoolkit/data/datastore` package wraps Android DataStore in a singleton `CommonDataStore`. It centralizes preferences such as startup flags, theme options and user consents, exposing them as Kotlin `Flow`s with suspend functions to persist updates. |
| 16 | +`DefaultMainRepository` implements these methods using `SharedPreferences`: |
18 | 17 |
|
19 | | -### Usage |
| 18 | +```java |
| 19 | +public class DefaultMainRepository implements MainRepository { |
| 20 | + private final Context context; |
20 | 21 |
|
21 | | -```kotlin |
22 | | -val dataStore = CommonDataStore.getInstance(context) |
| 22 | + public DefaultMainRepository(Context context) { |
| 23 | + this.context = context.getApplicationContext(); |
| 24 | + } |
23 | 25 |
|
24 | | -// Observe a value |
25 | | -val adsEnabled = dataStore.ads(default = true) |
| 26 | + @Override |
| 27 | + public boolean shouldShowStartupScreen() { |
| 28 | + SharedPreferences startup = context.getSharedPreferences("startup", Context.MODE_PRIVATE); |
| 29 | + return startup.getBoolean("value", true); |
| 30 | + } |
26 | 31 |
|
27 | | -// Save a value |
28 | | -scope.launch { dataStore.saveThemeMode("dark") } |
| 32 | + @Override |
| 33 | + public void markStartupScreenShown() { |
| 34 | + SharedPreferences startup = context.getSharedPreferences("startup", Context.MODE_PRIVATE); |
| 35 | + startup.edit().putBoolean("value", false).apply(); |
| 36 | + } |
| 37 | +} |
29 | 38 | ``` |
30 | 39 |
|
31 | | -## Ads |
| 40 | +## Data sources |
| 41 | + |
| 42 | +Remote and local sources supply the repositories with data. For example, `DefaultHomeRemoteDataSource` uses Volley to fetch promoted apps: |
| 43 | + |
| 44 | +```java |
| 45 | +public class DefaultHomeRemoteDataSource implements HomeRemoteDataSource { |
| 46 | + private final RequestQueue requestQueue; |
| 47 | + private final String apiUrl; |
| 48 | + |
| 49 | + public DefaultHomeRemoteDataSource(RequestQueue requestQueue, String apiUrl) { |
| 50 | + this.requestQueue = requestQueue; |
| 51 | + this.apiUrl = apiUrl; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void fetchPromotedApps(PromotedAppsCallback callback) { |
| 56 | + JsonObjectRequest request = new JsonObjectRequest( |
| 57 | + Request.Method.GET, |
| 58 | + apiUrl, |
| 59 | + null, |
| 60 | + response -> { /* parse and callback */ }, |
| 61 | + error -> { /* handle error */ } |
| 62 | + ); |
| 63 | + requestQueue.add(request); |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
32 | 67 |
|
33 | | -Ads are configured through preferences in `CommonDataStore` via the `ads` flag and related consent entries. The `core/ads` package provides `AdsCoreManager`, which checks those preferences before initializing Google Mobile Ads and manages app-open ad loading and display. |
| 68 | +## Models |
34 | 69 |
|
35 | | -Use `AdsCoreManager` when the application should show an app-open ad on start or resume: |
| 70 | +Model classes like `PromotedApp` encapsulate the data returned by the layer: |
36 | 71 |
|
37 | | -```kotlin |
38 | | -val adsManager = AdsCoreManager(context, buildInfoProvider) |
39 | | -scope.launch { adsManager.initializeAds("ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx") } |
40 | | -adsManager.showAdIfAvailable(activity, scope) |
| 72 | +```java |
| 73 | +public record PromotedApp(String name, String packageName, String iconUrl) {} |
41 | 74 | ``` |
42 | 75 |
|
| 76 | +## See also |
| 77 | + |
| 78 | +- [[Architecture]] – overview of app layers. |
| 79 | +- [[Core Module]] – shared utilities and components. |
| 80 | + |
0 commit comments