-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Summit Workshop - Part 2 #19
Draft
calvincestari
wants to merge
6
commits into
2024-summit-workshop/part-1
Choose a base branch
from
2024-summit-workshop/part-2
base: 2024-summit-workshop/part-1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d4b66b
Adds badge view for user trip count
calvincestari 98f0327
Adds query watcher
calvincestari 009976f
Adds local cache mutation
calvincestari 68610f4
Book trip cache mutation
calvincestari c483571
Cancel trip cache mutation
calvincestari 1513389
Refactor local cache mutation to fill all trip details
calvincestari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,39 @@ | ||
import SwiftUI | ||
|
||
struct TripBadgeView : View { | ||
|
||
private let size = 16.0 | ||
private let x = 20.0 | ||
private let y = 0.0 | ||
|
||
@StateObject private var viewModel = TripsBadgeViewModel() | ||
|
||
var body: some View { | ||
ZStack { | ||
Capsule() | ||
.fill(.red) | ||
.frame(width: size * widthMultplier(), height: size, alignment: .topTrailing) | ||
.position(x: x, y: y) | ||
|
||
Text("\(viewModel.count)") | ||
.foregroundColor(.white) | ||
.font(Font.caption) | ||
.position(x: x, y: y) | ||
} | ||
.task { | ||
viewModel.loadUserTrips() | ||
} | ||
} | ||
|
||
func widthMultplier() -> Double { | ||
let value = viewModel.count | ||
|
||
if value < 10 { | ||
return 1.0 | ||
} else if value < 100 { | ||
return 1.5 | ||
} else { | ||
return 2.0 | ||
} | ||
} | ||
} |
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,47 @@ | ||
import Apollo | ||
import KeychainSwift | ||
import RocketReserverAPI | ||
import SwiftUI | ||
|
||
class TripsBadgeViewModel: ObservableObject { | ||
|
||
private var watcher: GraphQLQueryWatcher<MeQuery>? | ||
|
||
@Published var count: Int = 0 | ||
@Published var appAlert: AppAlert? | ||
|
||
func loadUserTrips() { | ||
guard isLoggedIn(), watcher == nil else { | ||
return | ||
} | ||
|
||
watcher = Network.shared.apollo.watch( | ||
query: MeQuery(), cachePolicy: .returnCacheDataAndFetch | ||
) { [weak self] result in | ||
guard let self = self else { | ||
return | ||
} | ||
|
||
switch result { | ||
case.success(let graphQLResult): | ||
count = graphQLResult.data?.me?.trips.count ?? 0 | ||
|
||
if let errors = graphQLResult.errors { | ||
self.appAlert = .errors(errors: errors) | ||
} | ||
case .failure(let error): | ||
self.appAlert = .errors(errors: [error]) | ||
} | ||
} | ||
} | ||
|
||
deinit { | ||
watcher?.cancel() | ||
} | ||
|
||
private func isLoggedIn() -> Bool { | ||
let keychain = KeychainSwift() | ||
return keychain.get(LoginView.loginKeychainKey) != nil | ||
} | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
final/RocketReserverAPI/Sources/LocalCacheMutations/MeTripsLocalCacheMutation.graphql.swift
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,137 @@ | ||
// @generated | ||
// This file was automatically generated and should not be edited. | ||
|
||
@_exported import ApolloAPI | ||
|
||
public class MeTripsLocalCacheMutation: LocalCacheMutation { | ||
public static let operationType: GraphQLOperationType = .query | ||
|
||
public init() {} | ||
|
||
public struct Data: RocketReserverAPI.MutableSelectionSet { | ||
public var __data: DataDict | ||
public init(_dataDict: DataDict) { __data = _dataDict } | ||
|
||
public static var __parentType: any ApolloAPI.ParentType { RocketReserverAPI.Objects.Query } | ||
public static var __selections: [ApolloAPI.Selection] { [ | ||
.field("me", Me?.self), | ||
] } | ||
|
||
public var me: Me? { | ||
get { __data["me"] } | ||
set { __data["me"] = newValue } | ||
} | ||
|
||
public init( | ||
me: Me? = nil | ||
) { | ||
self.init(_dataDict: DataDict( | ||
data: [ | ||
"__typename": RocketReserverAPI.Objects.Query.typename, | ||
"me": me._fieldData, | ||
], | ||
fulfilledFragments: [ | ||
ObjectIdentifier(MeTripsLocalCacheMutation.Data.self) | ||
] | ||
)) | ||
} | ||
|
||
/// Me | ||
/// | ||
/// Parent Type: `User` | ||
public struct Me: RocketReserverAPI.MutableSelectionSet { | ||
public var __data: DataDict | ||
public init(_dataDict: DataDict) { __data = _dataDict } | ||
|
||
public static var __parentType: any ApolloAPI.ParentType { RocketReserverAPI.Objects.User } | ||
public static var __selections: [ApolloAPI.Selection] { [ | ||
.field("__typename", String.self), | ||
.field("trips", [Trip?].self), | ||
] } | ||
|
||
public var trips: [Trip?] { | ||
get { __data["trips"] } | ||
set { __data["trips"] = newValue } | ||
} | ||
|
||
public init( | ||
trips: [Trip?] | ||
) { | ||
self.init(_dataDict: DataDict( | ||
data: [ | ||
"__typename": RocketReserverAPI.Objects.User.typename, | ||
"trips": trips._fieldData, | ||
], | ||
fulfilledFragments: [ | ||
ObjectIdentifier(MeTripsLocalCacheMutation.Data.Me.self) | ||
] | ||
)) | ||
} | ||
|
||
/// Me.Trip | ||
/// | ||
/// Parent Type: `Launch` | ||
public struct Trip: RocketReserverAPI.MutableSelectionSet { | ||
public var __data: DataDict | ||
public init(_dataDict: DataDict) { __data = _dataDict } | ||
|
||
public static var __parentType: any ApolloAPI.ParentType { RocketReserverAPI.Objects.Launch } | ||
public static var __selections: [ApolloAPI.Selection] { [ | ||
.field("__typename", String.self), | ||
.field("isBooked", Bool.self), | ||
.fragment(LaunchListDetail.self), | ||
] } | ||
|
||
public var isBooked: Bool { | ||
get { __data["isBooked"] } | ||
set { __data["isBooked"] = newValue } | ||
} | ||
public var id: RocketReserverAPI.ID { | ||
get { __data["id"] } | ||
set { __data["id"] = newValue } | ||
} | ||
public var site: String? { | ||
get { __data["site"] } | ||
set { __data["site"] = newValue } | ||
} | ||
public var mission: Mission? { | ||
get { __data["mission"] } | ||
set { __data["mission"] = newValue } | ||
} | ||
|
||
public struct Fragments: FragmentContainer { | ||
public var __data: DataDict | ||
public init(_dataDict: DataDict) { __data = _dataDict } | ||
|
||
public var launchListDetail: LaunchListDetail { | ||
get { _toFragment() } | ||
_modify { var f = launchListDetail; yield &f; __data = f.__data } | ||
} | ||
} | ||
|
||
public init( | ||
isBooked: Bool, | ||
id: RocketReserverAPI.ID, | ||
site: String? = nil, | ||
mission: Mission? = nil | ||
) { | ||
self.init(_dataDict: DataDict( | ||
data: [ | ||
"__typename": RocketReserverAPI.Objects.Launch.typename, | ||
"isBooked": isBooked, | ||
"id": id, | ||
"site": site, | ||
"mission": mission._fieldData, | ||
], | ||
fulfilledFragments: [ | ||
ObjectIdentifier(MeTripsLocalCacheMutation.Data.Me.Trip.self), | ||
ObjectIdentifier(LaunchListDetail.self) | ||
] | ||
)) | ||
} | ||
|
||
public typealias Mission = LaunchListDetail.Mission | ||
} | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like there should be a way to just use the label's size here, but I don't know enough SwiftUI. Feels like there should be an equivalent to
UILabel.sizeToFit()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if you set the background of the
Text
as the capsule, that should automatically adjust the size.Also guess who's still subscribed to this repo 🙃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hahaha Hi Ellen! Thanks for the tip!