Files
swiftGrammar/AIGrammar/View/GrammarCheckView.swift

187 lines
7.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// GrammarCheckView.swift
// AIGrammar
//
// Created by oscar on 2024/3/27.
//
import SwiftUI
import ToastUI
import FirebaseAnalytics
fileprivate struct BuyProView: View {
var onTryForFree: () -> Void
var body: some View {
HStack {
VStack(alignment: .leading, spacing: 10) {
Text("Update to Pro")
.font(.headline)
Text("Remove Character limits and ads. Use Grammar Check with higher quality.")
.font(.subheadline)
}
Spacer()
Button("Try for free") {
//
// VIP
onTryForFree()
//
Analytics.logEvent(globalAnalyticsEvents.eventEnterPurchase, parameters: [
globalAnalyticsEvents.keyPurchaseEntry: globalAnalyticsEvents.valEntryBuyProBtn as NSObject,
globalAnalyticsEvents.keyDeviceID: globalEnvironment.deviceID as NSObject
])
}
.padding(.vertical, 6) // 2/3
.padding(.horizontal, 20)
.background(Color.green)
.foregroundColor(.white)
.cornerRadius(5)
.font(.headline) //
.alignmentGuide(.bottom) { d in d[.bottom] } // VStack
}
.padding()
.background(Color.white)
.cornerRadius(5)
.padding(5)
}
}
enum ActiveSheet {
case camera, share
}
extension ActiveSheet: Identifiable {
var id: Self { self }
}
struct GrammarCheckView: View {
@EnvironmentObject var globalEnv: GlobalEnvironment //
//
@State private var textInput: String
@State private var inputResult : String
@State private var progressValue: Float = 0
@State private var showKeyboard: Bool = false
@State private var showBuyProView: Bool = true
@State private var showResult : Bool = false
@State var results : [GrammarRes]
@State private var showVIPPaymentView: Bool = false // VIP
//
@State private var isLoading = false //
@State private var showingToast = false // toast
@State private var toastText = ""
@State private var activeSheet: ActiveSheet?
@State private var isTextEditorFocused: Bool = false // InputView
// 使
init() {
let demoGrammarData = GrammarData.demoInstance()
self.textInput = demoGrammarData.inputText
self.inputResult = demoGrammarData.correctText
self.results = demoGrammarData.results
}
var body: some View {
NavigationView {
ZStack {
Color.pink.opacity(0.2).edgesIgnoringSafeArea(.all) //
VStack {
if showResult {
ResultView(textContent: $textInput, results: $results, showResult: $showResult, showKeyboard: $showKeyboard)
} else {
InputView(textInput: $textInput, progressValue: $progressValue, showKeyboard: $showKeyboard, showBuyProView: $showBuyProView, showResult: $showResult, results: $results, isLoading: $isLoading, showingToast: $showingToast, toastText: $toastText)
// .environment(\.isTextEditorFocused, $isTextEditorFocused) // InputView
}
if showBuyProView {
BuyProView(onTryForFree: {
showVIPPaymentView = true // VIP
})
//BuyProView()
}
}
.fullScreenCover(isPresented: $showVIPPaymentView) {
VIPPaymentView() // VIP
}
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
HStack {
Text("Grammar & Spell Checker")
.font(.headline) //
if globalEnv.isVip {
Image("vipimg") // 使
.resizable() // 使
.scaledToFit() //
.frame(width: 24, height: 24) //
.foregroundColor(.yellow)
.font(.subheadline) //
.offset(y: -1) //
}
}
}
}
.foregroundColor(.black)
.navigationBarItems(leading: Button(action: {
//
self.activeSheet = .camera
}) {
Image(systemName: "camera")
}, trailing: Button(action: {
//
self.activeSheet = .share
}) {
Image(systemName: "square.and.arrow.up")
})
.sheet(item: $activeSheet, onDismiss: {
// sheet
}) { item in
switch item {
case .camera:
CameraView(textInput: $textInput)
case .share:
ShareSheet(itemsToShare: [textInput])
}
}
.toast(isPresented: $showingToast, dismissAfter: globalEnvironment.toastPresentMsNormal) {
HStack {
Image(systemName: "exclamationmark.bubble")
.foregroundColor(.yellow)
Text(toastText)
.foregroundColor(.black)
}
.padding()
.background(Color.white)
.cornerRadius(8)
.shadow(radius: 10)
}
//
if isLoading {
LoadingView()
}
}
}
}
}
// MARK:
struct GrammarCheckView_Previews: PreviewProvider {
static var previews: some View {
GrammarCheckView()
.environmentObject(IAPManager()) // IAPManager
.environmentObject(globalEnvironment) // IAPManager
}
}