Skip to content

Commit

Permalink
Comm links can be favorited
Browse files Browse the repository at this point in the history
  • Loading branch information
fusion44 committed Mar 22, 2016
1 parent bc8fdbd commit b50ad1e
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .idea/dictionaries/fusion44.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface Actions {
String GET_COMM_LINK = "get_comm_link";
String GET_COMM_LINKS = "get_comm_links";
String GET_COMM_LINK_CONTENT_WRAPPERS = "get_comm_link_content_wrappers";
String COMM_LINK_DATA_UPDATED = "comm_link_data_updated";

/**
* Action id for getting all Ships
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import me.stammberger.starcitizencompact.core.retrofit.OrganizationApiService;
import me.stammberger.starcitizencompact.core.retrofit.ShipApiService;
import me.stammberger.starcitizencompact.core.retrofit.UserApiService;
import me.stammberger.starcitizencompact.models.commlink.CommLinkModel;
import me.stammberger.starcitizencompact.models.favorites.Favorite;
import me.stammberger.starcitizencompact.models.forums.Forum;
import me.stammberger.starcitizencompact.models.forums.ForumSectioned;
Expand Down Expand Up @@ -66,22 +67,28 @@ public void getCommLink(Long id) {
mCommLinkFetcher = new CommLinkFetcher();
}

addRxAction(action, mCommLinkFetcher.getCommLink(id)
getFavoritesInternal(Favorite.TYPE_COMM_LINK, String.valueOf(id))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(comm_link -> {
action.getData().put(Keys.COMM_LINK, comm_link);
postRxAction(action);
}, throwable -> {
postError(action, throwable);
}));
.subscribe(favorite -> {
addRxAction(action, mCommLinkFetcher.getCommLink(id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(comm_link -> {
action.getData().put(Keys.COMM_LINK, comm_link);
postRxAction(action);
}, throwable -> {
postError(action, throwable);
}));
});

}

/**
* Initiates the get comm link retrieval through {@link SciActionCreator#mCommLinkFetcher}
* Creates the CommLinkFetcher field and subscribes to its observable
* Once the comm links are retrieved it posts a new {@link RxAction} to update {@link CommLinkStore}
*/
@SuppressWarnings("Convert2streamapi")
@Override
public void getCommLinks() {
Timber.d("Starting fetch comm link action");
Expand All @@ -93,15 +100,24 @@ public void getCommLinks() {
mCommLinkFetcher = new CommLinkFetcher();
}

addRxAction(action, mCommLinkFetcher.getCommLinks()
getFavoritesInternal(Favorite.TYPE_COMM_LINK)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(comm_links -> {
action.getData().put(Keys.COMM_LINKS, comm_links);
postRxAction(action);
}, throwable -> {
postError(action, throwable);
}));
.subscribe(favorites -> {
addRxAction(action, mCommLinkFetcher.getCommLinks()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(comm_links -> {
for (CommLinkModel comm_link : comm_links) {
if (favorites.containsKey(String.valueOf(comm_link.getCommLinkId()))) {
comm_link.favorite = true;
}
}
action.getData().put(Keys.COMM_LINKS, comm_links);
postRxAction(action);
}, throwable -> {
postError(action, throwable);
}));
});
}

/**
Expand Down Expand Up @@ -428,6 +444,7 @@ public void addFavorite(Favorite favorite) {
.prepare()
.asRxObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(wrapperPutResults -> {
if (favorite.type == Favorite.TYPE_SHIP) {
// Update the Ship data model
Expand All @@ -442,6 +459,17 @@ public void addFavorite(Favorite favorite) {
shipList.add(ship);
a.getData().put(Keys.SHIP_DATA_LIST, shipList);
postRxAction(a);
} else if (favorite.type == Favorite.TYPE_COMM_LINK) {
CommLinkStore commLinkStore = CommLinkStore.get(SciApplication.getInstance().getRxFlux().getDispatcher());
CommLinkModel cl = commLinkStore.getCommLink(Long.valueOf(favorite.reference));
cl.favorite = true;

// update the data object
RxAction a = newRxAction(Actions.COMM_LINK_DATA_UPDATED);
ArrayList<CommLinkModel> clList = new ArrayList<>(1);
clList.add(cl);
a.getData().put(Keys.COMM_LINKS, clList);
postRxAction(a);
}
}, throwable -> {
Timber.d("Error putting reference %s to DB", favorite.reference);
Expand Down Expand Up @@ -492,7 +520,8 @@ public void removeFavorite(Favorite favorite) {
.byQuery(q)
.prepare()
.asRxSingle()
.subscribeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(deleteResult -> {
if (favorite.type == Favorite.TYPE_SHIP) {
// Update the Ship data model
Expand All @@ -507,6 +536,17 @@ public void removeFavorite(Favorite favorite) {
shipList.add(ship);
a.getData().put(Keys.SHIP_DATA_LIST, shipList);
postRxAction(a);
} else if (favorite.type == Favorite.TYPE_COMM_LINK) {
CommLinkStore commLinkStore = CommLinkStore.get(SciApplication.getInstance().getRxFlux().getDispatcher());
CommLinkModel cl = commLinkStore.getCommLink(Long.valueOf(favorite.reference));
cl.favorite = false;

// update the data object
RxAction a = newRxAction(Actions.COMM_LINK_DATA_UPDATED);
ArrayList<CommLinkModel> clList = new ArrayList<>(1);
clList.add(cl);
a.getData().put(Keys.COMM_LINKS, clList);
postRxAction(a);
}
});
}
Expand Down Expand Up @@ -538,4 +578,26 @@ private Single<HashMap<String, Favorite>> getFavoritesInternal(int type) {
return hm;
});
}

/**
* Search for a favorite when the exact reference is known
*
* @param type Type of the Favorite. For example {@link Favorite#TYPE_COMM_LINK}
* @param value Reference value of the Favorite
* @return @return An rx.Single for the favorites
*/
private Single<Favorite> getFavoritesInternal(int type, String value) {
DefaultStorIOSQLite storio = SciApplication.getInstance().getStorIOSQLite();
Query q = Query.builder()
.table(FavoritesTable.TABLE)
.where(FavoritesTable.COLUMN_TYPE + "=? and " + FavoritesTable.COLUMN_REFERENCE + "=?")
.whereArgs(type, value)
.build();

return storio.get()
.object(Favorite.class)
.withQuery(q)
.prepare()
.asRxSingle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public class CommLinkModel extends BaseModel {
@Expose
public List<Wrapper> wrappers = new ArrayList<>();

/**
* Depicts whether the user has favorited this comm link or not
*/
public boolean favorite = false;

/**
* No args constructor for use in serialization
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public void onRxAction(RxAction action) {
this.mCommLinkContentWrappers.put(commLinkId, wrappers);
}

break;
case Actions.COMM_LINK_DATA_UPDATED:
break;
default:
// return without posting a change to the store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static String getCreateTableQuery() {
+ COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY, "
+ COLUMN_DATE + " LONG INTEGER NOT NULL, "
+ COLUMN_TYPE + " INTEGER NOT NULL, "
+ COLUMN_REFERENCE + " STRING NOT NULL"
+ COLUMN_REFERENCE + " STRING UNIQUE NOT NULL"
+ ");";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.os.Build;
import android.os.Bundle;
import android.support.customtabs.CustomTabsIntent;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
Expand All @@ -21,15 +23,20 @@
import com.hardsoftstudio.rxflux.dispatcher.RxViewDispatch;
import com.hardsoftstudio.rxflux.store.RxStoreChange;

import org.joda.time.DateTime;

import java.util.List;

import me.stammberger.starcitizencompact.R;
import me.stammberger.starcitizencompact.SciApplication;
import me.stammberger.starcitizencompact.actions.Actions;
import me.stammberger.starcitizencompact.actions.Keys;
import me.stammberger.starcitizencompact.actions.SciActionCreator;
import me.stammberger.starcitizencompact.core.chrome.CustomTabActivityHelper;
import me.stammberger.starcitizencompact.core.chrome.WebviewFallback;
import me.stammberger.starcitizencompact.models.commlink.CommLinkModel;
import me.stammberger.starcitizencompact.models.commlink.Wrapper;
import me.stammberger.starcitizencompact.models.favorites.Favorite;
import me.stammberger.starcitizencompact.stores.CommLinkStore;

/**
Expand All @@ -40,6 +47,7 @@ public class CommLinkReaderActivity extends AppCompatActivity implements RxViewD
public static final String COMM_LINK_ITEM = "comm_link_item";
private CommLinkModel mCommLink;
private CommLinkStore mCommLinkStore;
private FloatingActionButton mFab;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -58,6 +66,23 @@ protected void onCreate(Bundle savedInstanceState) {
Long commLinkId = getIntent().getLongExtra(COMM_LINK_ITEM, -1);
mCommLink = mCommLinkStore.getCommLink(commLinkId);

mFab = (FloatingActionButton) findViewById(R.id.share_fab);
mFab.setOnClickListener(v -> {
SciActionCreator actionCreator = SciApplication.getInstance().getActionCreator();
Favorite f = new Favorite();
f.type = Favorite.TYPE_COMM_LINK;
f.date = DateTime.now().getMillis();
f.reference = String.valueOf(mCommLink.getCommLinkId());

if (mCommLink.favorite) {
actionCreator.removeFavorite(f);
} else {
actionCreator.addFavorite(f);
}
});

updateFab();

ImageView backdropView = (ImageView) findViewById(R.id.comm_link_backdrop);
Glide.with(this)
.load(mCommLink.getMainBackdrop())
Expand All @@ -82,11 +107,22 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

private void updateFab() {
if (mCommLink.favorite) {
mFab.setImageDrawable(ContextCompat.getDrawable(
this, R.drawable.ic_star_gold_24dp));
} else {
mFab.setImageDrawable(ContextCompat.getDrawable(
this, R.drawable.ic_star_black_24dp));
}
}

/**
* Called by RxFlux whenever a RxStore has received data.
*
* @param change The change model with the data
*/
@SuppressWarnings({"unchecked", "Convert2streamapi"})
@Override
public void onRxStoreChanged(RxStoreChange change) {
switch (change.getStoreId()) {
Expand All @@ -98,6 +134,16 @@ public void onRxStoreChanged(RxStoreChange change) {
mCommLink.setWrappers(wrappers);
setupRecyclerView();
break;
case Actions.COMM_LINK_DATA_UPDATED:
List<CommLinkModel> updatedModels =
(List<CommLinkModel>) change.getRxAction()
.getData().get(Keys.COMM_LINKS);
for (CommLinkModel updatedModel : updatedModels) {
if (updatedModel == mCommLink) {
updateFab();
}
}
break;
}
break;
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-v21/ic_star_gold_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FFFFFF00"
android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/layout/activity_comm_link_reader_native.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,14 @@

</android.support.v7.widget.CardView>
</android.support.percent.PercentFrameLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/share_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/star_off"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>
9 changes: 8 additions & 1 deletion tools/CodeSnippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@ User something like this:

return mLoggingService;
}
}
}

#### Check whether current path is executed on the MainThread
If the check returns true == running on main thread.
More info on this [post](http://stackoverflow.com/questions/11411022/how-to-check-if-current-thread-is-not-main-thread)
``` java ```

Looper.myLooper() == Looper.getMainLooper()

0 comments on commit b50ad1e

Please sign in to comment.