Advertisement
greedydev

Untitled

Aug 23rd, 2024
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.63 KB | None | 0 0
  1. import SwiftUI
  2. import Defaults
  3.  
  4. private let starsCount = 7
  5. private let BG_GRAD_DARK = Color.hex("FFB13D")
  6. private let BG_GRAD_LIGHT = Color.hex("FFCC02")
  7. private let HANG_ANIM = Animation.spring(response: 0.3, dampingFraction: 0.5)
  8. private let ROT_ANIM = Animation.spring(response: 0.4, dampingFraction: 0.25)
  9.  
  10. struct Onboarding: View {
  11.   @State private var currentTab = 0
  12.   @State var showStars = false
  13.   @State var hanging = true
  14.   @State var hidden = false
  15.   @State var twisted = false
  16.   @State var appID = ""
  17.   @State var appSecret = ""
  18.   @Environment (\.colorScheme) var colorScheme: ColorScheme
  19.  
  20.   @State var tryingToDismiss = false
  21.  
  22.   func nextStep() {
  23.     withAnimation(.spring()) {
  24.       currentTab += 1
  25.     }
  26.   }
  27.  
  28.   func prevStep() {
  29.     withAnimation(.spring()) {
  30.       currentTab -= 1
  31.     }
  32.   }
  33.  
  34.   var body: some View {
  35.     let BG_GRAD = colorScheme == .dark ? BG_GRAD_DARK : BG_GRAD_LIGHT
  36.     TabView(selection: $currentTab) {
  37.       OnboardingWelcomeWrapper(nextStep: nextStep)
  38.         .frame(maxWidth: .infinity, maxHeight: .infinity)
  39.         .contentShape(Rectangle())
  40.         .simultaneousGesture(DragGesture())
  41.         .tag(0)
  42.       OnboardingAPIIntro(prevStep: prevStep, nextStep: nextStep)
  43.         .frame(maxWidth: .infinity, maxHeight: .infinity)
  44.         .contentShape(Rectangle())
  45.         .simultaneousGesture(DragGesture())
  46.         .tag(1)
  47.       Onboarding1OpeningSettings(prevStep: prevStep, nextStep: nextStep)
  48.         .frame(maxWidth: .infinity, maxHeight: .infinity)
  49.         .contentShape(Rectangle())
  50.         .simultaneousGesture(DragGesture())
  51.         .tag(2)
  52.       Onboarding2CreateApp(prevStep: prevStep, nextStep: nextStep)
  53.         .frame(maxWidth: .infinity, maxHeight: .infinity)
  54.         .contentShape(Rectangle())
  55.         .simultaneousGesture(DragGesture())
  56.         .tag(3)
  57.       Onboarding3FillingInfo(prevStep: prevStep, nextStep: nextStep)
  58.         .frame(maxWidth: .infinity, maxHeight: .infinity)
  59.         .contentShape(Rectangle())
  60.         .simultaneousGesture(DragGesture())
  61.         .tag(4)
  62.       Onboarding4GettingAppID(prevStep: prevStep, nextStep: nextStep, appID: $appID)
  63.         .frame(maxWidth: .infinity, maxHeight: .infinity)
  64.         .contentShape(Rectangle())
  65.         .simultaneousGesture(DragGesture())
  66.         .tag(5)
  67.       Onboarding5GettingSecret(prevStep: prevStep, nextStep: nextStep, appSecret: $appSecret)
  68.         .frame(maxWidth: .infinity, maxHeight: .infinity)
  69.         .contentShape(Rectangle())
  70.         .simultaneousGesture(DragGesture())
  71.         .tag(6)
  72.       Onboarding6Auth(prevStep: prevStep, nextStep: nextStep, appSecret: appSecret, appID: appID)
  73.         .frame(maxWidth: .infinity, maxHeight: .infinity)
  74.         .contentShape(Rectangle())
  75.         .simultaneousGesture(DragGesture())
  76.         .tag(7)
  77.       Onboarding7Ending()
  78.         .frame(maxWidth: .infinity, maxHeight: .infinity)
  79.         .contentShape(Rectangle())
  80.         .simultaneousGesture(DragGesture())
  81.         .tag(8)
  82.     }
  83.     .alert(
  84.       "Are you sure?",
  85.       isPresented: $tryingToDismiss
  86.     ) {
  87.       Button(role: .destructive) {
  88.         Defaults[.GeneralDefSettings].onboardingState = .dismissed
  89.         Nav.present(nil)
  90.       } label: {
  91.         Text("Yes").fontWeight(.medium)
  92.       }
  93.     } message: {
  94.       Text("Do you really wanna dismiss the oboarding? You can reopen it later.")
  95.     }
  96.     .closeSheetBtn {
  97.       tryingToDismiss = true
  98.     }
  99.     .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
  100.     //    .padding(.bottom, 16)
  101.     .onChange(of: currentTab) { _ in withAnimation { UIApplication.shared.dismissKeyboard() } }
  102.     .background(
  103.       ZStack {
  104.         if showStars {
  105.           ZStack {
  106.             ForEach(Array(0...starsCount - 1), id: \.self) { number in
  107.               Star(number: number)
  108.             }
  109.           }
  110.           .frame(maxWidth: .infinity, maxHeight: .infinity)
  111.           .opacity(1)
  112.           //                    .drawingGroup()
  113.         }
  114.       }
  115.         .frame(maxWidth: .infinity, maxHeight: 200)
  116.         .background(
  117.           LinearGradient(gradient: Gradient(stops: generateGradientStops(BG_GRAD)), startPoint: .top, endPoint: .bottom)
  118.             .frame(maxWidth: .infinity, maxHeight: colorScheme == .dark ? 175 : .infinity)
  119.             .opacity(colorScheme == .dark ? 0.5 : 1)
  120.           , alignment: .bottom
  121.         )
  122.         .allowsHitTesting(false)
  123.         .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
  124.     )
  125.     .ignoresSafeArea(edges: .bottom)
  126.     .onAppear {
  127.       showStars = true
  128.       withAnimation(HANG_ANIM) {
  129.         hanging = false
  130.       }
  131.       withAnimation(ROT_ANIM) {
  132.         twisted = true
  133.       }
  134.     }
  135.     .interactiveDismissDisabled(true)
  136.   }
  137. }
  138.  
  139.  
  140.  
  141. func generateGradientStops(_ color: Color) -> [Gradient.Stop] {
  142.   let opacities = [
  143.     0.0, 0.004, 0.014, 0.03, 0.051, 0.075, 0.102, 0.131, 0.159, 0.188, 0.215, 0.239, 0.26, 0.276, 0.286, 0.29
  144.   ]
  145.   let locations = [
  146.     0.0, 0.081, 0.155, 0.225, 0.29, 0.353, 0.412, 0.471, 0.529, 0.588, 0.647, 0.71, 0.775, 0.845, 0.919, 1.0
  147.   ]
  148.  
  149.   return zip(opacities, locations).map { Gradient.Stop(color: color.opacity($0.0), location: $0.1) }
  150. }
  151.  
  152. struct Star: View {
  153.   var number: Int
  154.   @State var duration = CGFloat.random(in: 1.25...2)
  155.   @State var up = false
  156.   @State var rotation: CGFloat = 180
  157.   @State var position = CGPoint(x: 0, y: 200 + 42)
  158.   @State var size = CGFloat.rand(10...25)
  159.   @State private var timer = Timer.publish(every: 1, on: .current, in: .common).autoconnect()
  160.   @State private var offset = CGFloat.rand(8...16) * ([-1, 1].randomElement())!
  161.   @Environment (\.colorScheme) var colorScheme: ColorScheme
  162.  
  163.   func move() {
  164.     withAnimation(.easeInOut(duration: duration)) {
  165.       offset = CGFloat.rand(8...16)
  166.       up.toggle()
  167.       rotation = CGFloat.random(in: -20...20)
  168.     }
  169.   }
  170.  
  171.   var body: some View {
  172.     Image(systemName: "star.fill")
  173.       .resizable()
  174.       .scaledToFit()
  175.       .frame(width: size, height: size)
  176.     //            .foregroundColor(Color(NSColor(hex: colorScheme == .dark ? "F9EFBA" : "fafafa")))
  177.       .foregroundColor(Color.hex(colorScheme == .dark ? "F9EFBA" : "7B733C"))
  178.       .rotationEffect(Angle(degrees: rotation))
  179.     //            .shadow(color: Color(NSColor(hex: colorScheme == .dark ? "FFA262" : "656565")), radius: size * 0.75, y: colorScheme == .dark ? 0 : size * 0.5)
  180.       .shadow(color: Color.hex(colorScheme == .dark ? "FFA262" : "A9A400"), radius: size * 0.75, y: colorScheme == .dark ? 0 : size * 0.5)
  181.       .shadow(color: Color.hex(colorScheme == .dark ? "FFA262" : "A9A400").opacity(0.75), radius: size * 0.75, y: colorScheme == .dark ? 0 : size * 0.5)
  182.       .offset(y: up ? offset : 0)
  183.       .position(position)
  184.       .onReceive(timer) { _ in
  185.         move()
  186.       }
  187.       .onAppear {
  188.         timer.upstream.connect().cancel()
  189.         position.x = (((.screenW - 48) / CGFloat(starsCount - 1)) * CGFloat(number)) + 24 + (CGFloat.rand(0...24) * ([-1, 1].randomElement())!)
  190.         DispatchQueue.main.asyncAfter(deadline: .now() + CGFloat.random(in: 0...0.75)) {
  191.           timer = Timer.publish(every: duration, on: .current, in: .common).autoconnect()
  192.           move()
  193.           withAnimation(.spring(response: CGFloat.random(in: 0.5...0.7), dampingFraction: 2)) {
  194.             position.y = CGFloat.rand(50...CGFloat(200 - Int(size) - 30))
  195.           }
  196.         }
  197.       }
  198.       .onDisappear { timer.upstream.connect().cancel() }
  199.   }
  200. }
  201.  
  202. extension CGFloat {
  203.   static func random(_ range1: ClosedRange<CGFloat>, _ range2: ClosedRange<CGFloat>) -> CGFloat {
  204.     let ranges = [range1, range2]
  205.     let selectedRange = ranges.randomElement()!
  206.     return CGFloat.random(in: selectedRange)
  207.   }
  208.   static func rand(_ range: ClosedRange<CGFloat>) -> CGFloat {
  209.     return CGFloat(arc4random_uniform(UInt32(range.upperBound - range.lowerBound) + 1) + UInt32(range.lowerBound))
  210.   }
  211. }
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement