Skip to content

Commit

Permalink
Merge pull request #136 from fassko/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
fassko authored Feb 23, 2019
2 parents fe4cf0e + e97e0ff commit 706bde7
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 162 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ fastlane/test_output


# End of https://www.gitignore.io/api/fastlane
/fastlane/report.xml
14 changes: 12 additions & 2 deletions Dodies.lv.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
636EA8361BA5BDF40071397A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 636EA8351BA5BDF40071397A /* AppDelegate.swift */; };
636EA83D1BA5BDF40071397A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 636EA83B1BA5BDF40071397A /* Main.storyboard */; };
636EA8421BA5BDF40071397A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 636EA8401BA5BDF40071397A /* LaunchScreen.storyboard */; };
637C52FE22216E6900875618 /* FeatureType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 637C52FD22216E6900875618 /* FeatureType.swift */; };
637C53002221944200875618 /* MapViewController+Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 637C52FF2221944200875618 /* MapViewController+Data.swift */; };
637E9C5021B2F31600050173 /* ClusterAnnotationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 637E9C4F21B2F31600050173 /* ClusterAnnotationView.swift */; };
639026FC1D1D393C003BD31F /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 639026FE1D1D393C003BD31F /* Localizable.strings */; };
639710851C8200F1003C3842 /* dodies_logo.png in Resources */ = {isa = PBXBuildFile; fileRef = 639710821C8200F1003C3842 /* dodies_logo.png */; };
Expand Down Expand Up @@ -103,6 +105,8 @@
636EA8351BA5BDF40071397A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
636EA8411BA5BDF40071397A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
636EA8431BA5BDF40071397A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
637C52FD22216E6900875618 /* FeatureType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeatureType.swift; sourceTree = "<group>"; };
637C52FF2221944200875618 /* MapViewController+Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MapViewController+Data.swift"; sourceTree = "<group>"; };
637E9C4F21B2F31600050173 /* ClusterAnnotationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClusterAnnotationView.swift; sourceTree = "<group>"; };
639026FD1D1D393C003BD31F /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Localizable.strings; sourceTree = "<group>"; };
639027001D1D3DF7003BD31F /* lv */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = lv; path = lv.lproj/Localizable.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -270,6 +274,7 @@
isa = PBXGroup;
children = (
6304EB001BA5C2C700B7CD49 /* MapViewController.swift */,
637C52FF2221944200875618 /* MapViewController+Data.swift */,
6326106720910C2700996FE8 /* MapViewController+MapKit.swift */,
63C86AC7208149DD00588CE0 /* DetailsProvider.swift */,
63F5DC6D1C8358F200AEB195 /* SettingsViewController.swift */,
Expand All @@ -283,6 +288,7 @@
isa = PBXGroup;
children = (
63B5218E205EE13600850E41 /* FeatureCollection.swift */,
637C52FD22216E6900875618 /* FeatureType.swift */,
63B52191205EE16400850E41 /* Feature.swift */,
63B52193205EE17A00850E41 /* Property.swift */,
63B52195205EE18600850E41 /* Geometry.swift */,
Expand Down Expand Up @@ -491,6 +497,7 @@
"${BUILT_PRODUCTS_DIR}/Kingfisher/Kingfisher.framework",
"${BUILT_PRODUCTS_DIR}/Lightbox/Lightbox.framework",
"${BUILT_PRODUCTS_DIR}/Localize-Swift/Localize_Swift.framework",
"${BUILT_PRODUCTS_DIR}/PromiseKit/PromiseKit.framework",
"${BUILT_PRODUCTS_DIR}/Realm/Realm.framework",
"${BUILT_PRODUCTS_DIR}/RealmSwift/RealmSwift.framework",
"${BUILT_PRODUCTS_DIR}/SwiftSpinner/SwiftSpinner.framework",
Expand All @@ -502,6 +509,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Kingfisher.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Lightbox.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Localize_Swift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/PromiseKit.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Realm.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RealmSwift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftSpinner.framework",
Expand Down Expand Up @@ -531,6 +539,7 @@
636EA8361BA5BDF40071397A /* AppDelegate.swift in Sources */,
637E9C5021B2F31600050173 /* ClusterAnnotationView.swift in Sources */,
63C86AC6208148C100588CE0 /* DodiesPointDetails.swift in Sources */,
637C52FE22216E6900875618 /* FeatureType.swift in Sources */,
63FB43D11F8A20DF002033CE /* Extensions.swift in Sources */,
63CA57A51BA5F41700A9F5DC /* DodiesAnnotation.swift in Sources */,
63475FEC1C5CC95500A9A5E4 /* DodiesPoint.swift in Sources */,
Expand All @@ -541,6 +550,7 @@
6326106820910C2700996FE8 /* MapViewController+MapKit.swift in Sources */,
63B52192205EE16400850E41 /* Feature.swift in Sources */,
63B52194205EE17A00850E41 /* Property.swift in Sources */,
637C53002221944200875618 /* MapViewController+Data.swift in Sources */,
634DABC220950A2F003AB312 /* MainCoordinator.swift in Sources */,
63CA57A71BA606B800A9F5DC /* DetailsViewController.swift in Sources */,
63B5218F205EE13600850E41 /* FeatureCollection.swift in Sources */,
Expand Down Expand Up @@ -675,7 +685,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer: Kristaps Grinbergs (MMGQCA8E72)";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer: Kristaps Grinbergs (MMGQCA8E72)";
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1.9.1.181203.2304;
CURRENT_PROJECT_VERSION = 1.9.1.181208.1533;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_BITCODE = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand Down Expand Up @@ -742,7 +752,7 @@
CODE_SIGN_IDENTITY = "iPhone Distribution: Kristaps Grinbergs (3SGNZQGS68)";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution: Kristaps Grinbergs (3SGNZQGS68)";
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1.9.1.181203.2304;
CURRENT_PROJECT_VERSION = 1.9.1.181208.1533;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_BITCODE = NO;
ENABLE_NS_ASSERTIONS = NO;
Expand Down
22 changes: 22 additions & 0 deletions Dodies.lv/FeatureType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// FeatureType.swift
// Dodies.lv
//
// Created by Kristaps Grinbergs on 23/02/2019.
// Copyright © 2019 fassko. All rights reserved.
//

import Foundation

/// Feature type
enum FeatureType: String, Codable, CaseIterable {

/// Hiking path
case taka

/// Tower
case tornis

/// Picnic place
case pikniks
}
4 changes: 2 additions & 2 deletions Dodies.lv/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.9.1</string>
<string>1.10.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.9.1.181203.2304</string>
<string>1.9.1.181208.1533</string>
<key>Fabric</key>
<dict>
<key>APIKey</key>
Expand Down
107 changes: 107 additions & 0 deletions Dodies.lv/MapViewController+Data.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// MapViewController+Data.swift
// Dodies.lv
//
// Created by Kristaps Grinbergs on 23/02/2019.
// Copyright © 2019 fassko. All rights reserved.
//

import Foundation

import PromiseKit
import RealmSwift

extension MapViewController {

func deleteData() -> Promise<Void> {
return Promise<Void> { seal in
do {
let realm = try Realm()

try realm.write {
realm.deleteAll()
seal.fulfill(())
}
} catch {
seal.reject(error)
fatalError("Can't update points in Realm")
}
}
}

func downloadData(with type: FeatureType) -> Promise<Void> {
return Promise { seal in

let url = URL(string: "https://dodies.lv/json/\(language)\(type.rawValue).geojson")!

let task = URLSession.shared.dataTask(with: url) {[weak self] data, _, error in
if let error = error {
debugPrint("Can't download data \(error) \(String(describing: self?.language))")
self?.showError(withMessage: "Can't download data. Please check your settings and try again.".localized())
seal.reject(error)
} else {

guard let data = data,
let features = try? JSONDecoder().decode(FeatureCollection.self, from: data).features else {
return
}

do {
let realm = try Realm()

let realmObjects = features.map { feature -> DodiesPoint in
let dodiesPoint = DodiesPoint()
dodiesPoint.latitude = feature.geometry.coordinates[1]
dodiesPoint.longitude = feature.geometry.coordinates[0]

let properties = feature.properties
dodiesPoint.name = properties.na
dodiesPoint.tips = properties.ti.rawValue
dodiesPoint.st = properties.st
dodiesPoint.km = properties.km
dodiesPoint.txt = properties.txt
dodiesPoint.dat = properties.dat
dodiesPoint.img = properties.img
dodiesPoint.img2 = properties.img2
dodiesPoint.url = properties.url

return dodiesPoint
}

try realm.write {
realm.add(realmObjects)
seal.fulfill(())
}
} catch {
seal.reject(error)
fatalError("Can't update points in Realm")
}
}
}

task.resume()
}
}

/// Check if need to update
///
/// - Parameter update: If need to force update
func checkLastChangedDate(update: Bool) {
let url = URL(string: "https://dodies.lv/apraksti/lastchanged.txt")!

URLSession.shared.dataTask(with: url) {[weak self] data, _, error in
if let error = error {
debugPrint("Can't get last changed date \(error)")
} else if let data = data,
let lastChangedDate = String(data: data, encoding: .utf8),
let timestamp = Int(lastChangedDate.replacingOccurrences(of: "\n", with: "")) {

if update, timestamp > UserDefaults.standard.integer(forKey: Constants.lastChangedTimestampKey) {
self?.downloadData()
} else {
UserDefaults.standard.set(timestamp, forKey: Constants.lastChangedTimestampKey)
}
}
}.resume()
}
}
105 changes: 22 additions & 83 deletions Dodies.lv/MapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import MapKit
import RealmSwift
import Localize_Swift
import SwiftSpinner
import PromiseKit

class MapViewController: UIViewController, CLLocationManagerDelegate, Storyboarded {

Expand All @@ -25,7 +26,7 @@ class MapViewController: UIViewController, CLLocationManagerDelegate, Storyboard

@IBOutlet private weak var mapView: MKMapView!

private var language: String = {
var language: String = {
guard let language = UserDefaults.standard.string(forKey: Constants.languageKey) else {
return "lv"
}
Expand Down Expand Up @@ -99,66 +100,29 @@ class MapViewController: UIViewController, CLLocationManagerDelegate, Storyboard
}

// download data from server
private func downloadData() {

guard let url = URL(string: "http://dodies.lv/json/\(language).geojson") else { return }

let task = URLSession.shared.dataTask(with: url, completionHandler: {[weak self] data, _, error in
if let error = error {
debugPrint("Can't download data \(error) \(String(describing: self?.language))")
self?.showError(withMessage: "Can't download data. Please check your settings and try again.".localized())
} else {
guard let data = data,
let features = try? JSONDecoder().decode(FeatureCollection.self, from: data).features else {
return
}

do {
let realm = try Realm()

try realm.write {
realm.deleteAll()
}

let realmObjects = features.map({ feature -> DodiesPoint in
let dodiesPoint = DodiesPoint()
dodiesPoint.latitude = feature.geometry.coordinates[1]
dodiesPoint.longitude = feature.geometry.coordinates[0]

let properties = feature.properties
dodiesPoint.name = properties.name
dodiesPoint.tips = properties.tips
dodiesPoint.st = properties.st
dodiesPoint.km = properties.km
dodiesPoint.txt = properties.txt
dodiesPoint.dat = properties.dat
dodiesPoint.img = properties.img
dodiesPoint.img2 = properties.img2
dodiesPoint.url = properties.url

return dodiesPoint
})

try realm.write {
realm.add(realmObjects)
}

self?.checkLastChangedDate(update: false)

DispatchQueue.main.async {
self?.removeAllAnnotations()
self?.loadPoints()
}
} catch {
fatalError("Can't update points in Realm")
}
func downloadData() {
firstly {
deleteData()
}.then { _ in
self.downloadData(with: .taka)
}.then {_ in
self.downloadData(with: .tornis)
}
.then { _ in
self.downloadData(with: .pikniks)
}.done { _ in
self.checkLastChangedDate(update: false)

DispatchQueue.main.async {
self.removeAllAnnotations()
self.loadPoints()
}
})

task.resume()
}.catch {
debugPrint("Can't download data \($0)")
}
}

// remove all annotation from mapview
/// Remove all annotation from mapview
private func removeAllAnnotations() {
let annotations = mapView.annotations

Expand Down Expand Up @@ -189,7 +153,6 @@ class MapViewController: UIViewController, CLLocationManagerDelegate, Storyboard
}

DispatchQueue.main.async {

self.mapView.addAnnotations(mapAnnotations)
SwiftSpinner.hide()

Expand All @@ -200,29 +163,5 @@ class MapViewController: UIViewController, CLLocationManagerDelegate, Storyboard
} catch {
print("Can't load points")
}

}

// check if need to update
private func checkLastChangedDate(update: Bool) {
let url = URL(string: "http://dodies.lv/apraksti/lastchanged.txt")!

URLSession.shared.dataTask(with: url) {[weak self] data, _, error in
if let error = error {
debugPrint("Can't get last changed date \(error)")
}

guard let data = data,
let lastChangedDate = String(data: data, encoding: .utf8),
let timestamp = Int(lastChangedDate.replacingOccurrences(of: "\n", with: "")) else {
return
}

if update, timestamp > UserDefaults.standard.integer(forKey: Constants.lastChangedTimestampKey) {
self?.downloadData()
} else {
UserDefaults.standard.set(timestamp, forKey: Constants.lastChangedTimestampKey)
}
}.resume()
}
}
5 changes: 3 additions & 2 deletions Dodies.lv/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import Foundation

/// Feature properties
// swiftlint:disable identifier_name
struct Property: Codable {

/// Name
var name: String
var na: String

/// Type
var tips: String
var ti: FeatureType

/// Status
var st: String
Expand Down
Binary file modified Dodies.paw
Binary file not shown.
1 change: 1 addition & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ target 'Dodies.lv' do
pod 'Lightbox'
pod 'RealmSwift'
pod 'SwiftLint'
pod 'PromiseKit'
end
Loading

0 comments on commit 706bde7

Please sign in to comment.