Skip to content

Commit

Permalink
refactor time logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ERussel committed Aug 11, 2023
1 parent 878e2df commit 326ad06
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 19 deletions.
4 changes: 4 additions & 0 deletions novawallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
0CF193D12A843DA9003F12F6 /* StakingTypeBalanceFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF193D02A843DA9003F12F6 /* StakingTypeBalanceFactory.swift */; };
0CF193D32A84FC7C003F12F6 /* SelectedStakingViewModelFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF193D22A84FC7C003F12F6 /* SelectedStakingViewModelFactory.swift */; };
0CF193D52A861926003F12F6 /* PredefinedTimeShortcut.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF193D42A861925003F12F6 /* PredefinedTimeShortcut.swift */; };
0CF193D72A861D7E003F12F6 /* StartStakingInfoConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF193D62A861D7E003F12F6 /* StartStakingInfoConstants.swift */; };
0D5245ED354CC52A842C85A0 /* TransferConfirmViewLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AD8B98AB03AAF06AA891695 /* TransferConfirmViewLayout.swift */; };
0D8213272889988B78188D9A /* DAppWalletAuthInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 337EC62037D657258BCBC02F /* DAppWalletAuthInteractor.swift */; };
0DACB56C0BDD4C984FE3C15C /* AssetReceiveWireframe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C1179A25C22AF0875A1ADCD /* AssetReceiveWireframe.swift */; };
Expand Down Expand Up @@ -3919,6 +3920,7 @@
0CF193D02A843DA9003F12F6 /* StakingTypeBalanceFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StakingTypeBalanceFactory.swift; sourceTree = "<group>"; };
0CF193D22A84FC7C003F12F6 /* SelectedStakingViewModelFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectedStakingViewModelFactory.swift; sourceTree = "<group>"; };
0CF193D42A861925003F12F6 /* PredefinedTimeShortcut.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PredefinedTimeShortcut.swift; sourceTree = "<group>"; };
0CF193D62A861D7E003F12F6 /* StartStakingInfoConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StartStakingInfoConstants.swift; sourceTree = "<group>"; };
0D3FE2CE7F9F2836755DBA63 /* GovernanceUnlockConfirmProtocols.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = GovernanceUnlockConfirmProtocols.swift; sourceTree = "<group>"; };
0D65686560E2E6C18A5C34CB /* StartStakingInfoWireframe.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = StartStakingInfoWireframe.swift; sourceTree = "<group>"; };
0D6E67AD564867E121601F18 /* WalletsListPresenter.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = WalletsListPresenter.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -17039,6 +17041,7 @@
0E4D3F275296975CA39280D5 /* StartStakingInfoBasePresenter.swift */,
6F5788D702D2B2203949329E /* StartStakingInfoViewController.swift */,
694E15BA7E0C59E46D0A6612 /* StartStakingInfoViewFactory.swift */,
0CF193D62A861D7E003F12F6 /* StartStakingInfoConstants.swift */,
);
path = StartStakingInfo;
sourceTree = "<group>";
Expand Down Expand Up @@ -19624,6 +19627,7 @@
AEE4E34D25E915ED00D6DF31 /* RewardCalculatorServiceProtocol.swift in Sources */,
8499FEE627C105DB00712589 /* RMRKV2SyncService.swift in Sources */,
849013DD24A927E2008F705E /* KeystoreExtensions.swift in Sources */,
0CF193D72A861D7E003F12F6 /* StartStakingInfoConstants.swift in Sources */,
846FB02C28C74F9700CA5444 /* ParaStkYieldBoostState.swift in Sources */,
844B2E7C27C426A7000CC079 /* NftLocalSubscriptionHandler.swift in Sources */,
8487584B27F1834E00495306 /* ImageGalleryPresentable.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ extension TimeInterval {
for locale: Locale,
preposition: String? = nil,
separator: String = " ",
shortcutHandler: PredefinedTimeShortcutProtocol? = nil
shortcutHandler: PredefinedTimeShortcutProtocol? = nil,
roundsDown: Bool = true
) -> String {
if let shortcut = shortcutHandler?.getShortcut(for: self, locale: locale) {
if
let shortcut = shortcutHandler?.getShortcut(
for: self,
roundsDown: roundsDown,
locale: locale
) {
return shortcut
}

let days = daysFromSeconds
let hours = (self - TimeInterval(days).secondsFromDays).hoursFromSeconds
let (days, hours) = getDaysAndHours(roundingDown: roundsDown)

var components: [String] = []

Expand Down
26 changes: 26 additions & 0 deletions novawallet/Common/Extension/Foundation/TimeInterval+Time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,30 @@ extension TimeInterval {

return UInt64(nextHour).timeInterval
}

func getDaysAndHours(roundingDown: Bool) -> (Int, Int) {
let days = daysFromSeconds
let hours = (self - TimeInterval(days).secondsFromDays).hoursFromSeconds

if !roundingDown {
return roundUp(days: days, hours: hours)
} else {
return (days, hours)
}
}

private func roundUp(days: Int, hours: Int) -> (Int, Int) {
let diff = self - TimeInterval(days).secondsFromDays - TimeInterval(hours).secondsFromHours
let remainedMinutes = diff.minutesFromSeconds

guard remainedMinutes > TimeInterval.secondsInHour.minutesFromSeconds / 2 else {
return (days, hours)
}

guard TimeInterval(hours + 1).secondsFromHours.daysFromSeconds == 0 else {
return (days + 1, 0)
}

return (days, hours + 1)
}
}
14 changes: 4 additions & 10 deletions novawallet/Common/Helpers/PredefinedTimeShortcut.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import Foundation

protocol PredefinedTimeShortcutProtocol {
func getShortcut(for timeInterval: TimeInterval, locale: Locale) -> String?
func getShortcut(for timeInterval: TimeInterval, roundsDown: Bool, locale: Locale) -> String?
}

final class EverydayShortcut: PredefinedTimeShortcutProtocol {
func getShortcut(for timeInterval: TimeInterval, locale: Locale) -> String? {
let days = timeInterval.daysFromSeconds
func getShortcut(for timeInterval: TimeInterval, roundsDown: Bool, locale: Locale) -> String? {
let (days, hours) = timeInterval.getDaysAndHours(roundingDown: roundsDown)

guard days == 1 else {
return nil
}

let hours = (timeInterval - TimeInterval(days).secondsFromDays).hoursFromSeconds

guard hours == 0 else {
guard days == 1, hours == 0 else {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ struct StartStakingViewModelFactory: StartStakingViewModelFactoryProtocol {
) -> ParagraphView.Model {
let separator = R.string.localizable.commonAnd(preferredLanguages: locale.rLanguages)
let timePreposition = R.string.localizable.commonTimeIn(preferredLanguages: locale.rLanguages)
let time = nextEra.localizedDaysHours(for: locale, preposition: timePreposition, separator: separator)
let time = nextEra.localizedDaysHours(
for: locale,
preposition: timePreposition,
separator: separator,
roundsDown: false
)

let precision = chainAsset.assetDisplayInfo.assetPrecision
let textWithAccents: AccentTextModel

Expand Down Expand Up @@ -119,8 +125,10 @@ struct StartStakingViewModelFactory: StartStakingViewModelFactoryProtocol {
let unstakePeriodString = unstakePeriod.localizedDaysHours(
for: locale,
preposition: preposition,
separator: separator
separator: separator,
roundsDown: false
)

let text = R.string.localizable.stakingStartUnstake(unstakePeriodString, preferredLanguages: locale.rLanguages)
let textWithAccents = AccentTextModel(
text: text,
Expand All @@ -145,8 +153,10 @@ struct StartStakingViewModelFactory: StartStakingViewModelFactoryProtocol {
for: locale,
preposition: preposition,
separator: separator,
shortcutHandler: EverydayShortcut()
shortcutHandler: EverydayShortcut(),
roundsDown: false
)

let text: String

if let amount = amount {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ extension StartStakingInfoParachainPresenter: StartStakingInfoParachainInteracto
}

func didReceive(stakingDuration: ParachainStakingDuration) {
state.stakingDuration = stakingDuration
if shouldUpdateEraDuration(
for: stakingDuration.round,
oldValue: state.stakingDuration?.round
) {
state.stakingDuration = stakingDuration
}
}

func didReceive(rewardPaymentDelay: UInt32) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ final class StartStakingInfoRelaychainPresenter: StartStakingInfoBasePresenter {

extension StartStakingInfoRelaychainPresenter: StartStakingInfoRelaychainInteractorOutputProtocol {
func didReceive(eraCountdown: EraCountdown?) {
state.eraCountdown = eraCountdown
if shouldUpdateEraDuration(
for: eraCountdown?.eraTimeInterval,
oldValue: state.eraCountdown?.eraTimeInterval
) {
state.eraCountdown = eraCountdown
}
}

func didReceive(networkInfo: NetworkStakingInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ class StartStakingInfoBasePresenter: StartStakingInfoInteractorOutputProtocol, S
}
}

func shouldUpdateEraDuration(for newValue: TimeInterval?, oldValue: TimeInterval?) -> Bool {
guard let oldValue = oldValue else {
return true
}

guard let newValue = newValue else {
return false
}

if newValue > oldValue {
return true
} else {
return oldValue - newValue > StartStakingInfoConstants.eraDurationReduceThreshold
}
}

// swiftlint:disable:next function_body_length
func provideViewModel(state: StartStakingStateProtocol) {
self.state = state
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

enum StartStakingInfoConstants {
static let eraDurationReduceThreshold = TimeInterval(10).secondsFromMinutes
}

0 comments on commit 326ad06

Please sign in to comment.