OnboardingSuggestionsView.swift (6376B)
1 // 2 // OnboardingSuggestionsView.swift 3 // damus 4 // 5 // Created by klabo on 7/17/23. 6 // 7 8 import SwiftUI 9 10 fileprivate let first_post_example_1: String = NSLocalizedString("Hello everybody!\n\nThis is my first post on Damus, I am happy to meet you all 🤙. What’s up?\n\n#introductions", comment: "First post example given to the user during onboarding, as a suggestion as to what they could post first") 11 fileprivate let first_post_example_2: String = NSLocalizedString("This is my first post on Nostr 💜. I love drawing and folding Origami!\n\nNice to meet you all! #introductions #plebchain ", comment: "First post example given to the user during onboarding, as a suggestion as to what they could post first") 12 fileprivate let first_post_example_3: String = NSLocalizedString("For #Introductions! I’m a software developer.\n\nMy side interests include languages and I am striving to be a #polyglot - I am a native English speaker and can speak French, German and Japanese.", comment: "First post example given to the user during onboarding, as a suggestion as to what they could post first") 13 fileprivate let first_post_example_4: String = NSLocalizedString("Howdy! I’m a graphic designer during the day and coder at night, but I’m also trying to spend more time outdoors.\n\nHope to meet folks who are on their own journeys to a peaceful and free life!", comment: "First post example given to the user during onboarding, as a suggestion as to what they could post first") 14 15 struct OnboardingSuggestionsView: View { 16 17 @StateObject var model: SuggestedUsersViewModel 18 @State var current_page: Int = 0 19 let first_post_examples: [String] = [first_post_example_1, first_post_example_2, first_post_example_3, first_post_example_4] 20 let initial_text_suffix: String = "\n\n#introductions" 21 22 @Environment(\.dismiss) var dismiss 23 24 func next_page() { 25 withAnimation { 26 current_page += 1 27 } 28 } 29 30 var body: some View { 31 NavigationView { 32 TabView(selection: $current_page) { 33 SuggestedUsersPageView(model: model, next_page: self.next_page) 34 .navigationTitle(NSLocalizedString("Who to Follow", comment: "Title for a screen displaying suggestions of who to follow")) 35 .navigationBarTitleDisplayMode(.inline) 36 .navigationBarItems(leading: Button(action: { 37 self.next_page() 38 }, label: { 39 Text("Skip", comment: "Button to dismiss the suggested users screen") 40 .font(.subheadline.weight(.semibold)) 41 }) 42 .accessibilityIdentifier(AppAccessibilityIdentifiers.onboarding_sheet_skip_button.rawValue) 43 ) 44 .tag(0) 45 46 PostView( 47 action: .posting(.user(model.damus_state.pubkey)), 48 damus_state: model.damus_state, 49 prompt_view: { 50 AnyView( 51 HStack { 52 Image(systemName: "sparkles") 53 Text("Add your first post", comment: "Prompt given to the user during onboarding, suggesting them to write their first post") 54 } 55 .foregroundColor(.secondary) 56 .font(.callout) 57 .padding(.top, 10) 58 ) 59 }, 60 placeholder_messages: self.first_post_examples, 61 initial_text_suffix: self.initial_text_suffix 62 ) 63 .onReceive(handle_notify(.post)) { _ in 64 // NOTE: Even though PostView already calls `dismiss`, that is not guaranteed to work under deeply nested views. 65 // Thus, we should also call `dismiss` from here (a direct subview of a sheet), which is explicitly supported by Apple. 66 // See https://github.com/damus-io/damus/issues/1726 for more context and information 67 dismiss() 68 } 69 .tag(1) 70 } 71 .tabViewStyle(.page(indexDisplayMode: .never)) 72 } 73 } 74 } 75 76 fileprivate struct SuggestedUsersPageView: View { 77 var model: SuggestedUsersViewModel 78 var next_page: (() -> Void) 79 80 var body: some View { 81 VStack { 82 List { 83 ForEach(model.groups) { group in 84 Section { 85 ForEach(group.users, id: \.self) { pk in 86 if let user = model.suggestedUser(pubkey: pk) { 87 SuggestedUserView(user: user, damus_state: model.damus_state) 88 } 89 } 90 } header: { 91 SuggestedUsersSectionHeader(group: group, model: model) 92 } 93 } 94 } 95 .listStyle(.plain) 96 97 Spacer() 98 99 Button(action: { 100 self.next_page() 101 }) { 102 Text("Continue", comment: "Button to dismiss suggested users view and continue to the main app") 103 .frame(minWidth: 300, maxWidth: .infinity, alignment: .center) 104 } 105 .buttonStyle(GradientButtonStyle()) 106 .padding([.leading, .trailing], 24) 107 .padding(.bottom, 16) 108 } 109 } 110 } 111 112 struct SuggestedUsersSectionHeader: View { 113 let group: SuggestedUserGroup 114 let model: SuggestedUsersViewModel 115 var body: some View { 116 HStack { 117 let locale = Locale.current 118 let format = localizedStringFormat(key: group.category, locale: locale) 119 let categoryName = String(format: format, locale: locale) 120 Text(categoryName) 121 Spacer() 122 Button(NSLocalizedString("Follow All", comment: "Button to follow all users in this section")) { 123 model.follow(pubkeys: group.users) 124 } 125 .font(.subheadline.weight(.semibold)) 126 } 127 } 128 } 129 130 struct SuggestedUsersView_Previews: PreviewProvider { 131 static var previews: some View { 132 OnboardingSuggestionsView(model: SuggestedUsersViewModel(damus_state: test_damus_state)) 133 } 134 }