Fix sample app to init Store on Application creation
This commit is contained in:
parent
d6ed9c7916
commit
9f8e88e027
4 changed files with 132 additions and 88 deletions
|
@ -8,9 +8,10 @@
|
|||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
android:name=".SampleApp"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity
|
||||
android:name=".activity.PersistingStoreActivity">
|
||||
android:name=".activity.StoreActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
|
||||
|
|
100
app/src/main/java/com/nytimes/android/sample/SampleApp.java
Normal file
100
app/src/main/java/com/nytimes/android/sample/SampleApp.java
Normal file
|
@ -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<RedditData, BarCode> nonPersistedStore;
|
||||
private Store<RedditData, BarCode> persistedStore;
|
||||
private Persister<BufferedSource, BarCode> 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<RedditData, BarCode> getNonPersistedStore() {
|
||||
return this.nonPersistedStore;
|
||||
}
|
||||
|
||||
public Store<RedditData, BarCode> getPersistedStore() {
|
||||
return this.persistedStore;
|
||||
}
|
||||
|
||||
private Store<RedditData, BarCode> provideRedditStore() {
|
||||
return StoreBuilder.<RedditData>barcode()
|
||||
.fetcher(barCode -> provideRetrofit().fetchSubreddit(barCode.getKey(), "10"))
|
||||
.memory(CacheBuilder.newBuilder()
|
||||
.maximumSize(1)
|
||||
.expireAfterWrite(10, TimeUnit.SECONDS)
|
||||
.build())
|
||||
.open();
|
||||
}
|
||||
|
||||
private Store<RedditData, BarCode> providePersistedRedditStore() {
|
||||
return StoreBuilder.<BarCode, BufferedSource, RedditData>parsedWithKey()
|
||||
.fetcher(this::fetcher)
|
||||
.persister(persister)
|
||||
.parser(GsonParserFactory.createSourceParser(provideGson(), RedditData.class))
|
||||
.open();
|
||||
}
|
||||
|
||||
private Persister<BufferedSource, BarCode> newPersister() throws IOException {
|
||||
return SourcePersisterFactory.create(getApplicationContext().getCacheDir());
|
||||
}
|
||||
|
||||
private Observable<BufferedSource> 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();
|
||||
}
|
||||
}
|
|
@ -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<BufferedSource, BarCode> persister;
|
||||
private RecyclerView recyclerView;
|
||||
private PostAdapter postAdapter;
|
||||
private Store<RedditData, BarCode> 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<RedditData, BarCode> provideRedditStore() {
|
||||
return StoreBuilder.<BarCode, BufferedSource, RedditData>parsedWithKey()
|
||||
.fetcher(this::fetcher)
|
||||
.persister(persister)
|
||||
.parser(GsonParserFactory.createSourceParser(provideGson(), RedditData.class))
|
||||
.open();
|
||||
}
|
||||
|
||||
private Persister<BufferedSource, BarCode> newPersister() throws IOException {
|
||||
return SourcePersisterFactory.create(getApplicationContext().getCacheDir());
|
||||
}
|
||||
|
||||
private Observable<BufferedSource> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<RedditData, BarCode> 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<RedditData, BarCode> provideRedditStore() {
|
||||
return StoreBuilder.<RedditData>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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue