Skip to content

Instantly share code, notes, and snippets.

@ruan65
Created May 25, 2018 17:17
Show Gist options
  • Save ruan65/beeff935f301ff3a00367027a0282d09 to your computer and use it in GitHub Desktop.
Save ruan65/beeff935f301ff3a00367027a0282d09 to your computer and use it in GitHub Desktop.
package ru.redsys.rea_conference.network;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.util.Log;
import org.greenrobot.eventbus.EventBus;
import java.util.List;
import io.realm.Realm;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import ru.redsys.rea_conference.di.Di;
import ru.redsys.rea_conference.event_bus.EventGotAllEvents;
import ru.redsys.rea_conference.event_bus.EventGotCreds;
import ru.redsys.rea_conference.event_bus.EventGotNews;
import ru.redsys.rea_conference.event_bus.EventGotParticipants;
import ru.redsys.rea_conference.event_bus.EventGotSchedule;
import ru.redsys.rea_conference.event_bus.EventNetworkError;
import ru.redsys.rea_conference.models.conference.ConferenceSummary;
import ru.redsys.rea_conference.models.conference.MobileConference;
import ru.redsys.rea_conference.models.news.NewsItem;
import ru.redsys.rea_conference.models.participant.Creds;
import ru.redsys.rea_conference.models.participant.Participant;
import ru.redsys.rea_conference.storage.LocalStorage;
import ru.redsys.rea_conference.utils.Cv;
import ru.redsys.rea_conference.utils.helpers.H;
public class ApiCallService extends JobIntentService {
public static void getConferenceSchedule(Context context) {
Intent intent = new Intent(context, ApiCallService.class);
intent.setAction(Cv.ACTION_GET_SCHEDULE);
enqueueWork(context, ApiCallService.class, Cv.JOB, intent);
}
public static void getAllConferences(Context context) {
Intent intent = new Intent(context, ApiCallService.class);
intent.setAction(Cv.ACTION_GET_ALL_CONFERENCES);
enqueueWork(context, ApiCallService.class, Cv.JOB, intent);
}
public static void getConferenceParticipants(Context context) {
Intent intent = new Intent(context, ApiCallService.class);
intent.setAction(Cv.ACTION_GET_PARTICIPANTS);
enqueueWork(context, ApiCallService.class, Cv.JOB, intent);
}
public static void getConferenceNews(Context context) {
Intent intent = new Intent(context, ApiCallService.class);
intent.setAction(Cv.ACTION_GET_NEWS);
enqueueWork(context, ApiCallService.class, Cv.JOB, intent);
}
public static void checkParticipant(Context context, String email, String code) {
Intent intent = new Intent(context, ApiCallService.class);
intent.setAction(Cv.ACTION_CHECK_PARTICIPANT);
intent.putExtra(Cv.EXTRA_EMAIL, email);
intent.putExtra(Cv.EXTRA_CODE, code);
enqueueWork(context, ApiCallService.class, Cv.JOB, intent);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
String action = intent.getAction();
String conferenceId = H.getConferenceId(this);
if (null == action) return;
switch (action) {
case Cv.ACTION_GET_ALL_CONFERENCES:
handleGetAllConferences();
break;
case Cv.ACTION_GET_SCHEDULE:
handleGetMobileConferenceById(conferenceId);
break;
case Cv.ACTION_GET_PARTICIPANTS:
handleGetParticipants(conferenceId);
break;
case Cv.ACTION_GET_NEWS:
handleGetNews(conferenceId);
break;
case Cv.ACTION_CHECK_PARTICIPANT:
handleCheckParticipant(conferenceId,
intent.getStringExtra(Cv.EXTRA_EMAIL),
intent.getStringExtra(Cv.EXTRA_CODE));
break;
}
}
private void handleCheckParticipant(String eventId, String email, String code) {
Di.getRetrofitInstance()
.checkParticipant(new Creds(email, code, eventId))
.enqueue(new Callback<Creds>() {
@Override
public void onResponse(Call<Creds> call, Response<Creds> response) {
if (200 == response.code() && null != response.body()) {
EventBus.getDefault()
.post(new EventGotCreds(response.body()));
} else {
EventBus.getDefault()
.post(new EventNetworkError(response.message()));
}
}
@Override
public void onFailure(Call<Creds> call, Throwable t) {
t.printStackTrace();
EventBus.getDefault()
.post(new EventNetworkError(t.getMessage())); }
});
}
private void handleGetAllConferences() {
Di.getRetrofitInstance()
.getAllConferences()
.enqueue(new Callback<List<ConferenceSummary>>() {
@Override
public void onResponse(Call<List<ConferenceSummary>> call,
Response<List<ConferenceSummary>> response) {
if (200 == response.code() && null != response.body()) {
Log.d("mytag", response.body()
.toString());
EventBus.getDefault()
.post(new EventGotAllEvents(response.body()));
} else {
EventBus.getDefault()
.post(new EventNetworkError(response.message()));
}
}
@Override
public void onFailure(Call<List<ConferenceSummary>> call, Throwable t) {
EventBus.getDefault()
.post(new EventNetworkError(t.getMessage()));
}
});
}
private void handleGetNews(String conferenceId) {
Di.getRetrofitInstance()
.getConferenceNews(conferenceId)
.enqueue(new Callback<List<NewsItem>>() {
@Override
public void onResponse(Call<List<NewsItem>> call, Response<List<NewsItem>>
response) {
if (200 == response.code() && null != response.body()) {
Realm realm = Di.getRealm();
realm.beginTransaction();
realm.where(NewsItem.class)
.findAll()
.deleteAllFromRealm();
realm.insertOrUpdate(response.body());
realm.commitTransaction();
EventBus.getDefault()
.post(new EventGotNews());
} else {
EventBus.getDefault()
.post(new EventNetworkError(response.message()));
}
}
@Override
public void onFailure(Call<List<NewsItem>> call, Throwable t) {
EventBus.getDefault()
.post(new EventNetworkError(t.getMessage()));
}
});
}
private void handleGetParticipants(String conferenceId) {
Di.getRetrofitInstance()
.getConferenceParticipants(conferenceId)
.enqueue(new Callback<List<Participant>>() {
@Override
public void onResponse(@NonNull Call<List<Participant>> call,
@NonNull Response<List<Participant>> response) {
if (200 == response.code() && null != response.body()) {
Realm realm = Di.getRealm();
realm.beginTransaction();
realm.where(Participant.class)
.findAll()
.deleteAllFromRealm();
realm.insertOrUpdate(response.body());
realm.commitTransaction();
realm.close();
EventBus.getDefault()
.post(new EventGotParticipants());
} else {
EventBus.getDefault()
.post(new EventNetworkError(response.message()));
}
}
@Override
public void onFailure(Call<List<Participant>> call, Throwable t) {
EventBus.getDefault()
.post(new EventNetworkError(t.getMessage()));
}
});
}
private void handleGetMobileConferenceById(String conferenceId) {
Di.getRetrofitInstance()
.getConference(conferenceId)
.enqueue(new Callback<MobileConference>() {
@Override
public void onResponse(@NonNull Call<MobileConference> call,
@NonNull Response<MobileConference> response) {
if (200 == response.code()) {
MobileConference mobileConference = response.body();
LocalStorage.saveConference(ApiCallService.this, mobileConference);
EventBus.getDefault()
.post(new EventGotSchedule());
if (null != mobileConference && null != mobileConference.majorSections
&& 0 != mobileConference.majorSections.size()) {
mobileConference.saveSessionSummaryToRealm();
}
} else {
EventBus.getDefault()
.post(new EventNetworkError(response.message()));
}
}
@Override
public void onFailure(Call<MobileConference> call, Throwable t) {
EventBus.getDefault()
.post(new EventNetworkError(t.getMessage()));
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment