Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
1
1weather
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
1weather
Commits
e984a98e
Commit
e984a98e
authored
Jul 09, 2021
by
Demid Merzlyakov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
IOS-101: widget: load up to date data.
parent
a5fe1230
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
15 deletions
+37
-15
OneWeatherWidget/Data/WeatherProvider.swift
+37
-15
No files found.
OneWeatherWidget/Data/WeatherProvider.swift
View file @
e984a98e
...
...
@@ -17,16 +17,6 @@ import CoreDataStorage
class
WeatherProvider
:
TimelineProvider
{
typealias
Entry
=
WeatherEntry
private
lazy
var
manager
:
LocationManager
=
{
LocationManager
.
shared
=
LocationManager
(
weatherUpdateSource
:
WdtWeatherSource
(),
healthSource
:
BlendHealthSource
(),
nwsAlertsManager
:
NWSAlertsManager
(),
fipsSource
:
BlendFIPSSource
(),
pushNotificationsManager
:
nil
,
storage
:
DelayedSaveStorage
(
storage
:
CoreDataStorage
(),
delay
:
2
))
return
LocationManager
.
shared
}()
func
placeholder
(
in
context
:
Context
)
->
WeatherEntry
{
return
WeatherEntry
()
}
...
...
@@ -35,13 +25,45 @@ class WeatherProvider: TimelineProvider {
let
entry
=
WeatherEntry
()
completion
(
entry
)
}
var
storage
:
Storage
=
CoreDataStorage
()
var
weatherSource
:
WeatherSource
=
WdtWeatherSource
()
func
getTimeline
(
in
context
:
Context
,
completion
:
@escaping
(
Timeline
<
WeatherEntry
>
)
->
Void
)
{
guard
let
currentLocation
=
manager
.
selectedLocation
else
{
return
}
manager
.
updateWeather
(
for
:
currentLocation
,
updateType
:
.
preferIncremental
)
{
updatedLocation
in
guard
let
location
=
updatedLocation
else
{
return
}
func
isFreshEnough
(
_
location
:
Location
)
->
Bool
{
guard
let
lastTimeUpdated
=
location
.
lastWeatherUpdateDate
else
{
return
false
}
return
Date
()
.
timeIntervalSince
(
lastTimeUpdated
)
<
self
.
weatherSource
.
weatherUpdateInterval
}
func
getUpToDateLocation
(
_
completion
:
@escaping
(
Location
?)
->
()
)
{
storage
.
load
{
[
weak
self
]
(
locations
,
selectedIndex
,
error
)
in
guard
let
self
=
self
else
{
completion
(
nil
)
return
}
guard
let
locations
=
locations
,
let
selectedIndex
=
selectedIndex
,
selectedIndex
<
locations
.
count
else
{
completion
(
nil
)
return
}
let
selectedLocation
=
locations
[
selectedIndex
]
guard
!
self
.
isFreshEnough
(
selectedLocation
)
else
{
completion
(
selectedLocation
)
return
}
self
.
weatherSource
.
updateWeather
(
for
:
selectedLocation
,
type
:
.
preferIncremental
)
{
updatedLocation
,
error
in
guard
let
updatedLocation
=
updatedLocation
else
{
completion
(
selectedLocation
)
return
}
completion
(
updatedLocation
)
}
}
}
func
getTimeline
(
in
context
:
Context
,
completion
:
@escaping
(
Timeline
<
WeatherEntry
>
)
->
Void
)
{
getUpToDateLocation
{
location
in
let
nextRefresh
=
Calendar
.
current
.
date
(
byAdding
:
.
minute
,
value
:
30
,
to
:
Date
())
!
let
entry
=
WeatherEntry
(
location
:
location
,
date
:
nextRefresh
)
let
timeline
=
Timeline
(
entries
:
[
entry
],
policy
:
.
atEnd
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment