Skip to content

Instantly share code, notes, and snippets.

@DarrenHurst
Created January 27, 2024 01:23
Show Gist options
  • Save DarrenHurst/e75a9c1c79b11e25ce922b620729efe3 to your computer and use it in GitHub Desktop.
Save DarrenHurst/e75a9c1c79b11e25ce922b620729efe3 to your computer and use it in GitHub Desktop.
Churck Norris Redacted.
import Foundation
//Data Response and View Model
final class ChuckNorrisJoke: Codable {
var value: String?
var icon_url: String?
}
//Interactor
// Get a Joke from the API "https://api.chucknorris.io/jokes/random"
extension ChuckNorrisJoke {
func getJoke() async -> ChuckNorrisJoke {
do {
return try await Network().fetch(from: "https://api.chucknorris.io/jokes/random")
}
catch {
// error handle GENERIC Something went wrong
}
return self
}
}
import Foundation
import SwiftUI
//View
struct JokeText : View {
@State var joke: ChuckNorrisJoke = ChuckNorrisJoke()
@State var redacted: Bool = true
var jokeText: Text = Text("")
@State var view: any View = VStack {}
var body: some View {
GeometryReader { r in
List {
Section {
Text("Chuck Norris Jokes *beware NSFW")
Toggle("Redacted NSFW", isOn: $redacted)
.tint(.gray)
Text(joke.value ?? "no joke")
.foregroundColor(.white.opacity(redacted ? 0.07 : 1.0))
.accessibilityLabel(joke.value ?? "no joke")
.padding(20)
.background(
RoundedRectangle(cornerSize: CGSize(width: 20, height: 20)).fill(.gray.opacity(0.4))
)
.background(
RoundedRectangle(cornerSize: CGSize(width: 20, height: 20)).stroke(style: StrokeStyle(lineWidth: 2.0))
)
.onAppear() {
Task {
joke = await joke.getJoke()
}
}
.onTapGesture {
Task {
joke = await joke.getJoke()
}
}
//Redacted Overlay
.overlay {
if redacted {
Text("REDACTED").foregroundColor(.black).font(.headline)
}
}.background(
LinearGradient(colors: !redacted ? [.black] : [.black,.gray,.white,.white], startPoint: .leading, endPoint: .bottom)
.mask( RoundedRectangle(cornerSize: CGSize(width: 20, height: 20)).fill(.black.opacity(1.0))
))
}
.animation(.easeInOut, value: redacted)
.listRowSeparator(.hidden)
}
.scrollContentBackground(.hidden)
.listStyle(.plain)
.background(Color.blue.edgesIgnoringSafeArea(.all).opacity(0.15))
}
}
}
//Preview
struct JokeTextPreview: PreviewProvider {
static var previews: some View {
JokeText().padding(20)
}
}
protocol TableOptions {
var sectionSeperator: Visibility { get set }
}
enum TableListStyle: String, CaseIterable, Hashable {
case grouped = "Grouped"
case inset = "Inset"
case insetGrouped = "InsetGrouped"
case plain = "Plain"
case sidebar = "Sidebar"
// map to SwiftUI ListStyle
var style: any SwiftUI.ListStyle {
switch self {
case .grouped:
return .grouped
case .inset:
return .inset
case .insetGrouped:
return .insetGrouped
case .plain:
return .plain
case .sidebar:
return .sidebar
}
}
var displayName: String {
String(localized: String.LocalizationValue(self.rawValue))
}
}
struct Network {
func fetch<T: Codable>(from urlString: String) async throws -> T {
// url
guard let url = URL(string: urlString) else {
throw URLError(.badURL)
}
// session
let (data, response) = try await URLSession.shared.data(from: url)
//status
guard let status = (response as? HTTPURLResponse)?.statusCode, (200...299).contains(status) else {
throw URLError(.badServerResponse)
}
let result = try JSONDecoder().decode(T.self, from: data)
print(result)
return result
// decode
//return
}
}
@DarrenHurst
Copy link
Author

Simulator.Screen.Recording.-.iPhone.14.Pro.-.2024-01-26.at.20.19.36.mp4

@DarrenHurst
Copy link
Author

Discover the power of using an overlay as a redacted option to protect confidential information and safeguard your privacy.

** will play with this and how preloader animation on phase might work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment