@@ -12,6 +12,31 @@ import CocoaLumberjackSwift
1212import RxSwift
1313
1414final class SyncToolbarController {
15+ private enum WarningLevel {
16+ case medium
17+ case critical
18+
19+ init ? ( errorCount: Int ) {
20+ if errorCount >= 10 {
21+ self = . critical
22+ } else if errorCount >= 5 {
23+ self = . medium
24+ } else {
25+ return nil
26+ }
27+ }
28+
29+ var color : UIColor {
30+ switch self {
31+ case . medium:
32+ return . systemYellow
33+
34+ case . critical:
35+ return . systemRed
36+ }
37+ }
38+ }
39+
1540 private static let finishVisibilityTime : RxTimeInterval = . seconds( 4 )
1641 private unowned let viewController : UIViewController
1742 private unowned let dbStorage : DbStorage
@@ -21,6 +46,7 @@ final class SyncToolbarController {
2146 private var toolbarBottom : NSLayoutConstraint !
2247 private var pendingErrors : [ Error ] ?
2348 private var timerDisposeBag : DisposeBag
49+ private var consecutiveErrorCount = 0
2450 private var toolbarIsHidden : Bool {
2551 return toolbarBottom. constant != 0
2652 }
@@ -54,7 +80,7 @@ final class SyncToolbarController {
5480 toolbarBottom = bottom
5581
5682 NSLayoutConstraint . activate ( [
57- toolbar. heightAnchor. constraint ( equalToConstant: 45 ) ,
83+ toolbar. heightAnchor. constraint ( equalToConstant: 50 ) ,
5884 toolbar. leadingAnchor. constraint ( equalTo: parent. view. leadingAnchor) ,
5985 toolbar. trailingAnchor. constraint ( equalTo: parent. view. trailingAnchor) ,
6086 bottom
@@ -80,6 +106,7 @@ final class SyncToolbarController {
80106 }
81107
82108 default :
109+ consecutiveErrorCount += 1
83110 pendingErrors = [ error]
84111 if toolbarIsHidden {
85112 setToolbar ( hidden: false , animated: true )
@@ -89,6 +116,7 @@ final class SyncToolbarController {
89116
90117 case . finished( let errors) :
91118 if errors. isEmpty {
119+ consecutiveErrorCount = 0
92120 pendingErrors = nil
93121 timerDisposeBag = DisposeBag ( )
94122 if !toolbarIsHidden {
@@ -97,6 +125,7 @@ final class SyncToolbarController {
97125 return
98126 }
99127
128+ consecutiveErrorCount += 1
100129 pendingErrors = errors
101130 if toolbarIsHidden {
102131 setToolbar ( hidden: false , animated: true )
@@ -299,20 +328,48 @@ final class SyncToolbarController {
299328 }
300329
301330 private func set( progress: SyncProgress ) {
302- let item = UIBarButtonItem ( customView: toolbarView ( with: text ( for: progress) ) )
331+ let item = UIBarButtonItem ( customView: toolbarView ( with: text ( for: progress) , warningLevel : . init ( errorCount : consecutiveErrorCount ) ) )
303332 toolbar. setItems ( [ item] , animated: false )
304333 }
305334
306- private func toolbarView( with text: String ) -> UIView {
335+ private func toolbarView( with text: String , warningLevel : WarningLevel ? = nil ) -> UIView {
307336 let textColor : UIColor = viewController. traitCollection. userInterfaceStyle == . light ? . black : . white
308- let button = UIButton ( frame: UIScreen . main. bounds)
309- button. titleLabel? . font = . preferredFont( forTextStyle: . footnote)
337+
338+ var configuration = UIButton . Configuration. plain ( )
339+ configuration. title = text
340+ configuration. titleLineBreakMode = . byWordWrapping
341+ configuration. baseForegroundColor = textColor
342+
343+ let titleFont : UIFont
344+ if let warningLevel {
345+ let symbolConfig = UIImage . SymbolConfiguration ( pointSize: 20 , weight: . medium)
346+ configuration. image = UIImage ( systemName: " exclamationmark.triangle " , withConfiguration: symbolConfig)
347+ configuration. imagePlacement = . leading
348+ configuration. imagePadding = 6
349+ configuration. imageColorTransformer = UIConfigurationColorTransformer { _ in warningLevel. color }
350+
351+ switch warningLevel {
352+ case . critical:
353+ let footnoteDescriptor = UIFontDescriptor . preferredFontDescriptor ( withTextStyle: . footnote)
354+ titleFont = . systemFont( ofSize: footnoteDescriptor. pointSize, weight: . medium)
355+
356+ case . medium:
357+ titleFont = . preferredFont( forTextStyle: . footnote)
358+ }
359+ } else {
360+ titleFont = . preferredFont( forTextStyle: . footnote)
361+ }
362+
363+ configuration. titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { container in
364+ var container = container
365+ container. font = titleFont
366+ return container
367+ }
368+
369+ let button = UIButton ( configuration: configuration)
370+ button. frame = UIScreen . main. bounds
310371 button. titleLabel? . adjustsFontSizeToFitWidth = true
311372 button. titleLabel? . numberOfLines = 2
312- button. setTitleColor ( textColor, for: . normal)
313- button. contentHorizontalAlignment = . center
314- button. contentVerticalAlignment = . center
315- button. setTitle ( text, for: . normal)
316373
317374 button
318375 . rx
0 commit comments