Skip to content

Commit

Permalink
Add initial changes for v2 API support wordpress-mobile#72
Browse files Browse the repository at this point in the history
  • Loading branch information
aagam-shah committed Jul 17, 2017
1 parent 9a7f60b commit 09212ab
Show file tree
Hide file tree
Showing 27 changed files with 1,158 additions and 1,210 deletions.
14 changes: 7 additions & 7 deletions app/src/main/java/org/wordcamp/android/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import org.wordcamp.android.networking.ResponseListener;
import org.wordcamp.android.networking.WPAPIClient;
import org.wordcamp.android.objects.WordCampDB;
import org.wordcamp.android.objects.wordcamp.WordCampNew;
import org.wordcamp.android.objects.wordcampv2.WordCamp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -197,19 +196,20 @@ public void onErrorResponse(VolleyError error) {
public void onResponseReceived(Object o) {
try {
wordCampsList = new ArrayList<>();
WordCampNew[] wordCampNews = (WordCampNew[]) o;
for (WordCampNew wordcamp : wordCampNews) {
WordCamp[] wordCamps = (WordCamp[]) o;
for (WordCamp wordCamp : wordCamps) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
Date d = sdf.parse(wordcamp.getModifiedGmt());
WordCampDB wordCampDB = new WordCampDB(wordcamp, d.toString());
Date d = sdf.parse(wordCamp.getModifiedGmt());
WordCampDB wordCampDB = new WordCampDB(wordCamp, d.toString());
if (!wordCampDB.getWc_start_date().isEmpty()) {
wordCampsList.add(wordCampDB);
}
}

communicator.addAllNewWC(wordCampsList);
refreshAllFragmentsData();
stopRefresh();
} catch (ParseException e) {
} catch (Exception e) {
e.printStackTrace();
stopRefresh();
}
Expand Down
19 changes: 10 additions & 9 deletions app/src/main/java/org/wordcamp/android/WordCampDetailActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
Expand All @@ -21,9 +22,9 @@
import org.wordcamp.android.networking.ResponseListener;
import org.wordcamp.android.networking.WPAPIClient;
import org.wordcamp.android.objects.WordCampDB;
import org.wordcamp.android.objects.speaker.Session;
import org.wordcamp.android.objects.speaker.SpeakerNew;
import org.wordcamp.android.objects.wordcamp.WordCampNew;
import org.wordcamp.android.objects.wordcampv2.SessionV2;
import org.wordcamp.android.objects.wordcampv2.Speaker;
import org.wordcamp.android.objects.wordcampv2.WordCamp;
import org.wordcamp.android.wcdetails.MySessionsActivity;
import org.wordcamp.android.wcdetails.SessionsFragment;
import org.wordcamp.android.wcdetails.SpeakerFragment;
Expand All @@ -48,7 +49,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initListener();
wcdb = (WordCampDB) getIntent().getSerializableExtra("wc");
wcid = wcdb.getWc_id();
wcid = (int) wcdb.getWc_id();
setContentView(R.layout.activity_wordcamp_detail);
communicator = new DBCommunicator(this);
communicator.start();
Expand Down Expand Up @@ -79,7 +80,7 @@ private void initGUI() {
}

private void setToolbar() {
toolbar.setTitle(wcdb.getWc_title());
toolbar.setTitle(Html.fromHtml(wcdb.getWc_title()));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Expand Down Expand Up @@ -236,7 +237,7 @@ public void onErrorResponse(VolleyError error) {

@Override
public void onResponseReceived(Object o) {
SpeakerNew[] speakerNews = (SpeakerNew[]) o;
Speaker[] speakerNews = (Speaker[]) o;

for (int i = 0; i < speakerNews.length; i++) {
try {
Expand Down Expand Up @@ -288,8 +289,8 @@ public void onErrorResponse(VolleyError error) {
scheduleResponseListener = new ResponseListener() {
@Override
public void onResponseReceived(Object o) {
Session[] sessions = (Session[]) o;
for (Session session : sessions) {
SessionV2[] sessions = (SessionV2[]) o;
for (SessionV2 session : sessions) {
communicator.addSession(session, wcid);
}
Toast.makeText(getApplicationContext(), getString(R.string.update_sessions_toast), Toast.LENGTH_SHORT).show();
Expand All @@ -303,7 +304,7 @@ public void onResponseReceived(Object o) {
wcResponseListener = new ResponseListener() {
@Override
public void onResponseReceived(Object o) {
WordCampNew wordCamp = (WordCampNew) o;
WordCamp wordCamp = (WordCamp) o;
WordCampDB wordCampDB = new WordCampDB(wordCamp, "");
communicator.updateWC(wordCampDB);
WordCampOverview overview = getOverViewFragment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@ public class SessionsListAdapter extends BaseAdapter implements StickyListHeader
private Context ctx;
private LayoutInflater inflater;
private OnAddToMySessionListener listener;
private int singleLineSize, doubleLineSize;

public SessionsListAdapter(Context ctx, List<SessionDB> dbList, HashMap<Integer, String> speakersForSession, OnAddToMySessionListener listener) {
this.ctx = ctx;
this.listener = listener;
this.list = dbList;
this.speakersForSession = speakersForSession;
inflater = LayoutInflater.from(ctx);
singleLineSize = (int) ctx.getResources().getDimension(R.dimen.single_line_list_item);
doubleLineSize = (int) ctx.getResources().getDimension(R.dimen.double_line_list_item);
}

@Override
Expand All @@ -61,8 +58,8 @@ public View getView(final int position, View convertView, ViewGroup parent) {
holder.location.setText(Html.fromHtml(sessionDB.getLocation()));
if (speakersForSession.containsKey(sessionDB.getPost_id())) {
holder.speakers.setVisibility(View.VISIBLE);
holder.speakers.setText(ctx.getString(R.string.separator)
+ speakersForSession.get(sessionDB.getPost_id()));
holder.speakers.setText(String.format("%s%s", ctx.getString(R.string.separator)
, speakersForSession.get(sessionDB.getPost_id())));
} else {
holder.speakers.setVisibility(View.GONE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -57,7 +58,7 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public void onBindViewHolder(final ViewHolder holder, final int position) {

final WordCampDB wc = filteredWordCamps.get(position);
holder.title.setText(wc.getWc_title());
holder.title.setText(Html.fromHtml(wc.getWc_title()));
holder.date.setText(WordCampUtils.getProperDate(wc));
if (wc.isMyWC) {
Picasso.with(ctx).load(R.drawable.ic_favorite_solid_24dp).into(holder.bookmark);
Expand All @@ -69,15 +70,15 @@ public void onBindViewHolder(final ViewHolder holder, final int position) {
@Override
public void onClick(View v) {
if (!wc.isMyWC) {
listener.addToMyWC(wc.getWc_id(), position);
listener.addToMyWC((int) wc.getWc_id(), position);
Picasso.with(ctx).load(R.drawable.ic_favorite_solid_24dp).into(holder.bookmark);
wc.isMyWC = true;
filteredWordCamps.set(position, wc);
} else {
Picasso.with(ctx).load(R.drawable.ic_favorite_outline_24dp).into(holder.bookmark);
wc.isMyWC = false;
filteredWordCamps.set(position, wc);
listener.removeMyWC(wc.getWc_id(), position);
listener.removeMyWC((int) wc.getWc_id(), position);
}
}
});
Expand Down
81 changes: 30 additions & 51 deletions app/src/main/java/org/wordcamp/android/db/DBCommunicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import org.wordcamp.android.objects.SessionDB;
import org.wordcamp.android.objects.SpeakerDB;
import org.wordcamp.android.objects.WordCampDB;
import org.wordcamp.android.objects.speaker.Session;
import org.wordcamp.android.objects.speaker.SpeakerNew;
import org.wordcamp.android.utils.WordCampUtils;
import org.wordcamp.android.objects.wordcampv2.SessionV2;
import org.wordcamp.android.objects.wordcampv2.Speaker;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -23,6 +22,8 @@
import java.util.List;
import java.util.Map;

import static org.wordcamp.android.utils.WCConstants.GRAVATAR_DEFAULT_SIZE;

/**
* Created by aagam on 27/1/15.
*/
Expand Down Expand Up @@ -126,87 +127,65 @@ public void removeFromMyWCSingle(int wcid) {
new String[]{String.valueOf(wcid)});
}

public long addSpeaker(SpeakerNew sk, int wcid) {
public long addSpeaker(Speaker sk, int wcid) {
ContentValues contentValues = new ContentValues();
contentValues.put("wcid", wcid);
contentValues.put("name", sk.getTitle());
contentValues.put("speaker_bio", sk.getContent());
contentValues.put("postid", sk.getID());
contentValues.put("speaker_id", sk.getID());
contentValues.put("name", sk.getTitle().getRendered());
contentValues.put("speaker_bio", sk.getContent().getRendered());
contentValues.put("postid", sk.getId());
contentValues.put("speaker_id", sk.getId());
contentValues.put("gsonobject", gson.toJson(sk));
contentValues.put("gravatar", sk.getAvatar().equals("") ? "null" :
sk.getAvatar().substring(0, sk.getAvatar().length() - 5) + "?s=120");
String avatarUrl = sk.getAvatarUrls().get96();

contentValues.put("gravatar", avatarUrl.equals("") ? "null" :
avatarUrl.replace("s=96", "s=" + GRAVATAR_DEFAULT_SIZE));

long id = db.insertWithOnConflict("speaker", null, contentValues, SQLiteDatabase.CONFLICT_IGNORE);

if (id == -1) {
id = db.update("speaker", contentValues, " wcid = ? AND postid = ?",
new String[]{String.valueOf(wcid), String.valueOf(sk.getID())});
new String[]{String.valueOf(wcid), String.valueOf(sk.getId())});
}

if (sk.getSessions().size() > 0)
addSessionFromSpeaker(sk.getSessions(), sk.getID(), wcid);
if (sk.getEmbedded().getSessions().size() > 0)
addSessionFromSpeaker(sk.getEmbedded().getSessions(), sk.getId(), wcid);

return id;
}

private void addSessionFromSpeaker(List<Session> sessions, int spid, int wcid) {
private void addSessionFromSpeaker(List<SessionV2> sessions, long spid, int wcid) {
for (int i = 0; i < sessions.size(); i++) {
Session ss = sessions.get(i);

/*HashMap<String, String> map = WordCampUtils.getTimeAndTypeSession(ss);
ContentValues contentValues = new ContentValues();
contentValues.put("wcid", wcid);
contentValues.put("title", ss.getTitle());
contentValues.put("time", map.get("_wcpt_session_time"));
contentValues.put("postid", ss.getID());
if (ss.getTerms()!=null && ss.getTerms().getWcbTrack().size() == 1)
contentValues.put("location", ss.getTerms().getWcbTrack().get(0).getName());
contentValues.put("category", map.get("_wcpt_session_type"));
contentValues.put("gsonobject", gson.toJson(ss));
long id = db.insertWithOnConflict("session", null, contentValues, SQLiteDatabase.CONFLICT_IGNORE);
if (id == -1) {
contentValues.remove("wcid");
contentValues.remove("postid");
id = db.update("session", contentValues, " wcid = ? AND postid = ?",
new String[]{String.valueOf(wcid), String.valueOf(ss.getID())});
}*/
mapSessionToSingleSpeaker(wcid, ss.getID(), spid);
SessionV2 ss = sessions.get(i);
mapSessionToSingleSpeaker(wcid, ss.getId(), spid);
}
}

private void mapSessionToSingleSpeaker(int wcid, int sid, int spid) {
private void mapSessionToSingleSpeaker(int wcid, long sid, long spid) {
ContentValues values = new ContentValues();
values.put("wcid", wcid);
values.put("sessionid", sid);
values.put("speakerid", spid);

long id = db.insertWithOnConflict("speakersessions", null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.insertWithOnConflict("speakersessions", null, values, SQLiteDatabase.CONFLICT_REPLACE);
}


public long addSession(org.wordcamp.android.objects.speaker.Session ss, int wcid) {
public long addSession(SessionV2 ss, int wcid) {
ContentValues contentValues = new ContentValues();
contentValues.put("wcid", wcid);
contentValues.put("title", ss.getTitle());
HashMap<String, String> map = WordCampUtils.getTimeAndTypeSession(ss);
contentValues.put("title", ss.getTitle().getRendered());

//Currently we are adding the sessions which are not getting fetched by Speakers API


contentValues.put("time", map.get("_wcpt_session_time"));
contentValues.put("time", ss.getMeta().getWcptSessionTime());

contentValues.put("postid", ss.getID());
if (ss.getTerms() != null && ss.getTerms().getWcbTrack().size() == 1)
contentValues.put("location", ss.getTerms().getWcbTrack().get(0).getName());
contentValues.put("postid", ss.getId());
if (ss.getEmbedded().getWpTerm() != null && ss.getEmbedded().getWpTerm().size() > 0
&& ss.getEmbedded().getWpTerm().get(ss.getEmbedded().getWpTerm().size()-1).size() > 0)
contentValues.put("location", ss.getEmbedded().getWpTerm().get(ss.getEmbedded().getWpTerm().size()-1).get(0).getName());

contentValues.put("category", map.get("_wcpt_session_type"));
contentValues.put("category", ss.getMeta().getWcptSessionType());
contentValues.put("gsonobject", gson.toJson(ss));

long id = db.insertWithOnConflict("session", null, contentValues, SQLiteDatabase.CONFLICT_IGNORE);
Expand All @@ -215,7 +194,7 @@ public long addSession(org.wordcamp.android.objects.speaker.Session ss, int wcid
contentValues.remove("wcid");
contentValues.remove("postid");
id = db.update("session", contentValues, " wcid = ? AND postid = ?",
new String[]{String.valueOf(wcid), String.valueOf(ss.getID())});
new String[]{String.valueOf(wcid), String.valueOf(ss.getId())});
}

return id;
Expand Down
30 changes: 15 additions & 15 deletions app/src/main/java/org/wordcamp/android/networking/WPAPIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@

import org.wordcamp.android.BuildConfig;
import org.wordcamp.android.WordCampApplication;
import org.wordcamp.android.objects.speaker.Session;
import org.wordcamp.android.objects.speaker.SpeakerNew;
import org.wordcamp.android.objects.wordcamp.WordCampNew;
import org.wordcamp.android.objects.wordcampv2.SessionV2;
import org.wordcamp.android.objects.wordcampv2.Speaker;
import org.wordcamp.android.objects.wordcampv2.WordCamp;

/**
* Created by aagam on 14/1/15.
*/
public class WPAPIClient {

private static final String QUERY_PARAM_SPEAKERS = "wp-json/posts?type=wcb_speaker&filter[posts_per_page]=100";
private static final String QUERY_PARAM_SPEAKERS_V2 = "wp-json/wp/v2/speakers?per_page=100&_embed=true&status=publish";

private static final String QUERY_PARAM_WORDCAMP_LIST = "wp-json/posts?type=wordcamp&filter[order]=DESC&filter[posts_per_page]=50";
private static final String QUERY_PARAM_WORDCAMP_LIST_V2 = "wp-json/wp/v2/wordcamps?per_page=100&status=wcpt-scheduled";

private static final String QUERY_PARAM_SCHEDULE = "wp-json/posts?type=wcb_session&filter[order]=DESC&filter[orderby]=modified&filter[posts_per_page]=100";
private static final String QUERY_PARAM_SCHEDULE_V2 = "wp-json/wp/v2/sessions?per_page=100&_embed=true&status=publish";

private static final String QUERY_PARAM_SINGLEWC = "wp-json/posts/";
private static final String QUERY_PARAM_SINGLEWC_V2 = "wp-json/wp/v2/wordcamps/";

private static String normalizeWordCampUrl(String wordcampURL) {
if (!wordcampURL.endsWith("/")) {
Expand All @@ -32,8 +32,8 @@ private static String normalizeWordCampUrl(String wordcampURL) {

public static void getAllWCs(Context context, Response.ErrorListener errorListener,
ResponseListener responseListener){
WCRequest request = new WCRequest(BuildConfig.CENTRAL_WORDCAMP_URL + QUERY_PARAM_WORDCAMP_LIST,
WordCampNew[].class, errorListener, responseListener);
WCRequest request = new WCRequest(BuildConfig.CENTRAL_WORDCAMP_URL + QUERY_PARAM_WORDCAMP_LIST_V2,
WordCamp[].class, errorListener, responseListener);
request.setTag(context);
WordCampApplication.requestQueue.cancelAll(context);
WordCampApplication.requestQueue.add(request);
Expand All @@ -42,24 +42,24 @@ public static void getAllWCs(Context context, Response.ErrorListener errorListen

public static void getWordCampSpeakersVolley(String wordcampURL, Context context, Response.ErrorListener errorListener,
ResponseListener responseListener){
WCRequest request = new WCRequest(normalizeWordCampUrl(wordcampURL) + QUERY_PARAM_SPEAKERS,
SpeakerNew[].class, errorListener, responseListener);
WCRequest request = new WCRequest(normalizeWordCampUrl(wordcampURL) + QUERY_PARAM_SPEAKERS_V2,
Speaker[].class, errorListener, responseListener);
request.setTag(context);
WordCampApplication.requestQueue.add(request);
}

public static void getWordCampScheduleVolley(String wordcampURL, Context context, Response.ErrorListener errorListener,
ResponseListener responseListener){
WCRequest request = new WCRequest(normalizeWordCampUrl(wordcampURL) + QUERY_PARAM_SCHEDULE,
Session[].class, errorListener, responseListener);
WCRequest request = new WCRequest(normalizeWordCampUrl(wordcampURL) + QUERY_PARAM_SCHEDULE_V2,
SessionV2[].class, errorListener, responseListener);
request.setTag(context);
WordCampApplication.requestQueue.add(request);
}

public static void getSingleWCVolley(int wcid, Context context, Response.ErrorListener errorListener,
ResponseListener responseListener){
WCRequest request = new WCRequest(BuildConfig.CENTRAL_WORDCAMP_URL + QUERY_PARAM_SINGLEWC + wcid,
WordCampNew.class, errorListener, responseListener);
WCRequest request = new WCRequest(BuildConfig.CENTRAL_WORDCAMP_URL + QUERY_PARAM_SINGLEWC_V2 + wcid,
WordCamp.class, errorListener, responseListener);
request.setTag(context);
WordCampApplication.requestQueue.add(request);
}
Expand Down
Loading

0 comments on commit 09212ab

Please sign in to comment.