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
e6583de2
Commit
e6583de2
authored
Dec 02, 2020
by
Demid Merzlyakov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Search implementation and a bit of refactoring.
parent
d761faf9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
14 deletions
+85
-14
1Weather/UI/View controllers/Location/LocationViewController.swift
+4
-4
1Weather/UI/View controllers/Location/LocationsViewModel.swift
+81
-10
No files found.
1Weather/UI/View controllers/Location/LocationViewController.swift
View file @
e6583de2
...
...
@@ -94,7 +94,7 @@ private extension SavedCitiesViewController {
func
prepareSearchBar
()
{
searchBar
.
searchBarStyle
=
.
minimal
searchBar
.
placeholder
=
"Search"
searchBar
.
placeholder
=
"Search"
.
localized
(
comment
:
"Search bar placeholder text."
)
view
.
addSubview
(
searchBar
)
searchBar
.
snp
.
makeConstraints
{
(
make
)
in
make
.
top
.
equalToSuperview
()
...
...
@@ -143,12 +143,12 @@ private extension SavedCitiesViewController {
//MARK: UItableView Data Source
extension
SavedCitiesViewController
:
UITableViewDataSource
{
func
tableView
(
_
tableView
:
UITableView
,
numberOfRowsInSection
section
:
Int
)
->
Int
{
return
locationsViewModel
.
savedC
ities
.
count
return
locationsViewModel
.
c
ities
.
count
}
func
tableView
(
_
tableView
:
UITableView
,
cellForRowAt
indexPath
:
IndexPath
)
->
UITableViewCell
{
let
cell
=
tableView
.
dequeueReusableCell
(
withIdentifier
:
CityCell
.
kIdentifier
,
for
:
indexPath
)
as!
CityCell
cell
.
configure
(
geoName
:
locationsViewModel
.
savedC
ities
[
indexPath
.
row
],
cell
.
configure
(
geoName
:
locationsViewModel
.
c
ities
[
indexPath
.
row
],
mode
:
locationsViewModel
.
displayMode
)
return
cell
}
...
...
@@ -161,7 +161,7 @@ extension SavedCitiesViewController: UITableViewDelegate {
container
.
backgroundColor
=
ThemeManager
.
currentTheme
.
navigationBarBackgroundColor
let
titleLabel
=
UILabel
()
titleLabel
.
text
=
"Saved Cities (
\(
self
.
locationsViewModel
.
savedC
ities
.
count
)
)"
titleLabel
.
text
=
"Saved Cities"
.
localized
+
"(
\(
self
.
locationsViewModel
.
c
ities
.
count
)
)"
titleLabel
.
font
=
AppFont
.
SFPro
.
bold
(
size
:
24
)
titleLabel
.
textColor
=
ThemeManager
.
currentTheme
.
primaryTextColor
container
.
addSubview
(
titleLabel
)
...
...
1Weather/UI/View controllers/Location/LocationsViewModel.swift
View file @
e6583de2
...
...
@@ -6,6 +6,7 @@
//
import
UIKit
import
AlgoliaSearchClient
protocol
LocationsViewModelDelegate
:
class
{
func
viewModelDidChange
(
model
:
LocationsViewModel
)
...
...
@@ -22,7 +23,12 @@ enum LocationsViewModelDisplayMode {
class
LocationsViewModel
{
//Public
weak
var
delegate
:
LocationsViewModelDelegate
?
private(set)
var
savedCities
=
[
GeoNamesPlace
]()
private(set)
var
cities
=
[
GeoNamesPlace
]()
{
didSet
{
assert
(
Thread
.
isMainThread
)
self
.
delegate
?
.
viewModelDidChange
(
model
:
self
)
}
}
private(set)
var
displayMode
:
LocationsViewModelDisplayMode
=
.
savedCities
init
()
{
...
...
@@ -49,28 +55,93 @@ class LocationsViewModel {
kazan
.
city
=
"Kazan"
kazan
.
stateCode
=
"000000"
kazan
.
country
=
"Russia"
savedC
ities
=
[
perm
,
moscow
,
sp
,
kazan
]
c
ities
=
[
perm
,
moscow
,
sp
,
kazan
]
}
func
fetchCities
(
query
:
String
)
{
//Some fetching method
//Tell delegate to refresh UI
self
.
delegate
?
.
viewModelDidChange
(
model
:
self
)
// This is a test location that can be interpreted on the server to return all alerts
guard
query
!=
"1wville"
else
{
let
fakePlace
=
GeoNamesPlace
()
fakePlace
.
latitude
=
"67.25639"
fakePlace
.
longitude
=
"-150.18417"
fakePlace
.
city
=
"1WVille"
fakePlace
.
state
=
"Alaska"
fakePlace
.
stateCode
=
"AK"
fakePlace
.
country
=
"United States"
fakePlace
.
countryCode
=
"US"
fakePlace
.
fcodeName
=
"populated place"
fakePlace
.
toponymName
=
"1WVille"
fakePlace
.
isMyLocation
=
false
DispatchQueue
.
main
.
async
{
self
.
cities
=
[]
}
return
}
Logger
.
minSeverityLevel
=
.
warning
var
algoliaQuery
=
PlacesQuery
(
query
)
algoliaQuery
.
aroundLatLngViaIP
=
true
algoliaQuery
.
type
=
.
city
let
placesClient
:
PlacesClient
=
PlacesClient
(
appID
:
ApplicationID
(
rawValue
:
kAlgoliaAppId
),
apiKey
:
APIKey
(
rawValue
:
kAlgoliaAPIKey
))
let
language
:
Language
=
Language
(
rawValue
:
NSLocale
.
preferredLanguages
.
first
?
.
components
(
separatedBy
:
"-"
)
.
first
??
"en"
)
print
(
"Search: using language
\(
language
.
rawValue
)
"
)
var
filteredPlaces
=
[
GeoNamesPlace
]()
placesClient
.
search
(
query
:
algoliaQuery
,
language
:
language
,
requestOptions
:
nil
)
{
(
result
:
Result
<
PlacesClient
.
SingleLanguageResponse
,
Error
>
)
in
switch
(
result
)
{
case
.
success
(
let
response
):
print
(
"Search: got
\(
response
.
nbHits
)
results"
)
for
hit
:
Hit
<
Place
>
in
response
.
hits
{
let
place
=
GeoNamesPlace
()
if
hit
.
object
.
localeNames
?
.
first
==
nil
{
print
(
"Search: skip 1 object."
);
continue
}
place
.
city
=
hit
.
object
.
localeNames
?
.
first
place
.
state
=
hit
.
object
.
administrative
?
.
first
place
.
country
=
hit
.
object
.
country
??
""
// ?? "" part is from the Android code, may be not needed on iOS
place
.
countryCode
=
hit
.
object
.
countryCode
?
.
rawValue
.
uppercased
()
??
""
// ?? "" part is from the Android code, may be not needed on iOS
if
let
geolocation
=
hit
.
geolocation
{
place
.
latitude
=
"
\(
geolocation
.
latitude
)
"
place
.
longitude
=
"
\(
geolocation
.
longitude
)
"
}
else
{
place
.
latitude
=
""
place
.
longitude
=
""
}
filteredPlaces
.
append
(
place
)
}
DispatchQueue
.
main
.
async
{
self
.
cities
=
filteredPlaces
}
break
case
.
failure
(
let
error
):
DispatchQueue
.
main
.
async
{
self
.
delegate
?
.
viewModel
(
model
:
self
,
errorHasOccured
:
error
)
}
break
}
}
}
func
add
(
city
:
GeoNamesPlace
)
{
//Tell delegate to refresh UI
self
.
delegate
?
.
viewModelDidChange
(
model
:
self
)
// TODO: Just update self.cities, it will trigger a viewModelDidChange call
}
func
delete
(
city
:
GeoNamesPlace
)
{
//Tell delegate to refresh UI
self
.
delegate
?
.
viewModelDidChange
(
model
:
self
)
// TODO: Just update self.cities, it will trigger a viewModelDidChange call
}
func
select
(
city
:
GeoNamesPlace
)
{
//Tell delegate to refresh UI
//
TODO:
Tell delegate to refresh UI
self
.
delegate
?
.
viewModelDidChange
(
model
:
self
)
}
}
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