@@ -11,6 +11,36 @@ import RxMoya
1111import RxSwift
1212import Swiftz
1313
14+ /// A data structure that represents zoom scale
15+ struct ZoomScale : CustomStringConvertible {
16+ /// Scale in 1 based, 1 -> 100%
17+ let scale : Double
18+
19+ /// String representation of zoom scale,
20+ /// will automatically multiply the scale by 100
21+ var description : String {
22+ return String ( Int ( scale * 100 ) )
23+ }
24+
25+ /// Normalize the given scale .
26+ ///
27+ /// - parameter scale: Zoom scale, if the scale is greater than 10 then
28+ /// it's considered as 100 based scale (I believe no one wants to zoom in by 1000%)
29+ ///
30+ /// - returns: zoom scale with base 1 (1 -> 100%)
31+ static private func normalize( scale: Double ) -> Double {
32+ return scale > 10 ? ( scale / 100 ) : scale
33+ }
34+
35+ init ( scale: Double ) {
36+ self . scale = ZoomScale . normalize ( scale: scale)
37+ }
38+
39+ init ( scale: String ) {
40+ self . init ( scale: Double ( scale) !)
41+ }
42+ }
43+
1444struct PageSizeMargin {
1545 let previousSize : CGSize
1646 let currentSize : CGSize
@@ -66,7 +96,7 @@ struct ChapterPageCollectionViewModel {
6696 width: Config . chapterPageSize. width,
6797 height: Config . chapterPageSize. height
6898 ) )
69- fileprivate let _zoomScale = Variable < Double > ( 1.0 )
99+ fileprivate let _zoomScale = Variable ( ZoomScale ( scale : 1.0 ) )
70100 fileprivate let chapterVM : ChapterViewModel
71101
72102 init ( chapterViewModel: ChapterViewModel ) {
@@ -89,38 +119,34 @@ struct ChapterPageCollectionViewModel {
89119 . map { " \( $0) / \( _chapterPages. value. count) Pages " }
90120
91121 zoomIn
92- . filter {
93- _zoomScale. value < Config . chapterPageSize. maximumZoomScale
94- }
95122 . map {
96- _zoomScale. value + Config. chapterPageSize. zoomScaleStep
123+ ZoomScale ( scale : _zoomScale. value. scale + Config. chapterPageSize. zoomScaleStep)
97124 }
98125 . bindTo ( _zoomScale) ==> disposeBag
99126
100127 zoomOut
101128 . filter {
102- _zoomScale. value > Config . chapterPageSize. minimumZoomScale
129+ ( _zoomScale. value. scale - Config . chapterPageSize . zoomScaleStep ) > Config . chapterPageSize. minimumZoomScale
103130 }
104131 . map {
105- _zoomScale. value - Config. chapterPageSize. zoomScaleStep
132+ ZoomScale ( scale : _zoomScale. value. scale - Config. chapterPageSize. zoomScaleStep)
106133 }
107134 . bindTo ( _zoomScale) ==> disposeBag
108135
109136 _zoomScale
110137 . asObservable ( )
111- . map { scale in
138+ . map { zoom in
112139 CGSize (
113- width: Int ( Double ( Config . chapterPageSize. width) * scale) ,
114- height: Int ( Double ( Config . chapterPageSize. height) * scale)
140+ width: Int ( Double ( Config . chapterPageSize. width) * zoom . scale) ,
141+ height: Int ( Double ( Config . chapterPageSize. height) * zoom . scale)
115142 )
116143 }
117144 . bindTo ( _pageSize) ==> disposeBag
118145
119146
120147 zoomScale = _zoomScale
121148 . asDriver ( )
122- . map { $0 * 100 }
123- . map ( String . init)
149+ . map { $0. description }
124150
125151 invalidateLayout = _zoomScale
126152 . asDriver ( )
@@ -168,7 +194,7 @@ struct ChapterPageCollectionViewModel {
168194 _currentPageIndex. value = index
169195 }
170196
171- func setZoomScale( _ scale: Double ) {
172- _zoomScale. value = scale
197+ func setZoomScale( _ scale: String ) {
198+ _zoomScale. value = ZoomScale ( scale: scale )
173199 }
174200}
0 commit comments