package Ġcom . v wo om . time lap seg allery . data . source Ċ Ċ import Ġandroid . location . Location Ċ import Ġcom . v wo om . time lap seg allery . weather . data . Forecast Location Response Ċ import Ġcom . v wo om . time lap seg allery . weather . data . Forecast Response Ċ import Ġcom . v wo om . time lap seg allery . weather . Weather Result Ċ import Ġcom . v wo om . time lap seg allery . weather . Weather Service Ċ import Ġjavax . inject . Inject Ċ Ċ const Ġval ĠERROR _ NO _ LOCATION _ RESPONSE Ġ= Ġ" no _ location _ response " Ċ const Ġval ĠERROR _ NO _ RESPONSE Ġ= Ġ" no _ response " Ċ Ċ class ĠWeather Remote DataSource Ġ@ Inject Ġconstructor ( private Ġval Ġweather Service : ĠWeather Service ): ĠI Weather Remote DataSource Ġ{ ĊĠĠĠ Ġ// ĠGet Ġthe Ġforecast Ġfrom Ġthe Ġnational Ġweather Ġservice Ġapi ĊĠĠĠ Ġ// ĠReturns Ġeither Ġ( 1 ) ĠWeather ĠResult : ĠNo ĠData Ġor Ġ( 2 ) ĠWeather ĠResult : ĠToday 's ĠFore cast ĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġget Forecast ( location : ĠLocation ): ĠWeather Result < Forecast Response > Ġ{ ĊĠĠĠĠĠĠĠ Ġ// Ġ 1 . ĠGet Ġthe Ġurl Ġto Ġquery Ġthe Ġforecast Ġfor Ġthis Ġdevices Ġlocation ĊĠĠĠĠĠĠĠ Ġval Ġforecast Location Response : ĠFore cast Location Response ? ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġforecast Location Deferred Ġ= Ġweather Service ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. get Forecast Location Async ( location . latitude . toString (), Ġlocation . longitude . toString ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġforecast Location Response Ġ= Ġforecast Location Deferred . await () ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĠWeather Result . No Data ( e , Ġe . localized Message ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ// ĠIf Ġthe Ġurl Ġdid Ġnot Ġreturn Ġthen Ġno Ġdata Ġcan Ġbe Ġreturned ĊĠĠĠĠĠĠĠ Ġif Ġ( forecast Location Response Ġ== Ġnull ) Ġreturn ĠWeather Result . No Data ( null , ĠERROR _ NO _ LOCATION _ RESPONSE ) ĊĊĠĠĠĠĠĠĠ Ġ// Ġ 2 . ĠCall Ġthe Ġurl Ġto Ġget Ġthe Ġforecast Ġfor Ġthe Ġlocation Ġand Ġreturn Ġthe Ġresult ĊĠĠĠĠĠĠĠ Ġval Ġurl Ġ= Ġforecast Location Response . properties . forecast ĊĠĠĠĠĠĠĠ Ġreturn Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġget Forecast Deferred Ġ= Ġweather Service . get Forecast Async ( url ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġforecast Response Ġ= Ġget Forecast Deferred . await () ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// ĠIf Ġwe Ġhave Ġa Ġresponse Ġreturn Ġa Ġweather Ġresult Ġwith Ġthe Ġforecast Ġpackaged Ġas Ġdata Ġand Ġthe Ġtimestamp ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( forecast Response Ġ!= Ġnull ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠWeather Result . Tod ays Forecast ( forecast Response , ĠSystem . currentTimeMillis ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// ĠOtherwise Ġgive Ġback Ġan Ġerror ĊĠĠĠĠĠĠĠĠĠĠĠ Ġelse ĠWeather Result . No Data ( null , ĠERROR _ NO _ RESPONSE ) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠWeather Result . No Data ( e ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ }