forked from fermoya/SwiftUIPager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
121 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// StateInstance.swift | ||
// SwiftUIPagerExample | ||
// | ||
// Created by Fernando Moya de Rivas on 29/11/20. | ||
// Copyright © 2020 Fernando Moya de Rivas. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// Workaround to use `StateObject` in _iOS 14_ and `ObservedObject` in _iOS 13_ | ||
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *) | ||
@propertyWrapper struct StateInstance<ObjectType>: DynamicProperty where ObjectType: ObservableObject { | ||
|
||
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *) | ||
@ObservedObject private var observedObject: ObjectType | ||
|
||
private var _stateObject: Any? | ||
|
||
@available(iOS 14.0, OSX 10.16, tvOS 14.0, watchOS 7.0, *) | ||
private var stateObject: ObjectType? { | ||
_stateObject as? ObjectType | ||
} | ||
|
||
var wrappedValue: ObjectType { | ||
get { | ||
if #available(iOS 14.0, OSX 10.16, tvOS 14.0, watchOS 7.0, *) { | ||
return stateObject ?? observedObject | ||
} else { | ||
return observedObject | ||
} | ||
} | ||
set { | ||
if #available(iOS 14.0, OSX 10.16, tvOS 14.0, watchOS 7.0, *) { | ||
self._stateObject = StateObject(wrappedValue: newValue) | ||
} | ||
observedObject = newValue | ||
} | ||
} | ||
|
||
init(wrappedValue: ObjectType) { | ||
if #available(iOS 14.0, OSX 10.16, tvOS 14.0, watchOS 7.0, *) { | ||
self._stateObject = StateObject(wrappedValue: wrappedValue) | ||
} | ||
observedObject = wrappedValue | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Pager+Helper.swift | ||
// SwiftUIPager | ||
// | ||
// Created by Fernando Moya de Rivas on 19/01/2020. | ||
// Copyright © 2020 Fernando Moya de Rivas. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// Workaround to avoid `Binding` updating after rest of `State` after drag ends | ||
/// More info [here](https://developer.apple.com/forums/thread/667988) and [here](https://developer.apple.com/forums/thread/667720) | ||
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *) | ||
class PagerModel: ObservableObject { | ||
|
||
@Published var page: Int | ||
|
||
init(page: Int) { | ||
self.page = page | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters