use of org.fossasia.openevent.data.repository.RealmDataRepository in project open-event-android by fossasia.
the class MainActivity method handleJsonEvent.
@Subscribe
public void handleJsonEvent(final JsonReadEvent jsonReadEvent) {
final String name = jsonReadEvent.getName();
final String json = jsonReadEvent.getJson();
disposable.add(Completable.fromAction(() -> {
ObjectMapper objectMapper = APIClient.getObjectMapper();
// Need separate instance for background thread
Realm realm = Realm.getDefaultInstance();
RealmDataRepository realmDataRepository = RealmDataRepository.getInstance(realm);
switch(name) {
case ConstantStrings.EVENT:
{
Event event = objectMapper.readValue(json, Event.class);
saveEventDates(event);
realmDataRepository.saveEvent(event).subscribe();
realmDataRepository.saveEvent(event).subscribe();
StrategyRegistry.getInstance().getEventBusStrategy().postEventOnUIThread(new EventDownloadEvent(true));
break;
}
case ConstantStrings.TRACKS:
{
List<Track> tracks = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, Track.class));
realmDataRepository.saveTracks(tracks).subscribe();
StrategyRegistry.getInstance().getEventBusStrategy().postEventOnUIThread(new TracksDownloadEvent(true));
break;
}
case ConstantStrings.SESSIONS:
{
List<Session> sessions = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, Session.class));
for (Session current : sessions) {
current.setStartDate(current.getStartsAt().split("T")[0]);
}
realmDataRepository.saveSessions(sessions).subscribe();
StrategyRegistry.getInstance().getEventBusStrategy().postEventOnUIThread(new SessionDownloadEvent(true));
break;
}
case ConstantStrings.SPEAKERS:
{
List<Speaker> speakers = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, Speaker.class));
realmRepo.saveSpeakers(speakers).subscribe();
StrategyRegistry.getInstance().getEventBusStrategy().postEventOnUIThread(new SpeakerDownloadEvent(true));
break;
}
case ConstantStrings.SPONSORS:
{
List<Sponsor> sponsors = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, Sponsor.class));
realmRepo.saveSponsors(sponsors).subscribe();
StrategyRegistry.getInstance().getEventBusStrategy().postEventOnUIThread(new SponsorDownloadEvent(true));
break;
}
case ConstantStrings.MICROLOCATIONS:
{
List<Microlocation> microlocations = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, Microlocation.class));
realmRepo.saveLocations(microlocations).subscribe();
StrategyRegistry.getInstance().getEventBusStrategy().postEventOnUIThread(new MicrolocationDownloadEvent(true));
break;
}
case ConstantStrings.SESSION_TYPES:
{
List<SessionType> sessionTypes = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, SessionType.class));
realmRepo.saveSessionTypes(sessionTypes).subscribe();
StrategyRegistry.getInstance().getEventBusStrategy().postEventOnUIThread(new SessionTypesDownloadEvent(true));
break;
}
default:
}
realm.close();
}).observeOn(Schedulers.computation()).subscribe(() -> Timber.d("Saved event from JSON"), throwable -> {
throwable.printStackTrace();
Timber.e(throwable);
StrategyRegistry.getInstance().getEventBusStrategy().postEventOnUIThread(new RetrofitError(throwable));
}));
}
use of org.fossasia.openevent.data.repository.RealmDataRepository in project open-event-android by fossasia.
the class DayScheduleViewHolder method bindSession.
public void bindSession(RealmDataRepository realmRepo) {
String startTimeText = DateConverter.formatDateWithDefault(DateConverter.FORMAT_24H, session.getStartsAt());
String endTimeText = DateConverter.formatDateWithDefault(DateConverter.FORMAT_24H, session.getEndsAt());
String title = Utils.checkStringEmpty(session.getTitle());
startTime.setText(startTimeText);
endTime.setText(endTimeText);
slotTitle.setText(title);
Track sessionTrack = session.getTrack();
if (!RealmDataRepository.isNull(sessionTrack)) {
int storedColor = Color.parseColor(sessionTrack.getColor());
slotTrack.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
slotTrack.setBackground(context.getDrawable(R.drawable.button_ripple));
}
slotTrack.getBackground().setColorFilter(storedColor, PorterDuff.Mode.SRC_ATOP);
slotTrack.setText(sessionTrack.getName());
if (session.getIsBookmarked()) {
slotBookmark.setImageResource(R.drawable.ic_bookmark_white_24dp);
} else {
slotBookmark.setImageResource(R.drawable.ic_bookmark_border_white_24dp);
}
slotBookmark.setColorFilter(storedColor, PorterDuff.Mode.SRC_ATOP);
final int sessionId = session.getId();
slotBookmark.setOnClickListener(v -> {
if (session.getIsBookmarked()) {
realmRepo.setBookmark(sessionId, false).subscribe();
slotBookmark.setImageResource(R.drawable.ic_bookmark_border_white_24dp);
if (onBookmarkSelectedListener != null)
onBookmarkSelectedListener.showSnackbar(new BookmarkStatus(storedColor, sessionId, CODE_UNDO_REMOVED));
} else {
// TODO: Move all logic to ViewModel
NotificationUtil.createNotification(session, context).subscribe(() -> {
if (onBookmarkSelectedListener != null)
onBookmarkSelectedListener.showSnackbar(new BookmarkStatus(storedColor, sessionId, CODE_UNDO_ADDED));
}, throwable -> {
if (onBookmarkSelectedListener != null)
onBookmarkSelectedListener.showSnackbar(new BookmarkStatus(-1, -1, CODE_ERROR));
});
realmRepo.setBookmark(sessionId, true).subscribe();
slotBookmark.setImageResource(R.drawable.ic_bookmark_white_24dp);
slotBookmark.setColorFilter(storedColor, PorterDuff.Mode.SRC_ATOP);
}
WidgetUpdater.updateWidget(context);
});
slotTrack.setOnClickListener(v -> {
Intent intent = new Intent(context, TrackSessionsActivity.class);
intent.putExtra(ConstantStrings.TRACK, sessionTrack.getName());
intent.putExtra(ConstantStrings.TRACK_ID, sessionTrack.getId());
context.startActivity(intent);
});
itemView.setOnClickListener(v -> {
final String sessionName = session.getTitle();
realmRepo.getTrack(session.getTrack().getId()).addChangeListener((RealmChangeListener<Track>) track -> {
String trackName = track.getName();
Intent intent = new Intent(context, SessionDetailActivity.class);
intent.putExtra(ConstantStrings.SESSION, sessionName);
intent.putExtra(ConstantStrings.TRACK, trackName);
intent.putExtra(ConstantStrings.ID, session.getId());
context.startActivity(intent);
});
});
} else {
slotTrack.setOnClickListener(null);
slotTrack.setVisibility(View.GONE);
itemView.setOnClickListener(v -> {
final String sessionName = session.getTitle();
Intent intent = new Intent(context, SessionDetailActivity.class);
intent.putExtra(ConstantStrings.SESSION, sessionName);
intent.putExtra(ConstantStrings.ID, session.getId());
context.startActivity(intent);
});
Timber.d("This session has no track somehow : " + session + " " + sessionTrack);
}
if (session.getMicrolocation() != null) {
String locationName = Utils.checkStringEmpty(session.getMicrolocation().getName());
slotLocation.setText(locationName);
}
}
Aggregations