-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1665 from planetary-social/onboarding-username
#1598: Username screen in onboarding
- Loading branch information
Showing
7 changed files
with
277 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
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,20 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"color" : { | ||
"color-space" : "srgb", | ||
"components" : { | ||
"alpha" : "1.000", | ||
"blue" : "0x5D", | ||
"green" : "0x19", | ||
"red" : "0xFF" | ||
} | ||
}, | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
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,185 @@ | ||
import Dependencies | ||
import Logger | ||
import SwiftUI | ||
|
||
/// The possible states of ``UsernameView``. | ||
fileprivate enum UsernameViewState { | ||
case idle | ||
case loading | ||
case verificationFailed | ||
case claimed | ||
case errorAlert | ||
} | ||
|
||
/// The Username view in the onboarding. | ||
struct UsernameView: View { | ||
@Environment(OnboardingState.self) private var state | ||
@Environment(CurrentUser.self) private var currentUser | ||
@Environment(\.managedObjectContext) private var viewContext | ||
|
||
@Dependency(\.crashReporting) private var crashReporting | ||
@Dependency(\.namesAPI) private var namesAPI | ||
|
||
@State private var username = "" | ||
@State private var usernameState: UsernameViewState = .idle | ||
@State private var saveError: SaveProfileError? | ||
|
||
private var showAlert: Binding<Bool> { | ||
Binding { | ||
usernameState == .errorAlert | ||
} set: { _ in | ||
usernameState = .idle | ||
} | ||
} | ||
|
||
private var nextButtonDisabled: Bool { | ||
if username.isEmpty { | ||
return true | ||
} else if usernameState == .loading { | ||
return true | ||
} else if usernameState == .claimed { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
var body: some View { | ||
ZStack { | ||
Color.appBg | ||
.ignoresSafeArea() | ||
ViewThatFits(in: .vertical) { | ||
displayNameStack | ||
|
||
ScrollView { | ||
displayNameStack | ||
} | ||
} | ||
} | ||
.navigationBarHidden(true) | ||
.alert("errorConnecting", isPresented: showAlert) { | ||
Button { | ||
nextStep() | ||
} label: { | ||
Text("skipForNow") | ||
} | ||
} | ||
} | ||
|
||
var displayNameStack: some View { | ||
VStack(alignment: .leading, spacing: 20) { | ||
LargeNumberView(4) | ||
HStack(alignment: .firstTextBaseline) { | ||
Text("usernameHeadline") | ||
.font(.clarityBold(.title)) | ||
.foregroundStyle(Color.primaryTxt) | ||
Text("usernameNIP05Parenthetical") | ||
.font(.clarityRegular(.title2)) | ||
.foregroundStyle(Color.secondaryTxt) | ||
} | ||
Text("usernameDescription") | ||
.font(.body) | ||
.foregroundStyle(Color.secondaryTxt) | ||
HStack { | ||
TextField( | ||
"", | ||
text: $username, | ||
prompt: Text("usernamePlaceholder") | ||
.foregroundStyle(Color.textFieldPlaceholder) | ||
) | ||
.textInputAutocapitalization(.never) | ||
.foregroundStyle(Color.primaryTxt) | ||
.fontWeight(.bold) | ||
.autocorrectionDisabled() | ||
.padding() | ||
.withStyledBorder() | ||
|
||
Text("@nos.social") | ||
.fontWeight(.bold) | ||
.foregroundStyle(Color.secondaryTxt) | ||
} | ||
if usernameState == .verificationFailed { | ||
usernameAlreadyClaimedText | ||
} | ||
Spacer() | ||
BigActionButton("next") { | ||
await verifyAndSave() | ||
} | ||
.disabled(nextButtonDisabled) | ||
} | ||
.padding(40) | ||
.readabilityPadding() | ||
} | ||
|
||
var usernameAlreadyClaimedText: some View { | ||
Text("usernameNotAvailable") | ||
.font(.subheadline) | ||
.fontWeight(.medium) | ||
.foregroundStyle(Color.error) | ||
} | ||
|
||
func nextStep() { | ||
state.step = .buildYourNetwork | ||
} | ||
|
||
/// Checks whether the username is available and saves it. Updates `usernameState` based on the result. | ||
func verifyAndSave() async { | ||
usernameState = .loading | ||
|
||
guard !username.isEmpty, let keyPair = currentUser.keyPair else { | ||
usernameState = .errorAlert | ||
return | ||
} | ||
|
||
do { | ||
let verified = try await namesAPI.checkAvailability( | ||
username: username, | ||
publicKey: keyPair.publicKey | ||
) | ||
guard verified else { | ||
usernameState = .verificationFailed | ||
return | ||
} | ||
await save() | ||
} catch { | ||
Log.error(error.localizedDescription) | ||
usernameState = .verificationFailed | ||
} | ||
} | ||
|
||
/// Saves the username locally, publishes the metadata, and registers it. | ||
func save() async { | ||
usernameState = .loading | ||
|
||
guard let author = await currentUser.author, | ||
let keyPair = currentUser.keyPair else { | ||
usernameState = .errorAlert | ||
return | ||
} | ||
|
||
author.nip05 = "\(username)@nos.social" | ||
do { | ||
try viewContext.save() | ||
try await currentUser.publishMetadata() | ||
let relays = author.relays.compactMap { | ||
$0.addressURL | ||
} | ||
try await namesAPI.register( | ||
username: username, | ||
keyPair: keyPair, | ||
relays: relays | ||
) | ||
usernameState = .claimed | ||
nextStep() | ||
} catch { | ||
crashReporting.report(error) | ||
usernameState = .errorAlert | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
UsernameView() | ||
.environment(OnboardingState()) | ||
.inject(previewData: PreviewData()) | ||
} |