Skip to content

Commit

Permalink
Merge branch 'icloud'
Browse files Browse the repository at this point in the history
  • Loading branch information
icodesign committed Aug 10, 2016
2 parents f0cde2b + 9e46119 commit 05ede3a
Show file tree
Hide file tree
Showing 49 changed files with 2,147 additions and 297 deletions.
4 changes: 2 additions & 2 deletions PacketProcessor/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.4.5</string>
<string>1.4.6</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>115</string>
<string>116</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
Expand Down
4 changes: 2 additions & 2 deletions PacketTunnel/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>1.4.5</string>
<string>1.4.6</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>115</string>
<string>116</string>
<key>Fabric</key>
<dict>
<key>APIKey</key>
Expand Down
1 change: 1 addition & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ target "Potatso" do
pod 'ObjectMapper'
pod 'CocoaLumberjack/Swift'
pod 'Helpshift', '5.6.1'
pod 'PSOperations', '~> 2.3'
tunnel
library
fabric
Expand Down
129 changes: 107 additions & 22 deletions Potatso.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions Potatso/Advance/ProxyConfigurationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ class ProxyConfigurationViewController: FormViewController {
break
}
let ota = values[kProxyFormOta] as? Bool ?? false
defaultRealm.beginWrite()
upstreamProxy.type = type
upstreamProxy.name = name
upstreamProxy.host = host
Expand All @@ -220,8 +219,7 @@ class ProxyConfigurationViewController: FormViewController {
upstreamProxy.ssrProtocol = values[kProxyFormProtocol] as? String
upstreamProxy.ssrObfs = values[kProxyFormObfs] as? String
upstreamProxy.ssrObfsParam = values[kProxyFormObfsParam] as? String
defaultRealm.add(upstreamProxy, update: true)
try defaultRealm.commitWrite()
try DBUtils.add(upstreamProxy)
close()
}catch {
showTextHUD("\(error)", dismissAfterDelay: 1.0)
Expand Down
6 changes: 2 additions & 4 deletions Potatso/Advance/RuleConfigurationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class RuleConfigurationViewController: FormViewController {
<<< PushRow<RuleType>(kRuleFormType) {
$0.title = "Type".localized()
$0.selectorTitle = "Choose type of rule".localized()
$0.options = [RuleType.URL, RuleType.DomainSuffix, RuleType.DomainMatch, RuleType.Domain, RuleType.IPCIDR, RuleType.GeoIP]
$0.options = [RuleType.DomainSuffix, RuleType.DomainMatch, RuleType.Domain, RuleType.IPCIDR, RuleType.GeoIP]
$0.value = self.rule.type
$0.disabled = Condition(booleanLiteral: !editable)
}.cellSetup({ (cell, row) -> () in
Expand Down Expand Up @@ -105,10 +105,8 @@ class RuleConfigurationViewController: FormViewController {
guard let action = values[kRuleFormAction] as? RuleAction else {
throw "You must choose a action".localized()
}
defaultRealm.beginWrite()
rule.update(type, action: action, value: value)
defaultRealm.add(rule, update: true)
try defaultRealm.commitWrite()
try DBUtils.add(rule)
callback?(rule)
close()
}catch {
Expand Down
5 changes: 1 addition & 4 deletions Potatso/Advance/RuleSetConfigurationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,10 @@ class RuleSetConfigurationViewController: FormViewController {
throw "Name already exists".localized()
}
}

defaultRealm.beginWrite()
ruleSet.name = name
ruleSet.rules.removeAll()
ruleSet.rules.appendContentsOf(rules)
defaultRealm.add(ruleSet, update: true)
try defaultRealm.commitWrite()
try DBUtils.add(ruleSet)
callback?(ruleSet)
close()
}catch {
Expand Down
29 changes: 29 additions & 0 deletions Potatso/Base/ActionRow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ActionRow.swift
// Potatso
//
// Created by LEI on 8/4/16.
// Copyright © 2016 TouchingApp. All rights reserved.
//

import Foundation
import Eureka

public final class ActionRow: _LabelRow, RowType {

public required init(tag: String?) {
super.init(tag: tag)
}

public override func updateCell() {
super.updateCell()
cell.selectionStyle = .Default
cell.accessoryType = .DisclosureIndicator
}

public override func didSelect() {
super.didSelect()
cell.setSelected(false, animated: true)
}

}
6 changes: 2 additions & 4 deletions Potatso/CloudDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ class CloudDetailViewController: UIViewController, UITableViewDataSource, UITabl
func subscribe() {
let uuid = ruleSet.uuid
if isExist(uuid) {
let sets = defaultRealm.objects(RuleSet).filter("uuid = %@", uuid)
let ids = defaultRealm.objects(RuleSet).filter("uuid = %@", uuid).map({ $0.uuid })
do {
try defaultRealm.write {
defaultRealm.delete(sets)
}
try DBUtils.softDelete(ids, type: RuleSet.self)
}catch {
self.showTextHUD("Fail to unsubscribe".localized(), dismissAfterDelay: 1.0)
return
Expand Down
4 changes: 1 addition & 3 deletions Potatso/ConfigGroupChooseVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ class ConfigGroupChooseVC: UIViewController, UITableViewDataSource, UITableViewD
}
do {
groups.removeAtIndex(indexPath.row)
try defaultRealm.write {
defaultRealm.delete(item)
}
try DBUtils.softDelete(item.uuid, type: ConfigurationGroup.self)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}catch {
self.showTextHUD("\("Fail to delete item".localized()): \((error as NSError).localizedDescription)", dismissAfterDelay: 1.5)
Expand Down
17 changes: 11 additions & 6 deletions Potatso/Core/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension RuleSet: Mappable {
uuid <- map["id"]
name <- map["name"]
createAt <- (map["created_at"], DateTransform())
updateAt <- (map["updated_at"], DateTransform())
remoteUpdatedAt <- (map["updated_at"], DateTransform())
desc <- map["description"]
ruleCount <- map["rule_count"]
isOfficial <- map["is_official"]
Expand All @@ -81,15 +81,20 @@ extension RuleSet {

static func addRemoteObject(ruleset: RuleSet, update: Bool = true) throws {
ruleset.isSubscribe = true
try defaultRealm.write {
defaultRealm.add(ruleset, update: true)
let id = ruleset.uuid
guard let local = DBUtils.get(id, type: RuleSet.self) else {
try DBUtils.add(ruleset)
return
}
if local.remoteUpdatedAt == ruleset.remoteUpdatedAt {
return
}
try DBUtils.add(ruleset)
}

static func addRemoteArray(rulesets: [RuleSet], update: Bool = true) throws {
rulesets.forEach({ $0.isSubscribe = true })
try defaultRealm.write {
defaultRealm.add(rulesets, update: true)
for ruleset in rulesets {
try addRemoteObject(ruleset, update: update)
}
}

Expand Down
40 changes: 40 additions & 0 deletions Potatso/Core/FeedbackManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// FeedbackManager.swift
// Potatso
//
// Created by LEI on 8/4/16.
// Copyright © 2016 TouchingApp. All rights reserved.
//

import Foundation
import ICSMainFramework

class FeedbackManager {
static let shared = FeedbackManager()

func showFeedback(inVC vc: UIViewController? = nil) {
guard let currentVC = vc ?? UIApplication.sharedApplication().keyWindow?.rootViewController else {
return
}
let options = [
"gotoConversationAfterContactUs": "YES"
]
let rulesets = Manager.sharedManager.defaultConfigGroup.ruleSets.map({ $0.name }).joinWithSeparator(", ")
let defaultToProxy = Manager.sharedManager.defaultConfigGroup.defaultToProxy
var tags: [String] = []
if AppEnv.isTestFlight {
tags.append("testflight")
} else if AppEnv.isAppStore {
tags.append("store")
}
HelpshiftSupport.setMetadataBlock { () -> [NSObject : AnyObject]! in
return [
"Full Version": AppEnv.fullVersion,
"Default To Proxy": defaultToProxy ? "true": "false",
"Rulesets": rulesets,
HelpshiftSupportTagsKey: tags
]
}
HelpshiftSupport.showConversation(currentVC, withOptions: options)
}
}
4 changes: 1 addition & 3 deletions Potatso/Core/Importer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ struct Importer {
proxy.name = text
do {
try proxy.validate(inRealm: defaultRealm)
try defaultRealm.write {
defaultRealm.add(proxy)
}
try DBUtils.add(proxy)
self.onConfigSaveCallback(true, error: nil)
}catch {
self.onConfigSaveCallback(false, error: error)
Expand Down
56 changes: 41 additions & 15 deletions Potatso/DataInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,78 @@
import UIKit
import ICSMainFramework
import NetworkExtension
import CloudKit
import Async
import RealmSwift
import Realm

class DataInitializer: NSObject, AppLifeCycleProtocol {

let s = ICloudSyncService()
var token: RLMNotificationToken? = nil

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
do {
try Manager.sharedManager.setup()
}catch {
error.log("Fail to setup manager")
}
updateCloudSets()
sync()
// token = defaultRealm.addNotificationBlock({ [weak self] (notification, realm) in
// self?.sync()
// })
return true
}

func applicationDidEnterBackground(application: UIApplication) {
_ = try? Manager.sharedManager.regenerateConfigFiles()
}

func applicationWillTerminate(application: UIApplication) {
_ = try? Manager.sharedManager.regenerateConfigFiles()
}

func applicationWillEnterForeground(application: UIApplication) {
Receipt.shared.validate()
sync()
}

func applicationDidBecomeActive(application: UIApplication) {
deleteOrphanRules()
func updateCloudSets() {
let uuids = defaultRealm.objects(RuleSet).filter("isSubscribe = true").map({$0.uuid})
API.updateRuleSetListDetail(uuids) { (response) in
if let sets = response.result.value {
do {
try RuleSet.addRemoteArray(sets)
}catch {
error.log("Unable to save updated rulesets")
return
Async.background(after: 1.5) {
API.updateRuleSetListDetail(uuids) { (response) in
if let sets = response.result.value {
do {
try RuleSet.addRemoteArray(sets)
}catch {
error.log("Unable to save updated rulesets")
return
}
}else {
response.result.error?.log("Fail to update ruleset details")
}
}else {
response.result.error?.log("Fail to update ruleset details")
}
}
}

func sync() {
cleanupData()
SyncManager.shared.sync()
}

func cleanupData() {
deleteOrphanRules()
}

func deleteOrphanRules() {
let orphanRules = defaultRealm.objects(Rule).filter("rulesets.@count == 0")
_ = try? defaultRealm.write({
defaultRealm.delete(orphanRules)
})
if orphanRules.count > 0 {
let ids = orphanRules.map({ $0.uuid })
for id in ids {
_ = try? DBUtils.softDelete(id, type: Rule.self)
}
}
}

}
23 changes: 6 additions & 17 deletions Potatso/HomePresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ class HomePresenter: NSObject {
func chooseProxy() {
let chooseVC = ProxyListViewController(allowNone: true) { [unowned self] proxy in
do {
try defaultRealm.write {
self.group.proxies.removeAll()
if let proxy = proxy {
self.group.proxies.append(proxy)
}
}
try ConfigurationGroup.changeProxy(forGroupId: self.group.uuid, proxyId: proxy?.uuid)
self.delegate?.handleRefreshUI()
}catch {
self.vc.showTextHUD("\("Fail to add ruleset".localized()): \((error as NSError).localizedDescription)", dismissAfterDelay: 1.5)
Expand Down Expand Up @@ -99,9 +94,7 @@ class HomePresenter: NSObject {
}
let group = ConfigurationGroup()
group.name = trimmedName
try defaultRealm.write {
defaultRealm.add(group)
}
try DBUtils.add(group)
CurrentGroupManager.shared.group = group
}

Expand All @@ -124,9 +117,7 @@ class HomePresenter: NSObject {
return
}
do {
try defaultRealm.write {
group.ruleSets.append(ruleSet)
}
try ConfigurationGroup.appendRuleSet(forGroupId: group.uuid, rulesetId: ruleSet.uuid)
self.delegate?.handleRefreshUI()
}catch {
self.vc.showTextHUD("\("Fail to add ruleset".localized()): \((error as NSError).localizedDescription)", dismissAfterDelay: 1.5)
Expand All @@ -152,9 +143,7 @@ class HomePresenter: NSObject {
}
}
do {
try defaultRealm.write {
self.group.dns = dns
}
try ConfigurationGroup.changeDNS(forGroupId: group.uuid, dns: dns)
}catch {
self.vc.showTextHUD("\("Fail to change dns".localized()): \((error as NSError).localizedDescription)", dismissAfterDelay: 1.5)
}
Expand All @@ -172,9 +161,9 @@ class HomePresenter: NSObject {
urlTextField = textField
}
alert.addAction(UIAlertAction(title: "OK".localized(), style: .Default, handler: { [unowned self] (action) in
if let input = urlTextField?.text {
if let newName = urlTextField?.text {
do {
try self.group.changeName(input)
try ConfigurationGroup.changeName(forGroupId: self.group.uuid, name: newName)
}catch {
Alert.show(self.vc, title: "Failed to change name", message: "\(error)")
}
Expand Down
Loading

0 comments on commit 05ede3a

Please sign in to comment.