From 9f8e88e027dd78e17ee518f3b9485c8cbea23c19 Mon Sep 17 00:00:00 2001 From: Tournaris Pavlos-Petros Date: Tue, 7 Mar 2017 21:16:26 +0200 Subject: [PATCH] Fix sample app to init Store on Application creation --- app/src/main/AndroidManifest.xml | 3 +- .../com/nytimes/android/sample/SampleApp.java | 100 ++++++++++++++++++ .../activity/PersistingStoreActivity.java | 70 +++--------- .../sample/activity/StoreActivity.java | 47 +++----- 4 files changed, 132 insertions(+), 88 deletions(-) create mode 100644 app/src/main/java/com/nytimes/android/sample/SampleApp.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ac31c7e..5a7e4dc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,9 +8,10 @@ android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" + android:name=".SampleApp" android:theme="@style/AppTheme"> + android:name=".activity.StoreActivity"> diff --git a/app/src/main/java/com/nytimes/android/sample/SampleApp.java b/app/src/main/java/com/nytimes/android/sample/SampleApp.java new file mode 100644 index 0000000..6a53232 --- /dev/null +++ b/app/src/main/java/com/nytimes/android/sample/SampleApp.java @@ -0,0 +1,100 @@ +package com.nytimes.android.sample; + +import android.app.Application; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.nytimes.android.external.cache.CacheBuilder; +import com.nytimes.android.external.fs.SourcePersisterFactory; +import com.nytimes.android.external.store.base.Persister; +import com.nytimes.android.external.store.base.impl.BarCode; +import com.nytimes.android.external.store.base.impl.Store; +import com.nytimes.android.external.store.base.impl.StoreBuilder; +import com.nytimes.android.external.store.middleware.GsonParserFactory; +import com.nytimes.android.sample.data.model.GsonAdaptersModel; +import com.nytimes.android.sample.data.model.RedditData; +import com.nytimes.android.sample.data.remote.Api; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import okio.BufferedSource; +import retrofit2.GsonConverterFactory; +import retrofit2.Retrofit; +import retrofit2.RxJavaCallAdapterFactory; +import rx.Observable; + +public class SampleApp extends Application { + + private Store nonPersistedStore; + private Store persistedStore; + private Persister persister; + + @Override + public void onCreate() { + super.onCreate(); + + initPersister(); + this.nonPersistedStore = provideRedditStore(); + this.persistedStore = providePersistedRedditStore(); + } + + private void initPersister() { + try { + persister = newPersister(); + } catch (IOException exception) { + throw new RuntimeException(exception); + } + } + + public Store getNonPersistedStore() { + return this.nonPersistedStore; + } + + public Store getPersistedStore() { + return this.persistedStore; + } + + private Store provideRedditStore() { + return StoreBuilder.barcode() + .fetcher(barCode -> provideRetrofit().fetchSubreddit(barCode.getKey(), "10")) + .memory(CacheBuilder.newBuilder() + .maximumSize(1) + .expireAfterWrite(10, TimeUnit.SECONDS) + .build()) + .open(); + } + + private Store providePersistedRedditStore() { + return StoreBuilder.parsedWithKey() + .fetcher(this::fetcher) + .persister(persister) + .parser(GsonParserFactory.createSourceParser(provideGson(), RedditData.class)) + .open(); + } + + private Persister newPersister() throws IOException { + return SourcePersisterFactory.create(getApplicationContext().getCacheDir()); + } + + private Observable fetcher(BarCode barCode) { + return provideRetrofit().fetchSubredditForPersister(barCode.getKey(), "10") + .map(responseBody -> responseBody.source()); + } + + private Api provideRetrofit() { + return new Retrofit.Builder() + .baseUrl("http://reddit.com/") + .addConverterFactory(GsonConverterFactory.create(provideGson())) + .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) + .validateEagerly(BuildConfig.DEBUG) // Fail early: check Retrofit configuration at creation time in Debug build. + .build() + .create(Api.class); + } + + Gson provideGson() { + return new GsonBuilder() + .registerTypeAdapterFactory(new GsonAdaptersModel()) + .create(); + } +} diff --git a/app/src/main/java/com/nytimes/android/sample/activity/PersistingStoreActivity.java b/app/src/main/java/com/nytimes/android/sample/activity/PersistingStoreActivity.java index 91118e2..74e1bf2 100644 --- a/app/src/main/java/com/nytimes/android/sample/activity/PersistingStoreActivity.java +++ b/app/src/main/java/com/nytimes/android/sample/activity/PersistingStoreActivity.java @@ -7,30 +7,17 @@ import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.widget.Toast; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.nytimes.android.external.fs.SourcePersisterFactory; -import com.nytimes.android.external.store.base.Persister; -import com.nytimes.android.external.store.base.impl.Store; import com.nytimes.android.external.store.base.impl.BarCode; -import com.nytimes.android.external.store.base.impl.StoreBuilder; -import com.nytimes.android.external.store.middleware.GsonParserFactory; -import com.nytimes.android.sample.BuildConfig; +import com.nytimes.android.external.store.base.impl.Store; import com.nytimes.android.sample.R; +import com.nytimes.android.sample.SampleApp; import com.nytimes.android.sample.data.model.Children; -import com.nytimes.android.sample.data.model.GsonAdaptersModel; import com.nytimes.android.sample.data.model.Post; import com.nytimes.android.sample.data.model.RedditData; -import com.nytimes.android.sample.data.remote.Api; import com.nytimes.android.sample.reddit.PostAdapter; -import java.io.IOException; import java.util.List; -import okio.BufferedSource; -import retrofit2.GsonConverterFactory; -import retrofit2.Retrofit; -import retrofit2.RxJavaCallAdapterFactory; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; @@ -40,9 +27,9 @@ import static android.widget.Toast.makeText; public class PersistingStoreActivity extends AppCompatActivity { - private Persister persister; private RecyclerView recyclerView; private PostAdapter postAdapter; + private Store persistedStore; @Override protected void onCreate(Bundle savedInstanceState) { @@ -51,25 +38,24 @@ public class PersistingStoreActivity extends AppCompatActivity { setContentView(R.layout.activity_store); setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); - try { - persister = newPersister(); - } catch (IOException exception) { - throw new RuntimeException(exception); - } - postAdapter = new PostAdapter(); recyclerView = (RecyclerView) findViewById(R.id.postRecyclerView); LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(postAdapter); + } - loadPosts(); + private void initStore() { + if (this.persistedStore == null) { + this.persistedStore = ((SampleApp) getApplicationContext()).getPersistedStore(); + } } public void loadPosts() { BarCode awwRequest = new BarCode(RedditData.class.getSimpleName(), "aww"); - provideRedditStore() + + this.persistedStore .get(awwRequest) .flatMap(this::sanitizeData) .toList() @@ -92,36 +78,10 @@ public class PersistingStoreActivity extends AppCompatActivity { .map(Children::data); } - private Store provideRedditStore() { - return StoreBuilder.parsedWithKey() - .fetcher(this::fetcher) - .persister(persister) - .parser(GsonParserFactory.createSourceParser(provideGson(), RedditData.class)) - .open(); - } - - private Persister newPersister() throws IOException { - return SourcePersisterFactory.create(getApplicationContext().getCacheDir()); - } - - private Observable fetcher(BarCode barCode) { - return provideRetrofit().fetchSubredditForPersister(barCode.getKey(), "10") - .map(responseBody -> responseBody.source()); - } - - private Api provideRetrofit() { - return new Retrofit.Builder() - .baseUrl("http://reddit.com/") - .addConverterFactory(GsonConverterFactory.create(provideGson())) - .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) - .validateEagerly(BuildConfig.DEBUG) // Fail early: check Retrofit configuration at creation time in Debug build. - .build() - .create(Api.class); - } - - Gson provideGson() { - return new GsonBuilder() - .registerTypeAdapterFactory(new GsonAdaptersModel()) - .create(); + @Override + protected void onResume() { + super.onResume(); + initStore(); + loadPosts(); } } diff --git a/app/src/main/java/com/nytimes/android/sample/activity/StoreActivity.java b/app/src/main/java/com/nytimes/android/sample/activity/StoreActivity.java index 677096a..9d4ace9 100644 --- a/app/src/main/java/com/nytimes/android/sample/activity/StoreActivity.java +++ b/app/src/main/java/com/nytimes/android/sample/activity/StoreActivity.java @@ -8,25 +8,17 @@ import android.support.v7.widget.Toolbar; import android.util.Log; import android.widget.Toast; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.nytimes.android.external.store.base.impl.Store; import com.nytimes.android.external.store.base.impl.BarCode; -import com.nytimes.android.external.store.base.impl.StoreBuilder; -import com.nytimes.android.sample.BuildConfig; +import com.nytimes.android.external.store.base.impl.Store; import com.nytimes.android.sample.R; +import com.nytimes.android.sample.SampleApp; import com.nytimes.android.sample.data.model.Children; -import com.nytimes.android.sample.data.model.GsonAdaptersModel; import com.nytimes.android.sample.data.model.Post; import com.nytimes.android.sample.data.model.RedditData; -import com.nytimes.android.sample.data.remote.Api; import com.nytimes.android.sample.reddit.PostAdapter; import java.util.List; -import retrofit2.GsonConverterFactory; -import retrofit2.Retrofit; -import retrofit2.RxJavaCallAdapterFactory; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; @@ -38,6 +30,7 @@ public class StoreActivity extends AppCompatActivity { private RecyclerView recyclerView; private PostAdapter postAdapter; + private Store nonPersistedStore; @Override protected void onCreate(Bundle savedInstanceState) { @@ -50,13 +43,18 @@ public class StoreActivity extends AppCompatActivity { layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(postAdapter); + } - loadPosts(); + private void initStore() { + if (this.nonPersistedStore == null) { + this.nonPersistedStore = ((SampleApp) getApplicationContext()).getNonPersistedStore(); + } } public void loadPosts() { BarCode awwRequest = new BarCode(RedditData.class.getSimpleName(), "aww"); - provideRedditStore() + + this.nonPersistedStore .get(awwRequest) .flatMap(this::sanitizeData) .toList() @@ -80,25 +78,10 @@ public class StoreActivity extends AppCompatActivity { .map(Children::data); } - private Store provideRedditStore() { - return StoreBuilder.barcode() - .fetcher(barCode -> provideRetrofit().fetchSubreddit(barCode.getKey(), "10")) - .open(); - } - - private Api provideRetrofit() { - return new Retrofit.Builder() - .baseUrl("http://reddit.com/") - .addConverterFactory(GsonConverterFactory.create(provideGson())) - .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) - .validateEagerly(BuildConfig.DEBUG) // Fail early: check Retrofit configuration at creation time in Debug build. - .build() - .create(Api.class); - } - - Gson provideGson() { - return new GsonBuilder() - .registerTypeAdapterFactory(new GsonAdaptersModel()) - .create(); + @Override + protected void onResume() { + super.onResume(); + initStore(); + loadPosts(); } }