From 951ce2ca288f227b0470b96a3c3733f28f08fc73 Mon Sep 17 00:00:00 2001 From: Mijin Jeon Date: Sun, 13 Oct 2019 23:02:49 +0900 Subject: [PATCH] Feat: Implement fetching base url from firebase #94 --- .../MartHoliday/Model/DataSetter.swift | 51 +++++++++++++++++++ .../Model/RemoteConfigManager.swift | 2 +- .../MartHoliday/Model/StorageAPI.swift | 12 +++-- .../ViewController/MainViewController.swift | 3 +- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/MartHoliday/MartHoliday/Model/DataSetter.swift b/MartHoliday/MartHoliday/Model/DataSetter.swift index bf01937..9606171 100644 --- a/MartHoliday/MartHoliday/Model/DataSetter.swift +++ b/MartHoliday/MartHoliday/Model/DataSetter.swift @@ -31,5 +31,56 @@ class DataSetter { } }.resume() } + + + static func request(url: URL?, handler: @escaping(() -> ())) { + guard let url = url else { return } + + let configure = URLSessionConfiguration.default + configure.timeoutIntervalForRequest = 15 + let session = URLSession(configuration: configure) + + session.dataTask(with: url) { (data, response, error) in + + var branches = [BranchRawData]() + if let response = response as? HTTPURLResponse, 200...299 ~= response.statusCode, let data = data { + do { + branches = try JSONDecoder().decode([BranchRawData].self, from: data, keyPath: "data") as! [BranchRawData] + handler() + } catch { + handler() + } + } else { + handler() + } + }.resume() + + } } +struct APIHelper { + + enum RequestType { + case martType + case branches(Mart) + case allMarts + + var componentText: String { + switch self { + case .martType: return "marts/types" + case .allMarts: return "marts" + case .branches(let mart): + return "marts/types/\(mart.pathComponent)" + } + } + + } + + static func url(path: RequestType, parameters: [String:String]?) -> URL? { + guard let baseUrl = RemoteConfigManager.shared().baseURL else { return nil } + let requestUrl = baseUrl.appendingPathComponent(path.componentText) + + return requestUrl + } + +} diff --git a/MartHoliday/MartHoliday/Model/RemoteConfigManager.swift b/MartHoliday/MartHoliday/Model/RemoteConfigManager.swift index d973e8f..dbe6807 100644 --- a/MartHoliday/MartHoliday/Model/RemoteConfigManager.swift +++ b/MartHoliday/MartHoliday/Model/RemoteConfigManager.swift @@ -43,7 +43,7 @@ class RemoteConfigManager: NSObject { let appConfig = AppConfig(status: updateStatus, min: min, latest: latest) let configResult = appConfig.compare() - self.setBaseURL(url: remoteConfig[RemoteConfigKey.baseURL.rawValue].stringValue) +// self.setBaseURL(url: remoteConfig[RemoteConfigKey.baseURL.rawValue].stringValue) handler(configResult) } else { // fetch 실패시 앱 실행 diff --git a/MartHoliday/MartHoliday/Model/StorageAPI.swift b/MartHoliday/MartHoliday/Model/StorageAPI.swift index 2110fe7..34d3e54 100644 --- a/MartHoliday/MartHoliday/Model/StorageAPI.swift +++ b/MartHoliday/MartHoliday/Model/StorageAPI.swift @@ -18,10 +18,14 @@ class StorageAPI { let pathReference = storage.reference(forURL: urlStr) // Download in memory with a maximum allowed size of 1KB (1 * 1024 bytes) - pathReference.getData(maxSize: 100 * 1024) { data, _ in - guard let data = data else { return } - guard let content = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return } - handler(content) + pathReference.getData(maxSize: 100 * 1024) { data, err in + if let err = err { + print(err) + } else { + guard let data = data else { return } + guard let content = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return } + handler(content) + } } } diff --git a/MartHoliday/MartHoliday/ViewController/MainViewController.swift b/MartHoliday/MartHoliday/ViewController/MainViewController.swift index 5cb4610..a08d6e2 100644 --- a/MartHoliday/MartHoliday/ViewController/MainViewController.swift +++ b/MartHoliday/MartHoliday/ViewController/MainViewController.swift @@ -48,7 +48,7 @@ class MainViewController: RechabilityDetectViewController, FavoriteConvertible, slideMenu.dataSource = slideMenuManager slideOpenFlag = false tableView.delaysContentTouches = false - setNaviBarButton() +// setNaviBarButton() addGestures() NotificationCenter.default.addObserver(self, selector: #selector(detectSelectedMenu(_:)), name: .slideMenuTapped, object: nil) @@ -101,6 +101,7 @@ class MainViewController: RechabilityDetectViewController, FavoriteConvertible, navigationItem.leftBarButtonItem = searhButton } + // 상단 편집버튼 private func setNaviBarButton() { let settingButton = UIButton(type: .custom)