package Ġnet . x pe ce . android . net Ċ Ċ import Ġandroid . content . Broadcast Receiver Ċ import Ġandroid . content . Context Ċ import Ġandroid . support . annotation . Check Result Ċ import Ġio . reactivex . Flow able Ċ import Ġio . reactivex . annotations . Scheduler Support Ċ import Ġio . reactivex . processors . Behavior Processor Ċ Ċ /** Ċ Ġ* Ġ@ author ĠE ug en Ġon Ġ 0 4 . 0 1 . 2 0 1 7 . Ċ Ġ*/ Ċ Ċ @ Deprecated ("") Ċ object ĠReactive Connectivity Ġ{ ĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠCreates Ġa Ġnew Ġ[ Flow able ] Ġbroadcast ing Ġchanges Ġin Ġconnectivity Ġreported Ġby Ġsystem Ġas ĊĠĠĠĠ Ġ* Ġ[ Connectivity Info ] Ġobjects . ĊĠĠĠĠ Ġ* ĊĠĠĠĠ Ġ* ĠA Ġtypical Ġflow Ġwould Ġlook Ġlike Ġthis : ĊĠĠĠĠ Ġ* ĊĠĠĠĠ Ġ* ĠĠĠĠĠ ĠReactive Connectivity . observe ( context ) ĊĠĠĠĠ Ġ* ĠĠĠĠĠĠĠĠĠ Ġ. subscribe On ( Schedulers . computation ()) ĊĠĠĠĠ Ġ* ĠĠĠĠĠĠĠĠĠ Ġ. observe On ( Android Schedulers . main Thread ()) ĊĠĠĠĠ Ġ* ĠĠĠĠĠĠĠĠĠ Ġ. skip While Ġ{ Ġit . isConnected Ġ} Ġ// ĠDo Ġnot Ġshow ĠSn ackbar Ġif Ġstarting Ġin Ġconnected Ġstate . ĊĠĠĠĠ Ġ* ĠĠĠĠĠĠĠĠĠ Ġ. compose ( Rx Lifecycle Android . bind Activity ( lifecycle ())) ĊĠĠĠĠ Ġ* ĠĠĠĠĠĠĠĠĠ Ġ. subscribe Ġ{ ĊĠĠĠĠ Ġ* ĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġshow Connectivity Snackbar ( it ) ĊĠĠĠĠ Ġ* ĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠ Ġ* Ġ</ code ></ pre > ĊĠĠĠĠ Ġ* ĊĠĠĠĠ Ġ* Ġ@ param Ġcontext ĠInstance Ġof Ġ[ Context ] Ġable Ġto Ġregister Ġ[ Broadcast Receiver ] s . ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġ@ Check Result ĊĠĠĠ Ġ@ Jvm Static ĊĠĠĠ Ġ@ Scheduler Support ( Scheduler Support . NONE ) ĊĠĠĠ Ġ@ Deprecated ("") ĊĠĠĠ Ġfun Ġobserve Old ( context : ĠContext ): ĠFlow able < Connectivity Info > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġconnectivity Subject Ġ= ĠBehavior Processor . create < Connectivity Info >() ĊĊĠĠĠĠĠĠĠ Ġval Ġconnectivity Receiver Ġ= ĠConnect ivity Receiver . newInstance ( context . application Context ) ĊĊĠĠĠĠĠĠĠ Ġreturn ĠFlow able . from Callable Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġconnectivity Callback Ġ= ĠConnect ivity Callback Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġconnectivity Subject . onNext ( it ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġconnectivity Receiver . register ( connectivity Callback ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn @ from Callable Ġconnectivity Callback ĊĠĠĠĠĠĠĠ Ġ}. flatMap Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġvar Ġflow able Ġ= Ġconnectivity Subject . do On Cancel Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġconnectivity Receiver . unregister ( it ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġflow able Ġ= Ġflow able . start With ( Connectivity Info . DEFAULT ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġflow able Ġ= Ġflow able . distinct Until Changed () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn @ flatMap Ġflow able ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Check Result ĊĠĠĠ Ġ@ Jvm Static ĊĠĠĠ Ġ@ Scheduler Support ( Scheduler Support . NONE ) ĊĠĠĠ Ġfun Ġobserve ( context : ĠContext ): ĠFlow able < Connectivity Info > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġconnectivity Receiver Ġ= ĠConnect ivity Receiver . newInstance ( context . application Context ) ĊĠĠĠĠĠĠĠ Ġreturn Ġobserve ( connectivity Receiver ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Check Result ĊĠĠĠ Ġ@ Jvm Static ĊĠĠĠ Ġ@ Scheduler Support ( Scheduler Support . NONE ) ĊĠĠĠ Ġfun Ġobserve ( connectivity Receiver : ĠConnect ivity Receiver ): ĠFlow able < Connectivity Info > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġconnectivity Subject Ġ= ĠBehavior Processor . create Default ( connectivity Receiver . info ) ĊĠĠĠĠĠĠĠ Ġval Ġconnectivity Callback Ġ= ĠConnect ivity Callback ( connectivity Subject :: onNext ) ĊĠĠĠĠĠĠĠ Ġreturn Ġconnectivity Subject . distinct Until Changed () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. do On Subscribe Ġ{ Ġconnectivity Receiver . register ( connectivity Callback ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. do On Cancel Ġ{ Ġconnectivity Receiver . unregister ( connectivity Callback ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. publish (). ref Count () ĊĠĠĠ Ġ} Ċ } Ċ