-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved responsibility for loading star map data from mapcore to the Gt…
…ActionCreator
- Loading branch information
Showing
12 changed files
with
329 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/src/main/java/me/stammberger/galactictavern/core/retrofit/StarMapService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package me.stammberger.galactictavern.core.retrofit; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import me.stammberger.starcitizencompact.map.data.StarMapData; | ||
import me.stammberger.starcitizencompact.map.data.Thumbnail; | ||
import retrofit2.Retrofit; | ||
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
import retrofit2.http.GET; | ||
import rx.Observable; | ||
|
||
/** | ||
* Retrofit interface for getting data related to the star map | ||
*/ | ||
public interface StarMapService { | ||
String BASE_URL = "http://fusion44.bitbucket.org"; | ||
|
||
/** | ||
* Gets the base starmap boot-up data | ||
* | ||
* @return Observable which will be called once done loading | ||
*/ | ||
@GET("/sci/starmap/bootup.json") | ||
Observable<StarMapData> getBootupData(); | ||
|
||
/** | ||
* Factory class to create and store the singleton | ||
*/ | ||
class Factory { | ||
private static StarMapService mService; | ||
|
||
/** | ||
* Get the service instance without logging enabled | ||
* | ||
* @return the api service without logging | ||
*/ | ||
public static synchronized StarMapService getInstance() { | ||
if (mService == null) { | ||
GsonBuilder builder = new GsonBuilder(); | ||
builder.registerTypeAdapter(Thumbnail.class, new ThumbnailsDeserializer()); | ||
Gson gson = builder.create(); | ||
|
||
Retrofit retrofit = | ||
new Retrofit.Builder() | ||
.addConverterFactory(GsonConverterFactory.create(gson)) | ||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) | ||
.baseUrl(BASE_URL) | ||
.build(); | ||
mService = retrofit.create(StarMapService.class); | ||
|
||
} | ||
return mService; | ||
} | ||
} | ||
|
||
class ThumbnailsDeserializer implements JsonDeserializer<Thumbnail> { | ||
@Override | ||
public Thumbnail deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
throws JsonParseException { | ||
// TODO: handle gracefully. For example add a default image. | ||
return new Thumbnail(); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/me/stammberger/galactictavern/stores/StarMapStoreInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package me.stammberger.galactictavern.stores; | ||
|
||
import me.stammberger.starcitizencompact.map.data.StarMapData; | ||
|
||
/** | ||
* Interface for the starmap store | ||
*/ | ||
public interface StarMapStoreInterface { | ||
|
||
/** | ||
* @return {@link me.stammberger.starcitizencompact.map.data.StarMapData} object with boot up data | ||
*/ | ||
StarMapData getBootUpData(); | ||
} |
80 changes: 80 additions & 0 deletions
80
app/src/main/java/me/stammberger/galactictavern/stores/StarmapStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package me.stammberger.galactictavern.stores; | ||
|
||
import com.hardsoftstudio.rxflux.action.RxAction; | ||
import com.hardsoftstudio.rxflux.dispatcher.Dispatcher; | ||
import com.hardsoftstudio.rxflux.store.RxStore; | ||
import com.hardsoftstudio.rxflux.store.RxStoreChange; | ||
|
||
import me.stammberger.galactictavern.actions.Actions; | ||
import me.stammberger.galactictavern.actions.Keys; | ||
import me.stammberger.starcitizencompact.map.data.StarMapData; | ||
|
||
/** | ||
* Stores the star map data once they've been loaded from the API | ||
* To adhere to the Flux contract loading must be triggered by the view: | ||
* https://raw.githubusercontent.com/lgvalle/lgvalle.github.io/master/public/images/flux-graph-complete.png | ||
* <p> | ||
* {@link RxStore#postChange(RxStoreChange)} will update the listener classes. Called in {@link StarmapStore#onRxAction(RxAction)} | ||
* <p> | ||
* This is a Singleton class | ||
*/ | ||
public class StarmapStore extends RxStore implements StarMapStoreInterface { | ||
public static final String ID = "StarmapStore"; | ||
private static StarmapStore mInstance; | ||
private StarMapData mBootUpData; | ||
|
||
/** | ||
* Private constructor. Use {@link #get(Dispatcher)} to retrieve an instance | ||
* | ||
* @param dispatcher The RxFlux dispatcher | ||
*/ | ||
private StarmapStore(Dispatcher dispatcher) { | ||
super(dispatcher); | ||
mBootUpData = new StarMapData(); | ||
} | ||
|
||
/** | ||
* Creates the singleton instance | ||
* | ||
* @param dispatcher RxFlux dispatcher | ||
* @return The StarmapStore instance | ||
*/ | ||
public static synchronized StarmapStore get(Dispatcher dispatcher) { | ||
if (mInstance == null) { | ||
mInstance = new StarmapStore(dispatcher); | ||
} | ||
|
||
return mInstance; | ||
} | ||
|
||
/** | ||
* Gets the boot up data | ||
* | ||
* @return {@link StarMapData} object with the boot up data. Empty if data hasn't been loaded yet. | ||
*/ | ||
@Override | ||
public StarMapData getBootUpData() { | ||
return mBootUpData; | ||
} | ||
|
||
/** | ||
* Method is called when the loading action has finished running. | ||
* | ||
* @param action RxAction that has finished loading. Must contain the {@link StarMapData} object. | ||
*/ | ||
@Override | ||
public void onRxAction(RxAction action) { | ||
switch (action.getType()) { | ||
case Actions.GET_STARMAP_BOOT_UP_DATA: | ||
mBootUpData = (StarMapData) action.getData().get(Keys.STARMAP_BOOTUP_DATA); | ||
break; | ||
default: | ||
// return without posting a change to the store. | ||
// The data we want wasn't contained in the action | ||
return; | ||
} | ||
|
||
postChange(new RxStoreChange(ID, action)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.