Skip to content

Commit

Permalink
Moved responsibility for loading star map data from mapcore to the Gt…
Browse files Browse the repository at this point in the history
…ActionCreator
  • Loading branch information
fusion44 committed May 15, 2016
1 parent a54a9ea commit 8456845
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public interface Actions {
*/
String GET_FORUM_THREAD_POSTS = "get_forum_thread_posts";

/**
* Action id for retrieving the star map boot-up data
*/
String GET_STARMAP_BOOT_UP_DATA = "get_starmap_boot_up_data";

/**
* Fetches a single comm link
*
Expand Down Expand Up @@ -104,6 +109,11 @@ public interface Actions {
*/
void getOrganizationById(String id);

/**
* Gets basic boot-up data for displaying the starmap
*/
void getStarMapBootUpData();

/**
* Get all forum names and the basic forum data
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.stammberger.galactictavern.actions;

import com.badlogic.gdx.math.Vector2;
import com.hardsoftstudio.rxflux.action.RxAction;
import com.hardsoftstudio.rxflux.action.RxActionCreator;
import com.hardsoftstudio.rxflux.dispatcher.Dispatcher;
Expand All @@ -23,6 +24,7 @@
import me.stammberger.galactictavern.core.retrofit.ForumsApiService;
import me.stammberger.galactictavern.core.retrofit.OrganizationApiService;
import me.stammberger.galactictavern.core.retrofit.ShipApiService;
import me.stammberger.galactictavern.core.retrofit.StarMapService;
import me.stammberger.galactictavern.core.retrofit.UserApiService;
import me.stammberger.galactictavern.models.commlink.CommLinkModel;
import me.stammberger.galactictavern.models.commlink.ContentBlock2;
Expand All @@ -37,6 +39,7 @@
import me.stammberger.galactictavern.stores.ShipStore;
import me.stammberger.galactictavern.stores.db.tables.FavoritesTable;
import me.stammberger.galactictavern.stores.db.tables.user.UserSearchHistoryEntryTable;
import me.stammberger.starcitizencompact.map.data.SystemsResultset;
import rx.Observable;
import rx.Single;
import rx.android.schedulers.AndroidSchedulers;
Expand Down Expand Up @@ -305,6 +308,57 @@ public void getOrganizationById(String id) {
}));
}

/**
* {@inheritDoc}
*/
@Override
public void getStarMapBootUpData() {
RxAction action = newRxAction(Actions.GET_STARMAP_BOOT_UP_DATA);

if (hasRxAction(action)) return;

addRxAction(action, StarMapService.Factory.getInstance().getBootupData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(starMapData -> {
// calculate the star map origin (center point) for applying a scale factor later on
Vector2 origin = new Vector2();
for (SystemsResultset s : starMapData.data.systems.resultset) {
origin.add(s.positionX, s.positionY);
}

origin.x = origin.x / starMapData.data.systems.rowcount;
origin.y = origin.y / starMapData.data.systems.rowcount;
starMapData.data.origin = origin;

Vector2 dist = new Vector2();
for (SystemsResultset s : starMapData.data.systems.resultset) {
// calculate distance between starmap origin and system
dist.x = s.positionX - origin.x;
dist.y = s.positionY - origin.y;

// scale the distance by factor x
dist.scl(80);

// set systems origin to the scaled distance relative to starmap origin
s.positionX = dist.x + origin.x;
s.positionY = dist.y + origin.y;

// Add the system to a HashMap so we can retrieve single systems by id
starMapData.data.systemHashMap.put(s.id, s);

s.generateBoundingCircle();
}

action.getData().put(Keys.STARMAP_BOOTUP_DATA, starMapData);
postRxAction(action);
}, throwable -> {
Timber.d("Error getting Starmap boot up data");
Timber.d(throwable.getCause().toString());
postError(action, throwable);
}));
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@ public interface Keys {
*/
String FORUM_THREAD_POSTS_FOR_PAGE = "forum_threads_for_page";

/**
* Key for requesting {@link me.stammberger.starcitizencompact.map.data.StarMapData}
*/
String STARMAP_BOOTUP_DATA = "starmap_bootup_data";
}
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();
}
}
}
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();
}
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));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import me.stammberger.galactictavern.stores.ForumStore;
import me.stammberger.galactictavern.stores.OrganizationStore;
import me.stammberger.galactictavern.stores.ShipStore;
import me.stammberger.galactictavern.stores.StarmapStore;
import me.stammberger.galactictavern.stores.UserStore;
import me.stammberger.galactictavern.ui.commlinks.CommLinkListFragment;
import me.stammberger.galactictavern.ui.forums.ForumListFragment;
Expand Down Expand Up @@ -76,7 +77,6 @@ public class MainActivity extends AppCompatActivity
*/
private ShipStore mShipStore;


/**
* Instance of {@link UserStore} for retrieving user data
*/
Expand All @@ -87,6 +87,11 @@ public class MainActivity extends AppCompatActivity
*/
private OrganizationStore mOrganizationStore;

/**
* Instance of {@link StarmapStore} for retrieving starmap data
*/
private StarmapStore mStarmapStore;

/**
* Instance of {@link ForumStore} for retrieving forum data
*/
Expand Down Expand Up @@ -482,6 +487,16 @@ public void onRxStoreChanged(RxStoreChange change) {
f.setForums(mForumsStore.getForums());
}
}
break;
case StarmapStore.ID:
switch (change.getRxAction().getType()) {
case Actions.GET_STARMAP_BOOT_UP_DATA:
if (mCurrentFragment != null && mCurrentFragment instanceof MapFragment) {
MapFragment f = (MapFragment) mCurrentFragment;
f.setStarMapData(mStarmapStore.getBootUpData());
}
}
break;
}
}

Expand Down Expand Up @@ -518,6 +533,9 @@ public void onRxStoresRegister() {

mForumsStore = ForumStore.get(dispatcher);
mForumsStore.register();

mStarmapStore = StarmapStore.get(dispatcher);
mStarmapStore.register();
}

@Override
Expand Down
Loading

0 comments on commit 8456845

Please sign in to comment.