Skip to content

Instantly share code, notes, and snippets.

@EdensEnlightenment
Created June 25, 2020 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdensEnlightenment/ef276384e0608e7bc5c4997045da41ba to your computer and use it in GitHub Desktop.
Save EdensEnlightenment/ef276384e0608e7bc5c4997045da41ba to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
@State var babblingState = false // Bool to show/hide progress indicators
@State var userInput = "" // binding var to set textfield to show user input
@State var timeDisplay = "Time"
@State var iterationsDisplay = "Iterations"
@State var output = ""
@State var result = []
@State var babbleQueue: DispatchWorkItem?
@State var babbleProgress = 0.1
@State var babbleTotal = 0.2 // placeholders until proper values are passed
var body: some View {
VStack {
ZStack {
VStack { // Progress Indicators
ForEach(0..<2) { i in
Spacer()
}
if babblingState {
ProgressView("Babbling")
.padding()
ProgressView(value: babbleProgress, total: babbleTotal)
.padding()
} else {
ProgressView("Babbling")
.padding()
.hidden()
ProgressView(value: 50, total: 100)
.padding()
.hidden()
}
ForEach(0..<1) { i in
Spacer()
}
}
VStack { // Time/Iterations
ForEach(0..<1) { i in
Spacer()
}
Text(timeDisplay)
.font(.title)
.padding()
Text(iterationsDisplay)
.font(.subheadline)
.padding(.leading)
.padding(.trailing)
ForEach(0..<6) { i in
Spacer()
}
}
VStack { // Babble/Cancel Button
ForEach(0..<2) { i in
Spacer()
}
if babblingState {
Button(action: {
babblingState = false;
print("Cancelling");
babbleQueue?.cancel();
print("babbleQueue cancelled?")
}) {
Text("Cancel")
.foregroundColor(.red)
}
} else {
Button(action: {
babblingState = true;
babbleQueue = DispatchWorkItem {
print("babbleIt dispatching")
result = babbleIt(input: userInput)
output = result[2] as! String
timeDisplay = result[1] as! String
iterationsDisplay = result[0] as! String
babblingState = false
}
DispatchQueue.global(qos: .utility).async(execute: babbleQueue!)
print("Executing babbleQueue")
}) {
Text("Babble")
.foregroundColor(.green)
}
}
ForEach(0..<3) { i in
Spacer()
}
}
VStack { // Text Entry
TextField("Babble Here", text: $userInput)
.font(.largeTitle)
.multilineTextAlignment(.center)
.padding()
}
VStack { // History
ForEach(0..<12) { i in
Spacer()
}
Divider()
.padding()
ForEach(0..<1) { i in
Spacer()
}
Text("View History")
.padding(.top)
ForEach(0..<1) { i in
Spacer()
}
}
}
}
}
func babbleIt(input: String) -> Array<String> { // Func takes a String input and returns an array of strings
print("babbleIt beginning")
let startTime = Double(Date.timeIntervalSinceReferenceDate) // sets start time to the current time since refernceDate
var finishTime = 0.0 // sets finish time to 0.0 as placeholder
let sanitizedInput = input.lowercased()
let charArray = sanitizedInput.map( {String($0) }) // transforms input string into an array of characters corresponding to each letter of the string
var asciiArray: Array<UInt8> = [] // inits an empty array of ints
for i in charArray { // loops for each item in charArray
asciiArray.append(Character(i).asciiValue!) // takes the ascii numerical value of each character, and adds it to the ascii array
}
var babbleComplete = false // state var for determining when to stop iterating
var positionInBabble = 0 // state var for keeping track of where in the array the current loop is
var iterations = 0 // state var to hold how many times the loop resets
while !babbleComplete { // loops while babbleComplete is false
iterations += 1 // adds one to iteration value
//usleep(500)
babbleProgress = Double(positionInBabble)
iterationsDisplay = String(iterations)
let num = Int.random(in: 96...122) // generates random int in the ascii range of a-z/A-Z
if UInt8(num) == asciiArray[positionInBabble] { // if the generated number matches the value of the current array position, move up one object in the array
positionInBabble += 1
if positionInBabble == asciiArray.count { // if the position in the array is the final position, set the complete state to true, ending the loop. Otherwise, resets loop
babbleComplete = true
finishTime = Double(Date.timeIntervalSinceReferenceDate) // sets finish time to current time since referenceDate
}
} else {
positionInBabble = 0
}
}
let elapsedTime = (finishTime - startTime) // declares elapsedTime as the difference between the finish and starting dates, to establish time elapsed
print("babbleIt completed")
return [String(iterations), String(elapsedTime), input]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment