Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.webinar.mobileV2.screen.event;
- import android.os.Handler;
- import android.util.Log;
- import android.widget.Toast;
- import com.google.firebase.crash.FirebaseCrash;
- import org.greenrobot.eventbus.EventBus;
- import org.greenrobot.eventbus.Subscribe;
- import java.util.List;
- import java.util.Queue;
- import ru.webinar.mobileV2.client.WebClient;
- import ru.webinar.mobileV2.client.api.RecordApi;
- import ru.webinar.mobileV2.client.callback.GetRecordHandler;
- import ru.webinar.mobileV2.comet.CometHandler;
- import ru.webinar.mobileV2.comet.events.Comet;
- import ru.webinar.mobileV2.comet.events.ConferenceAddEvent;
- import ru.webinar.mobileV2.comet.events.ConferenceUpdateEvent;
- import ru.webinar.mobileV2.comet.events.EventSessionStopEvent;
- import ru.webinar.mobileV2.comet.events.MediaSessionAddEvent;
- import ru.webinar.mobileV2.comet.events.MediaSessionUpdateEvent;
- import ru.webinar.mobileV2.comet.events.PresentationDeleteEvent;
- import ru.webinar.mobileV2.comet.events.PresentationUpdateEvent;
- import ru.webinar.mobileV2.comet.events.ScreenSharingDeleteEvent;
- import ru.webinar.mobileV2.comet.events.ScreenSharingUpdateEvent;
- import ru.webinar.mobileV2.entity.EventLog;
- import ru.webinar.mobileV2.entity.Item;
- import ru.webinar.mobileV2.entity.Session;
- import ru.webinar.mobileV2.entity.conference.Conference;
- import ru.webinar.mobileV2.entity.conference.MediaSession;
- import ru.webinar.mobileV2.entity.conference.Presentation;
- import ru.webinar.mobileV2.entity.conference.ScreenSharing;
- import ru.webinar.mobileV2.entity.event.EventRecord;
- import ru.webinar.mobileV2.screen.base.BasePresenter;
- import static ru.webinar.mobileV2.screen.event.EventContract.LOG_TAG;
- import static ru.webinar.mobileV2.screen.event.SeekAsyncTask.MESSAGE_POST_COMET;
- /**
- * Created by Igor Biryukov on 11.10.2017.
- * Copyright (c) 2017 Webinar LLC. All rights reserved.
- */
- public class RecordEventPresenter extends BasePresenter<EventContract.View> implements EventContract.Presenter {
- private EventRecord record;
- private Queue<EventLog> logQueue;
- private Handler handler;
- private Session session;
- private WebClient webClient;
- private RecordApi api;
- private SeekAsyncTask seekAsyncTask;
- private final EventBus eventBus = EventBus.getDefault();
- RecordEventPresenter(Session session, WebClient webClient) {
- this.record = (EventRecord) session.getCurrentEvent();
- this.webClient = webClient;
- this.session = session;
- api = webClient.createApi(RecordApi.class);
- handler = new Handler(msg -> {
- if (msg.what == MESSAGE_POST_COMET) {
- Comet comet = (Comet) msg.obj;
- eventBus.post(comet);
- return true;
- }
- return false;
- });
- }
- @Override
- public void dropView() {
- super.dropView();
- if (seekAsyncTask != null && !seekAsyncTask.isCancelled()) seekAsyncTask.cancel();
- cancelHandler();
- webClient.cancel();
- }
- @Override
- public void requestEvent() {
- if (!record.isLoaded()) {
- requestRecord();
- return;
- }
- if (!record.isPaused()) {
- seek(record.getCurrentTime());
- }
- }
- void seek(int time) {
- Log.d(LOG_TAG, "start seek to " + time);
- if (seekAsyncTask != null && !seekAsyncTask.isCancelled()) seekAsyncTask.cancel();
- if (!record.isLoaded()) return;
- record.setPaused(true);
- view.showLoading();
- cancelHandler();
- seekAsyncTask = new SeekAsyncTask(record, this, handler);
- seekAsyncTask.execute(time);
- }
- void onPostSeekTask(Queue<EventLog> logQueue) {
- if (view == null) return;
- record.setPaused(false);
- view.hideLoading();
- view.notifyEventIsUpdated();
- this.logQueue = logQueue;
- int currentTime = record.getCurrentTime();
- for (EventLog log : logQueue) {
- handler.postDelayed(handleLog, (long) ((log.getRelativeTime() - currentTime) * 1000));
- }
- handler.postDelayed(handleCurrentTime, 1000);
- view.setRecordSeekBarValue(currentTime);
- }
- private void requestRecord() {
- view.showLoading();
- GetRecordHandler.Callback callback = record -> {
- record.setParent(this.record.getParent());
- this.record = record;
- session.setCurrentEvent(record);
- if (view == null) return;
- view.hideLoading();
- view.notifyEventIsUpdated();
- if (!record.isPaused()) {
- seek(record.getCurrentTime());
- }
- };
- //api.getRecord("56240").enqueue(new GetRecordHandler(callback)); //big size
- //api.getRecord("788863").enqueue(new GetRecordHandler(callback)); //5 speakers
- //api.getRecord("745749").enqueue(new GetRecordHandler(callback)); //sasha/presentation/denis
- //api.getRecord("925489").enqueue(new GetRecordHandler(callback)); //images
- //api.getRecord("931921").enqueue(new GetRecordHandler(callback)); //pdf
- api.getRecord("1007123").enqueue(new GetRecordHandler(callback)); //screensharing
- }
- private void cancelHandler() {
- handler.removeCallbacks(handleLog);
- handler.removeCallbacks(handleCurrentTime);
- handler.removeMessages(MESSAGE_POST_COMET);
- }
- private Runnable handleLog = () -> {
- if (view == null) return;
- EventLog log = logQueue.poll();
- Toast.makeText(view.getContext(), log.getModule(), Toast.LENGTH_SHORT).show();
- CometHandler.post(log);
- };
- private Runnable handleCurrentTime = () -> {
- if (view == null) return;
- record.setCurrentTime(record.getCurrentTime() + 1);
- view.setRecordSeekBarValue(record.getCurrentTime());
- if (record.getCurrentTime() < record.getDuration()) {
- handler.postDelayed(RecordEventPresenter.this.handleCurrentTime, 1000);
- }
- };
- private void notifyViewIfRecordIsNotPaused() {
- if (!record.isPaused()) {
- view.notifyEventIsUpdated();
- }
- }
- //region Comet events - we change only model here
- @Subscribe
- void onEventSessionStop(EventSessionStopEvent event) {
- record.restoreFrom(null);
- notifyViewIfRecordIsNotPaused();
- }
- @Subscribe
- void onConferenceAdd(ConferenceAddEvent event) {
- Conference conference = event.getConference();
- record.getConferences().add(conference);
- notifyViewIfRecordIsNotPaused();
- }
- @Subscribe
- void onConferenceUpdate(ConferenceUpdateEvent event) {
- List<Conference> players = record.getConferences();
- Conference conference = event.getConference();
- Conference oldConference = Item.Companion.find(players, conference.getId());
- if (oldConference == null) {
- FirebaseCrash.log("onConferenceUpdate - oldConference is null");
- return;
- }
- oldConference.setHasAudio(conference.getHasAudio());
- oldConference.setHasVideo(conference.getHasVideo());
- notifyViewIfRecordIsNotPaused();
- }
- @Subscribe
- void onMediaSessionAdd(MediaSessionAddEvent event) {
- MediaSession mediaSession = event.getMediaSession();
- Conference conference = Item.Companion.find(record.getConferences(), mediaSession.getParentId());
- if (conference == null) {
- conference = record.getScreenSharing();
- }
- if (conference == null) {
- throw new IllegalStateException("add mediaSession to null conference");
- }
- long startTime = (long) (record.getCurrentTime() - event.getRelativeTime()) * 1000;
- mediaSession.setStartTime(startTime);
- conference.setMediaSession(mediaSession);
- notifyViewIfRecordIsNotPaused();
- }
- @Subscribe
- void onMediaSessionUpdate(MediaSessionUpdateEvent event) {
- Conference conference = Item.Companion.find(record.getConferences(), event.getParentId());
- if (conference == null) {
- conference = record.getScreenSharing();
- }
- if (conference == null) {
- throw new IllegalStateException("add mediaSession to null conference");
- }
- conference.setMediaSession(null);
- notifyViewIfRecordIsNotPaused();
- }
- @Subscribe
- void onPresentationUpdate(PresentationUpdateEvent event) {
- record.setPresentation(event.getPresentation());
- notifyViewIfRecordIsNotPaused();
- }
- @Subscribe
- void onPresentationDelete(PresentationDeleteEvent event) {
- Presentation presentation = record.getPresentation();
- if (presentation != null && event.getId().equals(presentation.getId())) {
- record.setPresentation(null);
- }
- notifyViewIfRecordIsNotPaused();
- }
- @Subscribe
- void onScreenSharingUpdate(ScreenSharingUpdateEvent event) {
- record.setScreenSharing(event.getScreenSharing());
- notifyViewIfRecordIsNotPaused();
- }
- @Subscribe
- void onScreenSharingDelete(ScreenSharingDeleteEvent event) {
- ScreenSharing screenSharing = record.getScreenSharing();
- if (screenSharing != null && event.getId().equals(screenSharing.getId())) {
- record.setScreenSharing(null);
- }
- notifyViewIfRecordIsNotPaused();
- }
- //endregion
- }
Add Comment
Please, Sign In to add comment