Skip to content

Commit cf7c807

Browse files
committed
CoreSpotlight added
- Tab and space problem fixed. - CoreSpotlight support added. After First run the application, user can search location with CoreSpotlight.
1 parent 5eb4c0b commit cf7c807

File tree

3 files changed

+91
-76
lines changed

3 files changed

+91
-76
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ There is two major version for the app released before.
6060
* Core Location
6161

6262
## TODO
63-
* App indexing like CoreSpotlight and `NSUserActivity`
63+
* <s>App indexing like CoreSpotlight and `NSUserActivity`</s>
6464
* <s>Unit Tests</s>
6565
* UI Tests
6666
* <s>Animations</s>

SwiftWeather/AppDelegate.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1313

1414
var window: UIWindow?
1515

16-
1716
func application(_ application: UIApplication,
1817
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
1918
// Override point for customization after application launch.
@@ -51,5 +50,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
5150
// applicationDidEnterBackground:.
5251
}
5352

54-
5553
}

SwiftWeather/WeatherViewController.swift

Lines changed: 90 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,91 +4,108 @@
44
//
55

66
import UIKit
7+
import CoreSpotlight
8+
import MobileCoreServices
79

8-
//MARK: - UIViewController Properties
10+
//MARK: - UIViewController Properties
911
class WeatherViewController: UIViewController {
12+
13+
//MARK: - IBOutlets
14+
@IBOutlet weak var locationLabel: UILabel!
15+
@IBOutlet weak var iconLabel: UILabel!
16+
@IBOutlet weak var temperatureLabel: UILabel!
17+
@IBOutlet var forecastViews: [ForecastView]!
18+
19+
let identifier = "WeatherIdentifier"
20+
21+
//MARK: - Super Methods
22+
override func viewDidLoad() {
23+
super.viewDidLoad()
24+
viewModel = WeatherViewModel()
25+
viewModel?.startLocationService()
26+
}
27+
28+
override func viewWillAppear(_ animated: Bool) {
29+
super.viewWillAppear(animated)
1030

11-
//MARK: - IBOutlets
12-
@IBOutlet weak var locationLabel: UILabel!
13-
@IBOutlet weak var iconLabel: UILabel!
14-
@IBOutlet weak var temperatureLabel: UILabel!
15-
@IBOutlet var forecastViews: [ForecastView]!
31+
locationLabel.center.x -= view.bounds.width
32+
iconLabel.center.x -= view.bounds.width
33+
temperatureLabel.center.x -= view.bounds.width
1634

17-
//MARK: - Super Methods
18-
override func viewDidLoad() {
19-
super.viewDidLoad()
20-
viewModel = WeatherViewModel()
21-
viewModel?.startLocationService()
22-
}
35+
iconLabel.alpha = 0.0
36+
locationLabel.alpha = 0.0
37+
temperatureLabel.alpha = 0.0
38+
}
39+
40+
override func viewDidAppear(_ animated: Bool) {
41+
super.viewDidAppear(animated)
42+
43+
UIView.animate(withDuration: 0.5, animations: {
44+
self.locationLabel.center.x += self.view.bounds.width
45+
})
46+
47+
UIView.animate(withDuration: 0.5, delay: 0.3, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
48+
self.iconLabel.center.x += self.view.bounds.width
49+
}, completion: nil)
50+
51+
UIView.animate(withDuration: 0.5, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
52+
self.temperatureLabel.center.x += self.view.bounds.width
53+
}, completion: nil)
54+
55+
56+
UIView.animate(withDuration: 0.5, delay: 0.3, options: [], animations: {
57+
self.iconLabel.alpha = 1.0
58+
}, completion: nil)
59+
60+
UIView.animate(withDuration: 0.5, delay: 0.4, options: [], animations: {
61+
self.locationLabel.alpha = 1.0
62+
}, completion: nil)
63+
64+
UIView.animate(withDuration: 0.5, delay: 0.5, options: [], animations: {
65+
self.temperatureLabel.alpha = 1.0
66+
}, completion: nil)
67+
68+
}
2369

24-
override func viewWillAppear(_ animated: Bool) {
25-
super.viewWillAppear(animated)
70+
71+
// MARK: ViewModel
72+
var viewModel: WeatherViewModel? {
73+
didSet {
74+
viewModel?.location.observe {
75+
[unowned self] in
76+
self.locationLabel.text = $0
2677

27-
locationLabel.center.x -= view.bounds.width
28-
iconLabel.center.x -= view.bounds.width
29-
temperatureLabel.center.x -= view.bounds.width
78+
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
79+
attributeSet.title = self.locationLabel.text
3080

31-
iconLabel.alpha = 0.0
32-
locationLabel.alpha = 0.0
33-
temperatureLabel.alpha = 0.0
81+
let item = CSSearchableItem(uniqueIdentifier: self.identifier, domainIdentifier: "com.rushjet.SwiftWeather", attributeSet: attributeSet)
82+
CSSearchableIndex.default().indexSearchableItems([item]){error in
83+
if let error = error {
84+
print("Indexing error: \(error.localizedDescription)")
85+
} else {
86+
print("Location item successfully indexed")
87+
}
88+
}
3489
}
3590

36-
override func viewDidAppear(_ animated: Bool) {
37-
super.viewDidAppear(animated)
38-
39-
UIView.animate(withDuration: 0.5, animations: {
40-
self.locationLabel.center.x += self.view.bounds.width
41-
})
42-
43-
UIView.animate(withDuration: 0.5, delay: 0.3, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
44-
self.iconLabel.center.x += self.view.bounds.width
45-
}, completion: nil)
46-
47-
UIView.animate(withDuration: 0.5, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
48-
self.temperatureLabel.center.x += self.view.bounds.width
49-
}, completion: nil)
50-
51-
52-
UIView.animate(withDuration: 0.5, delay: 0.3, options: [], animations: {
53-
self.iconLabel.alpha = 1.0
54-
}, completion: nil)
55-
56-
UIView.animate(withDuration: 0.5, delay: 0.4, options: [], animations: {
57-
self.locationLabel.alpha = 1.0
58-
}, completion: nil)
59-
60-
UIView.animate(withDuration: 0.5, delay: 0.5, options: [], animations: {
61-
self.temperatureLabel.alpha = 1.0
62-
}, completion: nil)
63-
91+
viewModel?.iconText.observe {
92+
[unowned self] in
93+
self.iconLabel.text = $0
6494
}
6595

66-
// MARK: ViewModel
67-
var viewModel: WeatherViewModel? {
68-
didSet {
69-
viewModel?.location.observe {
70-
[unowned self] in
71-
self.locationLabel.text = $0
72-
}
73-
74-
viewModel?.iconText.observe {
75-
[unowned self] in
76-
self.iconLabel.text = $0
77-
}
78-
79-
viewModel?.temperature.observe {
80-
[unowned self] in
81-
self.temperatureLabel.text = $0
96+
viewModel?.temperature.observe {
97+
[unowned self] in
98+
self.temperatureLabel.text = $0
99+
}
100+
101+
viewModel?.forecasts.observe {
102+
[unowned self] (forecastViewModels) in
103+
if forecastViewModels.count >= 4 {
104+
for (index, forecastView) in self.forecastViews.enumerated() {
105+
forecastView.loadViewModel(forecastViewModels[index])
82106
}
83-
84-
viewModel?.forecasts.observe {
85-
[unowned self] (forecastViewModels) in
86-
if forecastViewModels.count >= 4 {
87-
for (index, forecastView) in self.forecastViews.enumerated() {
88-
forecastView.loadViewModel(forecastViewModels[index])
89-
}
90-
}
91107
}
92108
}
109+
}
93110
}
94-
}
111+
}

0 commit comments

Comments
 (0)