Guest User

Untitled

a guest
May 26th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.21 KB | None | 0 0
  1. package ru.webinar.mobileV2.screen.event;
  2.  
  3. import android.os.Handler;
  4. import android.util.Log;
  5. import android.widget.Toast;
  6.  
  7. import com.google.firebase.crash.FirebaseCrash;
  8.  
  9. import org.greenrobot.eventbus.EventBus;
  10. import org.greenrobot.eventbus.Subscribe;
  11.  
  12. import java.util.List;
  13. import java.util.Queue;
  14.  
  15. import ru.webinar.mobileV2.client.WebClient;
  16. import ru.webinar.mobileV2.client.api.RecordApi;
  17. import ru.webinar.mobileV2.client.callback.GetRecordHandler;
  18. import ru.webinar.mobileV2.comet.CometHandler;
  19. import ru.webinar.mobileV2.comet.events.Comet;
  20. import ru.webinar.mobileV2.comet.events.ConferenceAddEvent;
  21. import ru.webinar.mobileV2.comet.events.ConferenceUpdateEvent;
  22. import ru.webinar.mobileV2.comet.events.EventSessionStopEvent;
  23. import ru.webinar.mobileV2.comet.events.MediaSessionAddEvent;
  24. import ru.webinar.mobileV2.comet.events.MediaSessionUpdateEvent;
  25. import ru.webinar.mobileV2.comet.events.PresentationDeleteEvent;
  26. import ru.webinar.mobileV2.comet.events.PresentationUpdateEvent;
  27. import ru.webinar.mobileV2.comet.events.ScreenSharingDeleteEvent;
  28. import ru.webinar.mobileV2.comet.events.ScreenSharingUpdateEvent;
  29. import ru.webinar.mobileV2.entity.EventLog;
  30. import ru.webinar.mobileV2.entity.Item;
  31. import ru.webinar.mobileV2.entity.Session;
  32. import ru.webinar.mobileV2.entity.conference.Conference;
  33. import ru.webinar.mobileV2.entity.conference.MediaSession;
  34. import ru.webinar.mobileV2.entity.conference.Presentation;
  35. import ru.webinar.mobileV2.entity.conference.ScreenSharing;
  36. import ru.webinar.mobileV2.entity.event.EventRecord;
  37. import ru.webinar.mobileV2.screen.base.BasePresenter;
  38.  
  39. import static ru.webinar.mobileV2.screen.event.EventContract.LOG_TAG;
  40. import static ru.webinar.mobileV2.screen.event.SeekAsyncTask.MESSAGE_POST_COMET;
  41.  
  42.  
  43. /**
  44. * Created by Igor Biryukov on 11.10.2017.
  45. * Copyright (c) 2017 Webinar LLC. All rights reserved.
  46. */
  47. public class RecordEventPresenter extends BasePresenter<EventContract.View> implements EventContract.Presenter {
  48.  
  49. private EventRecord record;
  50. private Queue<EventLog> logQueue;
  51. private Handler handler;
  52. private Session session;
  53. private WebClient webClient;
  54. private RecordApi api;
  55. private SeekAsyncTask seekAsyncTask;
  56. private final EventBus eventBus = EventBus.getDefault();
  57.  
  58.  
  59. RecordEventPresenter(Session session, WebClient webClient) {
  60. this.record = (EventRecord) session.getCurrentEvent();
  61. this.webClient = webClient;
  62. this.session = session;
  63.  
  64. api = webClient.createApi(RecordApi.class);
  65.  
  66. handler = new Handler(msg -> {
  67. if (msg.what == MESSAGE_POST_COMET) {
  68. Comet comet = (Comet) msg.obj;
  69. eventBus.post(comet);
  70.  
  71. return true;
  72. }
  73.  
  74. return false;
  75. });
  76. }
  77.  
  78.  
  79. @Override
  80. public void dropView() {
  81. super.dropView();
  82.  
  83. if (seekAsyncTask != null && !seekAsyncTask.isCancelled()) seekAsyncTask.cancel();
  84.  
  85. cancelHandler();
  86. webClient.cancel();
  87. }
  88.  
  89.  
  90. @Override
  91. public void requestEvent() {
  92. if (!record.isLoaded()) {
  93. requestRecord();
  94.  
  95. return;
  96. }
  97.  
  98. if (!record.isPaused()) {
  99. seek(record.getCurrentTime());
  100. }
  101. }
  102.  
  103.  
  104. void seek(int time) {
  105. Log.d(LOG_TAG, "start seek to " + time);
  106.  
  107. if (seekAsyncTask != null && !seekAsyncTask.isCancelled()) seekAsyncTask.cancel();
  108.  
  109. if (!record.isLoaded()) return;
  110.  
  111. record.setPaused(true);
  112.  
  113. view.showLoading();
  114.  
  115. cancelHandler();
  116.  
  117. seekAsyncTask = new SeekAsyncTask(record, this, handler);
  118. seekAsyncTask.execute(time);
  119. }
  120.  
  121.  
  122. void onPostSeekTask(Queue<EventLog> logQueue) {
  123. if (view == null) return;
  124.  
  125. record.setPaused(false);
  126.  
  127. view.hideLoading();
  128. view.notifyEventIsUpdated();
  129.  
  130. this.logQueue = logQueue;
  131.  
  132. int currentTime = record.getCurrentTime();
  133.  
  134. for (EventLog log : logQueue) {
  135. handler.postDelayed(handleLog, (long) ((log.getRelativeTime() - currentTime) * 1000));
  136. }
  137.  
  138. handler.postDelayed(handleCurrentTime, 1000);
  139.  
  140. view.setRecordSeekBarValue(currentTime);
  141. }
  142.  
  143.  
  144. private void requestRecord() {
  145. view.showLoading();
  146.  
  147. GetRecordHandler.Callback callback = record -> {
  148. record.setParent(this.record.getParent());
  149. this.record = record;
  150. session.setCurrentEvent(record);
  151.  
  152. if (view == null) return;
  153.  
  154. view.hideLoading();
  155. view.notifyEventIsUpdated();
  156.  
  157. if (!record.isPaused()) {
  158. seek(record.getCurrentTime());
  159. }
  160. };
  161.  
  162. //api.getRecord("56240").enqueue(new GetRecordHandler(callback)); //big size
  163. //api.getRecord("788863").enqueue(new GetRecordHandler(callback)); //5 speakers
  164. //api.getRecord("745749").enqueue(new GetRecordHandler(callback)); //sasha/presentation/denis
  165. //api.getRecord("925489").enqueue(new GetRecordHandler(callback)); //images
  166. //api.getRecord("931921").enqueue(new GetRecordHandler(callback)); //pdf
  167. api.getRecord("1007123").enqueue(new GetRecordHandler(callback)); //screensharing
  168. }
  169.  
  170.  
  171. private void cancelHandler() {
  172. handler.removeCallbacks(handleLog);
  173. handler.removeCallbacks(handleCurrentTime);
  174. handler.removeMessages(MESSAGE_POST_COMET);
  175. }
  176.  
  177.  
  178. private Runnable handleLog = () -> {
  179. if (view == null) return;
  180.  
  181. EventLog log = logQueue.poll();
  182.  
  183. Toast.makeText(view.getContext(), log.getModule(), Toast.LENGTH_SHORT).show();
  184.  
  185. CometHandler.post(log);
  186. };
  187.  
  188.  
  189. private Runnable handleCurrentTime = () -> {
  190. if (view == null) return;
  191.  
  192. record.setCurrentTime(record.getCurrentTime() + 1);
  193. view.setRecordSeekBarValue(record.getCurrentTime());
  194.  
  195. if (record.getCurrentTime() < record.getDuration()) {
  196. handler.postDelayed(RecordEventPresenter.this.handleCurrentTime, 1000);
  197. }
  198. };
  199.  
  200.  
  201. private void notifyViewIfRecordIsNotPaused() {
  202. if (!record.isPaused()) {
  203. view.notifyEventIsUpdated();
  204. }
  205. }
  206.  
  207.  
  208. //region Comet events - we change only model here
  209.  
  210.  
  211. @Subscribe
  212. void onEventSessionStop(EventSessionStopEvent event) {
  213. record.restoreFrom(null);
  214.  
  215. notifyViewIfRecordIsNotPaused();
  216. }
  217.  
  218.  
  219. @Subscribe
  220. void onConferenceAdd(ConferenceAddEvent event) {
  221. Conference conference = event.getConference();
  222.  
  223. record.getConferences().add(conference);
  224.  
  225. notifyViewIfRecordIsNotPaused();
  226. }
  227.  
  228.  
  229. @Subscribe
  230. void onConferenceUpdate(ConferenceUpdateEvent event) {
  231. List<Conference> players = record.getConferences();
  232.  
  233. Conference conference = event.getConference();
  234. Conference oldConference = Item.Companion.find(players, conference.getId());
  235.  
  236. if (oldConference == null) {
  237. FirebaseCrash.log("onConferenceUpdate - oldConference is null");
  238. return;
  239. }
  240.  
  241. oldConference.setHasAudio(conference.getHasAudio());
  242. oldConference.setHasVideo(conference.getHasVideo());
  243.  
  244. notifyViewIfRecordIsNotPaused();
  245. }
  246.  
  247.  
  248. @Subscribe
  249. void onMediaSessionAdd(MediaSessionAddEvent event) {
  250. MediaSession mediaSession = event.getMediaSession();
  251.  
  252. Conference conference = Item.Companion.find(record.getConferences(), mediaSession.getParentId());
  253. if (conference == null) {
  254. conference = record.getScreenSharing();
  255. }
  256.  
  257. if (conference == null) {
  258. throw new IllegalStateException("add mediaSession to null conference");
  259. }
  260.  
  261. long startTime = (long) (record.getCurrentTime() - event.getRelativeTime()) * 1000;
  262. mediaSession.setStartTime(startTime);
  263.  
  264. conference.setMediaSession(mediaSession);
  265.  
  266. notifyViewIfRecordIsNotPaused();
  267. }
  268.  
  269.  
  270. @Subscribe
  271. void onMediaSessionUpdate(MediaSessionUpdateEvent event) {
  272. Conference conference = Item.Companion.find(record.getConferences(), event.getParentId());
  273. if (conference == null) {
  274. conference = record.getScreenSharing();
  275. }
  276.  
  277. if (conference == null) {
  278. throw new IllegalStateException("add mediaSession to null conference");
  279. }
  280.  
  281. conference.setMediaSession(null);
  282.  
  283. notifyViewIfRecordIsNotPaused();
  284. }
  285.  
  286.  
  287. @Subscribe
  288. void onPresentationUpdate(PresentationUpdateEvent event) {
  289. record.setPresentation(event.getPresentation());
  290.  
  291. notifyViewIfRecordIsNotPaused();
  292. }
  293.  
  294.  
  295. @Subscribe
  296. void onPresentationDelete(PresentationDeleteEvent event) {
  297. Presentation presentation = record.getPresentation();
  298.  
  299. if (presentation != null && event.getId().equals(presentation.getId())) {
  300. record.setPresentation(null);
  301. }
  302.  
  303. notifyViewIfRecordIsNotPaused();
  304. }
  305.  
  306.  
  307. @Subscribe
  308. void onScreenSharingUpdate(ScreenSharingUpdateEvent event) {
  309. record.setScreenSharing(event.getScreenSharing());
  310.  
  311. notifyViewIfRecordIsNotPaused();
  312. }
  313.  
  314.  
  315. @Subscribe
  316. void onScreenSharingDelete(ScreenSharingDeleteEvent event) {
  317. ScreenSharing screenSharing = record.getScreenSharing();
  318.  
  319. if (screenSharing != null && event.getId().equals(screenSharing.getId())) {
  320. record.setScreenSharing(null);
  321. }
  322.  
  323. notifyViewIfRecordIsNotPaused();
  324. }
  325.  
  326. //endregion
  327. }
Add Comment
Please, Sign In to add comment