Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.98 KB | None | 0 0
  1. fun Bone.presentStreaming(scheduleEvent: ScheduleEvent, isTablet: Boolean) {
  2.     fun showStreaming() {
  3.         GlobalScope.launch {
  4.             var bone: Bone? = null
  5.             ProgressBone.lock()
  6.             try {
  7.                 bone = StreamingScreenBone(scheduleEvent, networkGuard { LifeRequest().list() })
  8.             } catch (e: Exception) {
  9.             }
  10.             bone?.let {
  11.                 withContext(Main) {
  12.                     ProgressBone.unlock()
  13.                     if (isTablet) show(bone)
  14.                     else present(bone)
  15.                 }
  16.             }
  17.         }
  18.     }
  19.  
  20.     if (showPaywallIfNeeded(scheduleEvent) { isSuccess -> if (isSuccess) showStreaming() }) return
  21.     showStreaming()
  22. }
  23.  
  24.  
  25. suspend fun <T> networkGuard(nested: suspend () -> T): T = coroutineScope {
  26.     try {
  27.         Log.d("Попытка")
  28.         nested()
  29.     } catch (e: NetworkNotAvailable) {
  30.         Log.d("ошибка")
  31.         suspendCoroutine<T> {
  32.             launch(Main) {
  33.                 val context: Context = service(App)
  34.  
  35.                 PopupData(
  36.                     VectorDrawableCompat.create(context.resources, drawable.ic_no_connection, null),
  37.                     "Кажется, пропала связь",
  38.                     e.localizedMessage ?: "Ошибка",
  39.                     { isReplay ->
  40.                         if (isReplay) {
  41.                             Log.d("повтор")
  42.                             launch {
  43.                                 it.resume(networkGuard(nested))
  44.                                 ProgressBone.unlock()
  45.                             }
  46.                         } else {
  47.                             ProgressBone.unlock()
  48.                             it.resumeWithException(e)
  49.                         }
  50.                     },
  51.                     PopupDataButton("Закрыть", type = NEGATIVE, actionCallback = { })
  52.                 ).show((context.activity() as RootActivity).bone)
  53.             }
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement