Created
March 14, 2024 04:25
-
-
Save dqhieu/b344e1bb751b61e2d22d6eb44e7bea8c to your computer and use it in GitHub Desktop.
License View
This file contains hidden or 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
// | |
// LicenseView.swift | |
// CompressX | |
// | |
// Created by Dinh Quang Hieu on 06/01/2024. | |
// | |
import SwiftUI | |
struct LicenseView: View { | |
@ObservedObject var licenseManager = LicenseManager.shared | |
@State var licenseKey = "" | |
var body: some View { | |
Form { | |
if licenseManager.isValid { | |
if let status = licenseManager.licenseStatus { | |
HStack(spacing: 0) { | |
Text("License status") | |
Spacer() | |
if status == "active" { | |
Image(systemName: "checkmark.seal.fill").foregroundStyle(.green) | |
} else { | |
Image(systemName: "exclamationmark.triangle.fill").foregroundStyle(.orange) | |
} | |
Text(" \(status.capitalized)") | |
} | |
} | |
HStack(spacing: 0) { | |
Text("License usage") | |
Spacer() | |
Text("\(licenseManager.activation_usage)/\(licenseManager.activation_limit) used") | |
} | |
HStack(spacing: 0) { | |
Text("License expiry date") | |
Spacer() | |
Text(convertISO8601ToReadableDate(isoDate: licenseManager.expiryDate)) | |
} | |
HStack(spacing: 0) { | |
Text("License key") | |
Spacer(minLength: 0) | |
Text(licenseManager.licenseKey) | |
.textSelection(.enabled) | |
} | |
HStack { | |
Spacer() | |
Button(action: { | |
Task { | |
await licenseManager.deactivate() | |
} | |
}, label: { | |
Text("Unlink device") // when license expires, show something else | |
.foregroundStyle(.red) | |
}) | |
Spacer() | |
} | |
} else { | |
VStack(alignment: .leading) { | |
HStack { | |
Text("Enter your license key") | |
Spacer() | |
Link(destination: URL(string: "https://hieudinh.notion.site/How-to-find-your-license-key-9242c24a0b914213bdb3ab5d90c08d48?pvs=4")!, label: { | |
Text("How to get license key \(Image(systemName: "questionmark.circle"))") | |
}) | |
} | |
HStack { | |
TextField("", text: $licenseKey, prompt: Text("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")) | |
.textFieldStyle(.squareBorder) | |
.labelsHidden() | |
.disabled(licenseManager.isActivating) | |
.onAppear(perform: { | |
licenseKey = licenseManager.licenseKey | |
}) | |
Spacer() | |
if licenseManager.isActivating { | |
ProgressView() | |
.controlSize(.small) | |
} else { | |
Button { | |
Task { | |
await licenseManager.activate(key: licenseKey) | |
} | |
} label: { | |
Text("Activate") | |
} | |
.disabled(licenseManager.isActivating || licenseKey.isEmpty || licenseKey == licenseManager.licenseKey) | |
} | |
} | |
if !licenseManager.activateError.isEmpty { | |
Text(licenseManager.activateError) | |
.foregroundStyle(.red) | |
} | |
} | |
} | |
} | |
.frame(height: licenseManager.isValid ? 230 : !licenseManager.activateError.isEmpty ? 130 : 110) | |
} | |
} | |
#Preview { | |
LicenseView() | |
.formStyle(.grouped) | |
.frame(width: 520, height: 260, alignment: .center) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment