Skip to content

Commit

Permalink
Optimize Today Extension
Browse files Browse the repository at this point in the history
  • Loading branch information
神楽坂花火 committed Mar 14, 2020
1 parent 548ef2d commit e159c56
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
3 changes: 3 additions & 0 deletions ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ target 'Runner' do
use_frameworks!
use_modular_headers!

pod 'Alamofire', '~> 5.0'
pod 'AlamofireImage', '~> 4.0'

# Flutter Pod

copied_flutter_dir = File.join(__dir__, 'Flutter')
Expand Down
11 changes: 10 additions & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
PODS:
- Alamofire (5.0.2)
- AlamofireImage (4.0.2):
- Alamofire (< 6.0.0, >= 5.0.2)
- Flutter (1.0.0)
- flutter_plugin_android_lifecycle (0.0.1):
- Flutter
Expand Down Expand Up @@ -32,6 +35,8 @@ PODS:
- Flutter

DEPENDENCIES:
- Alamofire (~> 5.0)
- AlamofireImage (~> 4.0)
- Flutter (from `Flutter`)
- flutter_plugin_android_lifecycle (from `.symlinks/plugins/flutter_plugin_android_lifecycle/ios`)
- image_picker (from `.symlinks/plugins/image_picker/ios`)
Expand All @@ -49,6 +54,8 @@ DEPENDENCIES:

SPEC REPOS:
trunk:
- Alamofire
- AlamofireImage
- FMDB

EXTERNAL SOURCES:
Expand Down Expand Up @@ -82,6 +89,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/url_launcher_web/ios"

SPEC CHECKSUMS:
Alamofire: 3ba7a4db18b4f62c4a1c0e1cb39d7f3d52e10ada
AlamofireImage: ca7325440303d041a80b7caa55066ba945522378
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
flutter_plugin_android_lifecycle: 47de533a02850f070f5696a623995e93eddcdb9b
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
Expand All @@ -98,6 +107,6 @@ SPEC CHECKSUMS:
url_launcher_macos: fd7894421cd39320dce5f292fc99ea9270b2a313
url_launcher_web: e5527357f037c87560776e36436bf2b0288b965c

PODFILE CHECKSUM: a2b0bf7fe93e4e6b732ab20a6fda201dc8a23f90
PODFILE CHECKSUM: de92ffc9c4fb086e3bef042f78de79dfec856d10

COCOAPODS: 1.8.4
44 changes: 19 additions & 25 deletions ios/TodayExtension/TodayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
//

import Alamofire
import AlamofireImage
import UIKit
import NotificationCenter

Expand All @@ -28,15 +30,24 @@ class TodayViewController: UIViewController, NCWidgetProviding {
}

func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
Picture.today(completion: { data in
self.current = data[Int.random(in: 0..<data.count)]
let url = URL.init(string: self.current!.url)
let bin = try! Data(contentsOf: url!)
DispatchQueue.main.async {
self.imageView.image = UIImage(data: bin)
completionHandler(.newData)
AF.request("https://v2.api.dailypics.cn/today").responseJSON { response in
switch response.result {
case .success:
if let data = response.value as? Array<Any> {
let random = data[Int.random(in: 0..<data.count)]
self.current = Picture(source: random as! [String : Any])
let url = URL.init(string: self.current!.url)
DispatchQueue.main.async {
self.imageView.af.setImage(withURL: url!)
completionHandler(.newData)
}
} else {
completionHandler(.noData)
}
case .failure:
completionHandler(.failed)
}
})
}
}

func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
Expand Down Expand Up @@ -69,21 +80,4 @@ struct Picture {
self.id = id
self.url = "https://s1.images.dailypics.cn/\(path)!w1080_jpg"
}

static func today(completion: @escaping ([Picture]) -> Void) {
var result: [Picture] = []

let url = URL.init(string: "https://v2.api.dailypics.cn/today")
URLSession.shared.dataTask(with: url!) { data, response, error in
if let data = data,
let decoded = try? JSONSerialization.jsonObject(with: data, options: []) as? Array<Any> {
for case let item in decoded {
if let obj = Picture.init(source: item as! [String : Any]) {
result.append(obj)
}
}
completion(result)
}
}.resume()
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dailypics
description: A new Flutter application.
version: 2.1.2+200305
version: 2.1.2+200307

environment:
sdk: ">=2.3.0 <3.0.0"
Expand Down

0 comments on commit e159c56

Please sign in to comment.