Skip to content

Commit

Permalink
Merge branch 'release/0.3.8'
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
  • Loading branch information
RealBonus committed Jun 1, 2018
2 parents 8b5b644 + c58c9e5 commit 4f89e14
Show file tree
Hide file tree
Showing 67 changed files with 1,528 additions and 707 deletions.
187 changes: 51 additions & 136 deletions Adamant.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "0940"
version = "2.0">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
8 changes: 8 additions & 0 deletions Adamant/Adamant.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
</dict>
</plist>
120 changes: 94 additions & 26 deletions Adamant/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

import UIKit
import Swinject
import CryptoSwift


// MARK: - Constants
extension String.adamantLocalized {
struct tabItems {
static let account = NSLocalizedString("Tabs.Account", comment: "Main tab bar: Account page")
Expand All @@ -17,14 +20,43 @@ extension String.adamantLocalized {
}
}

extension StoreKey {
struct application {
static let deviceTokenHash = "app.deviceTokenHash"

private init() {}
}
}


// MARK: - Resources
struct AdamantResources {
static let jsCore = Bundle.main.url(forResource: "adamant-core", withExtension: "js")!
static let coreDataModel = Bundle.main.url(forResource: "ChatModels", withExtension: "momd")!

static let servers = [
"https://endless.adamant.im",
"https://clown.adamant.im",
"https://lake.adamant.im"
]

static let iosAppSupportEmail = "[email protected]"

private init() {}
}


// MARK: - Application
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var repeater: RepeaterService!
var container: Container!

weak var accountService: AccountService?
weak var notificationService: NotificationsService?
// MARK: Dependencies
var accountService: AccountService!
var notificationService: NotificationsService!
var dialogService: DialogService!

// MARK: - Lifecycle

Expand All @@ -34,7 +66,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
container.registerAdamantServices()
accountService = container.resolve(AccountService.self)
notificationService = container.resolve(NotificationsService.self)
dialogService = container.resolve(DialogService.self)

// MARK: 2. Init UI
window = UIWindow(frame: UIScreen.main.bounds)
Expand Down Expand Up @@ -86,9 +118,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

switch reachability.connection {
case .cellular, .wifi:
dialogService.dissmisNoConnectionNotification()
break

case .none:
dialogService.showNoConnectionNotification()
repeater.pauseAll()
}

Expand All @@ -100,9 +134,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

switch connection {
case .cellular, .wifi:
self?.dialogService.dissmisNoConnectionNotification()
repeater.resumeAll()

case .none:
self?.dialogService.showNoConnectionNotification()
repeater.pauseAll()
}
}
Expand All @@ -120,25 +156,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}


// MARK: 6. Notifications
if let service = container.resolve(NotificationsService.self) {
if service.notificationsEnabled {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
} else {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalNever)
}

NotificationCenter.default.addObserver(forName: Notification.Name.AdamantNotificationService.showNotificationsChanged, object: service, queue: OperationQueue.main) { _ in
if service.notificationsEnabled {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
} else {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalNever)
}
}
}


// MARK: 7. Logout reset
// MARK: 6. Logout reset
NotificationCenter.default.addObserver(forName: Notification.Name.AdamantAccountService.userLoggedOut, object: nil, queue: OperationQueue.main) { [weak self] _ in
// On logout, pop all navigators to root.
guard let tbc = self?.window?.rootViewController as? UITabBarController, let vcs = tbc.viewControllers else {
Expand All @@ -163,9 +181,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
repeater.pauseAll()
}

// MARK: Notifications

func applicationDidBecomeActive(_ application: UIApplication) {
if accountService?.account != nil {
notificationService?.removeAllDeliveredNotifications()
if accountService.account != nil {
notificationService.removeAllDeliveredNotifications()
}

if let connection = container.resolve(ReachabilityMonitor.self)?.connection {
Expand All @@ -182,8 +202,56 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}

// MARK: - Remote notifications
extension AppDelegate {
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
guard let address = accountService.account?.address, let keypair = accountService.keypair else {
print("Trying to register with no user logged")
UIApplication.shared.unregisterForRemoteNotifications()
return
}

let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()

// Checking, if device token had not changed
guard let securedStore = container.resolve(SecuredStore.self) else {
fatalError("can't get secured store to get device token hash")
}

let tokenHash = token.md5()

if let savedHash = securedStore.get(StoreKey.application.deviceTokenHash), tokenHash == savedHash {
return
} else {
securedStore.set(tokenHash, for: StoreKey.application.deviceTokenHash)
}

// Storing new token in blockchain
guard let apiService = container.resolve(ApiService.self) else {
fatalError("can't get api service to register device token")
}

apiService.store(key: "deviceToken", value: token, type: StateType.keyValue, sender: address, keypair: keypair) { [weak self] result in
switch result {
case .success:
return

case .failure(let error):
print("Failed to store device token: \(error)")
self?.notificationService?.setNotificationsMode(.disabled, completion: nil)
}
}
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
if let service = container.resolve(DialogService.self) {
service.showError(withMessage: String.localizedStringWithFormat(String.adamantLocalized.notifications.registerRemotesError, error.localizedDescription), error: error)
}
}
}


// MARK: - BackgroundFetch
// MARK: - Background Fetch
extension AppDelegate {
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let container = Container()
Expand All @@ -208,7 +276,7 @@ extension AppDelegate {

for service in services {
group.enter()
service.fetchBackgroundData(notificationService: notificationsService) { result in
service.fetchBackgroundData(notificationsService: notificationsService) { result in
defer {
group.leave()
}
Expand Down
23 changes: 23 additions & 0 deletions Adamant/Assets/Assets.xcassets/Icons/error.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "error.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 39 additions & 3 deletions Adamant/Assets/adamant-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -42501,12 +42501,19 @@ class Adamant {
var assetBytes = null

switch (transaction.type) {
case 0:
case 0: // Send
break
case 8:

case 8: // Chat message
assetBytes = this.chatGetBytes(transaction)
assetSize = assetBytes.length
break

case 9: // State
assetBytes = this.statesGetBytes(transaction)
assetSize = assetBytes.length
break

default:
alert('Not supported yet')
}
Expand Down Expand Up @@ -42578,7 +42585,7 @@ class Adamant {

static transactionSign (trs, keypair) {
var hash = Adamant.getHash(trs)
return Adamant.sign(hash, keypair).toString('hex')
return Adamant.sign(hash, keypair).toString('hex');
}

static chatGetBytes (trs) {
Expand All @@ -42596,6 +42603,7 @@ class Adamant {
var bb = new ByteBuffer(4 + 4, true)
bb.writeInt(trs.asset.chat.type)
bb.flip()

buf = Buffer.concat([buf, Buffer.from(bb.toBuffer())])
} catch (e) {
throw e
Expand All @@ -42604,6 +42612,34 @@ class Adamant {
return buf
}

static statesGetBytes (trs) {
if (!trs.asset.state.value) {
return null;
}
var buf;

try {
buf = Buffer.from([]);
var stateBuf = Buffer.from(trs.asset.state.value);
buf = Buffer.concat([buf, stateBuf]);

if (trs.asset.state.key) {
var keyBuf = Buffer.from(trs.asset.state.key);
buf = Buffer.concat([buf, keyBuf]);
}

var bb = new ByteBuffer(4 + 4, true);
bb.writeInt(trs.asset.state.type);
bb.flip();

buf = Buffer.concat([buf, Buffer.from(bb.toBuffer())]);
} catch (e) {
throw e;
}

return buf;
}

/**
* Creates a signature based on a hash and a keypair.
* @implements {sodium}
Expand Down
Loading

0 comments on commit 4f89e14

Please sign in to comment.