Skip to content

Commit

Permalink
Merge branch 'release/0.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
RealBonus committed Mar 13, 2018
2 parents cdf7296 + 69abc42 commit 2aec06e
Show file tree
Hide file tree
Showing 46 changed files with 691 additions and 340 deletions.
42 changes: 31 additions & 11 deletions Adamant.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

93 changes: 45 additions & 48 deletions Adamant/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate {


// MARK: 4 Autoupdate
let chatsProvider = container.resolve(ChatsProvider.self)!
repeater = RepeaterService()
repeater.registerForegroundCall(label: "chatsProvider", interval: 3, queue: DispatchQueue.global(qos: .utility), callback: chatsProvider.update)
if let chatsProvider = container.resolve(ChatsProvider.self),
let transfersProvider = container.resolve(TransfersProvider.self),
let accountService = container.resolve(AccountService.self) {
repeater.registerForegroundCall(label: "chatsProvider", interval: 3, queue: .global(qos: .utility), callback: chatsProvider.update)
repeater.registerForegroundCall(label: "transfersProvider", interval: 15, queue: .global(qos: .utility), callback: transfersProvider.update)
repeater.registerForegroundCall(label: "accountService", interval: 15, queue: .global(qos: .utility), callback: accountService.update)
} else {
fatalError("Failed to get chatsProvider")
}


// MARK: 4. Notifications
Expand Down Expand Up @@ -120,67 +127,57 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
notificationService?.removeAllDeliveredNotifications()
}
}

// MARK: Background fetch

}


// MARK: - BackgroundFetch
extension AppDelegate {
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let container = Container()
container.registerAdamantBackgroundFetchServices()

guard let securedStore = container.resolve(SecuredStore.self),
let apiService = container.resolve(ApiService.self),
let notificationsService = container.resolve(NotificationsService.self) else {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalNever)
completionHandler(.failed)
return
guard let notificationsService = container.resolve(NotificationsService.self) else {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalNever)
completionHandler(.failed)
return
}

guard let address = securedStore.get(StoreKey.chatProvider.address) else {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalNever)
completionHandler(.failed)
return
}
let services: [BackgroundFetchService] = [
container.resolve(ChatsProvider.self) as! BackgroundFetchService,
container.resolve(TransfersProvider.self) as! BackgroundFetchService
]

var lastHeight: Int64?
if let raw = securedStore.get(StoreKey.chatProvider.receivedLastHeight) {
lastHeight = Int64(raw)
} else {
lastHeight = nil
}
let group = DispatchGroup()
let semaphore = DispatchSemaphore(value: 1)
var results = [FetchResult]()

var notifiedCount = 0
if let raw = securedStore.get(StoreKey.chatProvider.notifiedLastHeight), let notifiedHeight = Int64(raw), let h = lastHeight {
if h < notifiedHeight {
lastHeight = notifiedHeight

if let raw = securedStore.get(StoreKey.chatProvider.notifiedMessagesCount), let count = Int(raw) {
notifiedCount = count
for service in services {
group.enter()
service.fetchBackgroundData(notificationService: notificationsService) { result in
defer {
group.leave()
}

semaphore.wait()
results.append(result)
semaphore.signal()
}
}

group.wait()

apiService.getChatTransactions(address: address, height: lastHeight, offset: nil) { result in
for result in results {
switch result {
case .success(let transactions):
if transactions.count > 0 {
let total = transactions.count + notifiedCount
securedStore.set(String(total), for: StoreKey.chatProvider.notifiedMessagesCount)

if let newLastHeight = transactions.map({$0.height}).sorted().last {
securedStore.set(String(newLastHeight), for: StoreKey.chatProvider.notifiedLastHeight)
}


notificationsService.showNotification(title: String.adamantLocalized.notifications.newMessageTitle, body: String.localizedStringWithFormat(String.adamantLocalized.notifications.newMessageBody, total), type: .newMessages(count: total))
completionHandler(.newData)
} else {
completionHandler(.noData)
}
case .newData:
completionHandler(.newData)
return

case .noData:
break

case .failure(_):
case .failed:
completionHandler(.failed)
return
}
}
}
}
}}
22 changes: 19 additions & 3 deletions Adamant/Assets/Base.lproj/Localizable.stringsdict
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,33 @@
<key>You have %d new message(s)</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@messages@</string>
<string>You have %#@messages@</string>
<key>messages</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>one</key>
<string>You have %d new message</string>
<string>%d new message</string>
<key>other</key>
<string>You have %d new messages</string>
<string>%d new messages</string>
</dict>
</dict>
<key>You have %d new transfer(s)</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>You have %#@transfers@</string>
<key>transfers</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>one</key>
<string>%d new transaction</string>
<key>other</key>
<string>%d new transactions</string>
</dict>
</dict>
</dict>
Expand Down
Binary file added Adamant/Assets/notification.mp3
Binary file not shown.
3 changes: 3 additions & 0 deletions Adamant/Assets/ru.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
Notifications: New message notification title */
"New message" = "Новое сообщение";

/* Notifications: New transfer transaction title */
"New transfer" = "Новый перевод";

/* ApiService: No connection message. Generally bad network. */
"No connection" = "Нет соединения с интернетом";

Expand Down
26 changes: 22 additions & 4 deletions Adamant/Assets/ru.lproj/Localizable.stringsdict
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,37 @@
<key>You have %d new message(s)</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@messages@</string>
<string>У вас %#@messages@</string>
<key>messages</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>one</key>
<string>У вас %d новое сообщение</string>
<string>%d новое сообщение</string>
<key>many</key>
<string>У вас %d новых сообщений</string>
<string>%d новых сообщений</string>
<key>other</key>
<string>У вас %d новых сообщения</string>
<string>%d новых сообщения</string>
</dict>
</dict>
<key>You have %d new transfer(s)</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>У вас %#@transfers@</string>
<key>transfers</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>one</key>
<string>%d новая транзакция</string>
<key>many</key>
<string>%d новых транзакций</string>
<key>other</key>
<string>%d новые транзакции</string>
</dict>
</dict>
</dict>
Expand Down
6 changes: 4 additions & 2 deletions Adamant/CoreData/BaseTransaction+CoreDataProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// BaseTransaction+CoreDataProperties.swift
// Adamant
//
// Created by Anokhov Pavel on 10.02.2018.
// Created by Anokhov Pavel on 12.03.2018.
// Copyright © 2018 Adamant. All rights reserved.
//
//
Expand All @@ -21,10 +21,12 @@ extension BaseTransaction {
@NSManaged public var date: NSDate?
@NSManaged public var fee: NSDecimalNumber?
@NSManaged public var height: Int64
@NSManaged public var isOutgoing: Bool
@NSManaged public var recipientId: String?
@NSManaged public var senderId: String?
@NSManaged public var transactionId: String?
@NSManaged public var type: Int16
@NSManaged public var isOutgoing: Bool
@NSManaged public var blockId: String?
@NSManaged public var confirmations: Int64

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="13772" systemVersion="17D102" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="BaseTransaction" representedClassName="BaseTransaction" isAbstract="YES" syncable="YES">
<attribute name="amount" optional="YES" attributeType="Decimal" defaultValueString="0" syncable="YES"/>
<attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
<attribute name="blockId" attributeType="String" syncable="YES"/>
<attribute name="confirmations" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
<attribute name="date" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
<attribute name="fee" optional="YES" attributeType="Decimal" defaultValueString="0" syncable="YES"/>
<attribute name="height" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
<attribute name="isOutgoing" attributeType="Boolean" usesScalarValueType="YES" syncable="YES"/>
Expand Down Expand Up @@ -35,13 +37,14 @@
<relationship name="transfers" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="TransferTransaction" inverseName="partner" inverseEntity="TransferTransaction" syncable="YES"/>
</entity>
<entity name="TransferTransaction" representedClassName="TransferTransaction" parentEntity="BaseTransaction" syncable="YES">
<attribute name="isUnread" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES" syncable="YES"/>
<relationship name="partner" maxCount="1" deletionRule="Nullify" destinationEntity="CoreDataAccount" inverseName="transfers" inverseEntity="CoreDataAccount" syncable="YES"/>
</entity>
<elements>
<element name="BaseTransaction" positionX="-9" positionY="99" width="128" height="180"/>
<element name="BaseTransaction" positionX="-9" positionY="99" width="128" height="210"/>
<element name="Chatroom" positionX="-45" positionY="108" width="128" height="120"/>
<element name="ChatTransaction" positionX="-18" positionY="27" width="128" height="120"/>
<element name="CoreDataAccount" positionX="0" positionY="81" width="128" height="150"/>
<element name="TransferTransaction" positionX="-18" positionY="63" width="128" height="60"/>
<element name="TransferTransaction" positionX="-18" positionY="63" width="128" height="75"/>
</elements>
</model>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// TransferTransaction+CoreDataProperties.swift
// Adamant
//
// Created by Anokhov Pavel on 07.02.2018.
// Created by Anokhov Pavel on 13.03.2018.
// Copyright © 2018 Adamant. All rights reserved.
//
//
Expand All @@ -17,6 +17,7 @@ extension TransferTransaction {
return NSFetchRequest<TransferTransaction>(entityName: "TransferTransaction")
}

@NSManaged public var isUnread: Bool
@NSManaged public var partner: CoreDataAccount?

}
19 changes: 19 additions & 0 deletions Adamant/Helpers/Decimal+adamant.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Decimal+adamant.swift
// Adamant
//
// Created by Anokhov Pavel on 11.03.2018.
// Copyright © 2018 Adamant. All rights reserved.
//

import Foundation

extension Decimal {
func shiftedFromAdamant() -> Decimal {
return Decimal(sign: self.isSignMinus ? .minus : .plus, exponent: AdamantUtilities.currencyExponent, significand: self)
}

func shiftedToAdamant() -> Decimal {
return Decimal(sign: self.isSignMinus ? .minus : .plus, exponent: 0, significand: self)
}
}
4 changes: 2 additions & 2 deletions Adamant/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.3</string>
<string>0.3.1</string>
<key>CFBundleVersion</key>
<string>16</string>
<string>17</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSCameraUsageDescription</key>
Expand Down
11 changes: 7 additions & 4 deletions Adamant/Models/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import Foundation

struct Account {
let address: String
var unconfirmedBalance: UInt64
var balance: UInt64
var unconfirmedBalance: Decimal
var balance: Decimal
let publicKey: String
let unconfirmedSignature: Int
let secondSignature: Int
Expand All @@ -37,14 +37,17 @@ extension Account: Decodable {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.address = try container.decode(String.self, forKey: .address)
self.unconfirmedBalance = UInt64(try container.decode(String.self, forKey: .unconfirmedBalance))!
self.balance = UInt64(try container.decode(String.self, forKey: .balance))!
self.unconfirmedSignature = try container.decode(Int.self, forKey: .unconfirmedSignature)
self.publicKey = try container.decode(String.self, forKey: .publicKey)
self.secondSignature = try container.decode(Int.self, forKey: .secondSignature)
self.secondPublicKey = try? container.decode(String.self, forKey: .secondPublicKey)
self.multisignatures = try? container.decode([String].self, forKey: .multisignatures)
self.uMultisignatures = try? container.decode([String].self, forKey: .uMultisignatures)

let unconfirmedBalance = Decimal(string: try container.decode(String.self, forKey: .unconfirmedBalance))!
self.unconfirmedBalance = unconfirmedBalance.shiftedFromAdamant()
let balance = Decimal(string: try container.decode(String.self, forKey: .balance))!
self.balance = balance.shiftedFromAdamant()
}
}

Expand Down
27 changes: 27 additions & 0 deletions Adamant/Models/AdamantMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// AdamantMessage.swift
// Adamant
//
// Created by Anokhov Pavel on 11.03.2018.
// Copyright © 2018 Adamant. All rights reserved.
//

import Foundation

/// Adamant message types
///
/// - text: Simple text message
enum AdamantMessage {
case text(String)
}

extension AdamantMessage {
static private let textFee = Decimal(sign: .plus, exponent: -3, significand: 1)

var fee: Decimal {
switch self {
case .text(let message):
return Decimal(ceil(Double(message.count) / 255.0)) * AdamantMessage.textFee
}
}
}
2 changes: 1 addition & 1 deletion Adamant/Models/ChatType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

/// - messageExpensive: Old message type, with 0.005 transaction fee
/// - message: new and main message type, with 0.001 transaction fee
enum ChatType: Int, Codable {
enum ChatType: Int16, Codable {
case messageOld = 0
case message = 1
}
Loading

0 comments on commit 2aec06e

Please sign in to comment.