damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

OnboardingSuggestionsView.swift (6061B)


      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                     .tag(0)
     43                 
     44                 PostView(
     45                     action: .posting(.user(model.damus_state.pubkey)),
     46                     damus_state: model.damus_state, 
     47                     prompt_view: {
     48                         AnyView(
     49                             HStack {
     50                                 Image(systemName: "sparkles")
     51                                 Text("Add your first post", comment: "Prompt given to the user during onboarding, suggesting them to write their first post")
     52                             }
     53                                 .foregroundColor(.secondary)
     54                                 .font(.callout)
     55                                 .padding(.top, 10)
     56                         )
     57                     },
     58                     placeholder_messages: self.first_post_examples,
     59                     initial_text_suffix: self.initial_text_suffix
     60                 )
     61                 .onReceive(handle_notify(.post)) { _ in
     62                     // NOTE: Even though PostView already calls `dismiss`, that is not guaranteed to work under deeply nested views.
     63                     // Thus, we should also call `dismiss` from here (a direct subview of a sheet), which is explicitly supported by Apple.
     64                     // See https://github.com/damus-io/damus/issues/1726 for more context and information
     65                     dismiss()
     66                 }
     67                 .tag(1)
     68             }
     69             .tabViewStyle(.page(indexDisplayMode: .never))
     70         }
     71     }
     72 }
     73 
     74 fileprivate struct SuggestedUsersPageView: View {
     75     var model: SuggestedUsersViewModel
     76     var next_page: (() -> Void)
     77     
     78     var body: some View {
     79         VStack {
     80             List {
     81                 ForEach(model.groups) { group in
     82                     Section {
     83                         ForEach(group.users, id: \.self) { pk in
     84                             if let user = model.suggestedUser(pubkey: pk) {
     85                                 SuggestedUserView(user: user, damus_state: model.damus_state)
     86                             }
     87                         }
     88                     } header: {
     89                         SuggestedUsersSectionHeader(group: group, model: model)
     90                     }
     91                 }
     92             }
     93             .listStyle(.plain)
     94             
     95             Spacer()
     96             
     97             Button(action: {
     98                 self.next_page()
     99             }) {
    100                 Text("Continue", comment: "Button to dismiss suggested users view and continue to the main app")
    101                     .frame(minWidth: 300, maxWidth: .infinity, alignment: .center)
    102             }
    103             .buttonStyle(GradientButtonStyle())
    104             .padding([.leading, .trailing], 24)
    105             .padding(.bottom, 16)
    106         }
    107     }
    108 }
    109 
    110 struct SuggestedUsersSectionHeader: View {
    111     let group: SuggestedUserGroup
    112     let model: SuggestedUsersViewModel
    113     var body: some View {
    114         HStack {
    115             Text(group.title.uppercased())
    116             Spacer()
    117             Button(NSLocalizedString("Follow All", comment: "Button to follow all users in this section")) {
    118                 model.follow(pubkeys: group.users)
    119             }
    120             .font(.subheadline.weight(.semibold))
    121         }
    122     }
    123 }
    124 
    125 struct SuggestedUsersView_Previews: PreviewProvider {
    126     static var previews: some View {
    127         OnboardingSuggestionsView(model: SuggestedUsersViewModel(damus_state: test_damus_state))
    128     }
    129 }