implemented simpleView type in PasswordView

This commit is contained in:
neon443
2024-06-03 20:02:54 +01:00
parent a3d1a7783e
commit 30971c4ade
3 changed files with 310 additions and 122 deletions

View File

@@ -2,97 +2,102 @@ import SwiftUI
struct DiceView: View {
@State var generated: [Int] = [0]
@State var displayDies: [Int] = []
@State var displayMultiDieMode = ""
@State var multiDieMode = "plus"
@State var result = 0
@State var resultDescription = ""
@State var history: [Int] = []
@State var displayDies: [Int] = []
@State var displayMultiDieMode = ""
@State var multiDieMode = "plus"
@State var result = 0
@State var resultDescription = ""
@State var history: [Int] = []
let columns = [GridItem(.adaptive(minimum: 25, maximum: 75))]
@State var dies: Double = 1
var body: some View {
VStack {
List {
Section("Number of die") {
HStack {
Text(String(Int(dies)))
Slider(value: $dies, in: 1...10, step: 1)
}
}
Section("Multi die mode") {
Picker(selection: $multiDieMode, label: Text("ioug")) {
Image(systemName: "plus").tag("plus")
Image(systemName: "multiply").tag("multiply")
}.pickerStyle(SegmentedPickerStyle())
}
Section("Visual") {
HStack {
Spacer()
if displayDies.isEmpty {
Text("Results are visualised here when you press the generate button")
.font(.subheadline)
} else if displayDies.count != Int(dies) {
Text("Press Generate")
.font(.title)
} else {
LazyVGrid(columns: columns, spacing: 10) {
ForEach(0..<displayDies.count, id: \.self) { index in
Image(systemName: "die.face.\(index > displayDies.count ? Int.random(in: 1...6) : displayDies[index])")
.resizable()
.scaledToFit()
.frame(width: 50, height: 50)
if index != displayDies.count-1 {
Image(systemName: displayMultiDieMode)
.resizable()
.scaledToFit()
.frame(width: 10, height: 10)
}
}
}.padding()
}
Spacer()
}
}
Button("Generate") {
generated = rngN6DieArr(dies: Int(dies))
displayDies = generated
displayMultiDieMode = multiDieMode
result = arrCombine(arr: generated, combineMode: multiDieMode)
resultDescription = describeResult(inp: displayDies, combineMode: multiDieMode)
}
Text(String(result))
.bold()
.font(.largeTitle)
Text(resultDescription)
}
}
}
VStack {
List {
Section("Number of die") {
HStack {
Text(String(Int(dies)))
Slider(value: $dies, in: 1...10, step: 1)
}
}
Section("Multi die mode") {
Picker(selection: $multiDieMode, label: Text("ioug")) {
Image(systemName: "plus").tag("plus")
Image(systemName: "multiply").tag("multiply")
}.pickerStyle(SegmentedPickerStyle())
}
Section("Visual") {
HStack {
Spacer()
if displayDies.isEmpty {
Text("Results are visualised here when you press the generate button")
.font(.subheadline)
} else if displayDies.count != Int(dies) {
Text("Press Generate")
.font(.title)
} else {
LazyVGrid(columns: columns, spacing: 10) {
ForEach(0..<displayDies.count, id: \.self) { index in
Image(systemName: "die.face.\(index > displayDies.count ? Int.random(in: 1...6) : displayDies[index])")
.resizable()
.scaledToFit()
.frame(width: 50, height: 50)
if index != displayDies.count-1 {
Image(systemName: displayMultiDieMode)
.resizable()
.scaledToFit()
.frame(width: 10, height: 10)
}
}
}.padding()
}
Spacer()
}
}
Button("Generate") {
generated = rngN6DieArr(dies: Int(dies))
displayDies = generated
displayMultiDieMode = multiDieMode
result = arrCombine(arr: generated, combineMode: multiDieMode)
resultDescription = describeResult(inp: displayDies, combineMode: multiDieMode)
}
HStack {
Spacer()
Text(String(result))
.bold()
.font(.largeTitle)
Spacer()
}
Text(resultDescription)
}
.listRowSpacing(5)
}
}
}
func describeResult(inp: [Int], combineMode: String) -> String {
var len = inp.count-1
var result = ""
var symbol = ""
if inp.isEmpty {
return "0"
} else {
if combineMode == "plus" {
symbol = " + "
} else if combineMode == "multiply" {
symbol = " x "
}
for i in 0...len {
if i == len {
result += String(inp[i])
} else {
result += String(inp[i]) + symbol
}
}
result += " = " + String(arrCombine(arr: inp, combineMode: combineMode))
return result
}
let len = inp.count-1
var result = ""
var symbol = ""
if inp.isEmpty {
return "0"
} else {
if combineMode == "plus" {
symbol = " + "
} else if combineMode == "multiply" {
symbol = " x "
}
for i in 0...len {
if i == len {
result += String(inp[i])
} else {
result += String(inp[i]) + symbol
}
}
result += " = " + String(arrCombine(arr: inp, combineMode: combineMode))
return result
}
}
#Preview {
DiceView()
DiceView()
}

View File

@@ -11,39 +11,39 @@ struct RNGApp: App {
// array combiner, adds up all ints in an array
func totalIntArr(arr: [Int]) -> Int {
var total = 0
for num in arr {
total += num
}
return total
var total = 0
for num in arr {
total += num
}
return total
}
// array combiner, adds up all doubles in an array
func totalDoubleArr(arr: [Double]) -> Double {
var total: Double = 0
for num in arr {
total += num
}
return total
var total: Double = 0
for num in arr {
total += num
}
return total
}
// array combiner, adds or multiplies all ints in an array
func arrCombine(arr: [Int], combineMode: String) -> Int {
var output = 0
if combineMode == "plus" {
for num in arr {
output += num
}
} else if combineMode == "multiply" {
output = 1
for num in arr {
output *= num
}
} else {
print("invalid combineMode '\(combineMode)'. must be 'plus' or 'multiply', returning 0")
return 0
}
return output
var output = 0
if combineMode == "plus" {
for num in arr {
output += num
}
} else if combineMode == "multiply" {
output = 1
for num in arr {
output *= num
}
} else {
print("invalid combineMode '\(combineMode)'. must be 'plus' or 'multiply', returning 0")
return 0
}
return output
}
@@ -55,25 +55,40 @@ func rng(min: Int, max: Int, step: Int) -> Int {
}
func rng6Die() -> Int {
return Int.random(in: 1...6)
return Int.random(in: 1...6)
}
func rngCDie(min: Int, max: Int) -> Int {
return Int.random(in: min...max)
return Int.random(in: min...max)
}
func rngN6DieArr(dies: Int) -> [Int] {
var output: [Int] = []
for _ in 1...dies {
output.append(rng6Die())
}
return output
var output: [Int] = []
for _ in 1...dies {
output.append(rng6Die())
}
return output
}
func rngNCDie(dies: Int, min: Int, max: Int) -> [Int] {
var output: [Int] = []
for _ in 1...dies {
output.append(rngCDie(min: min, max: max))
}
return output
var output: [Int] = []
for _ in 1...dies {
output.append(rngCDie(min: min, max: max))
}
return output
}
func rngInt(len: Int) -> Int {
var result = ""
for _ in 1...len {
result += String(Int.random(in: 0...9))
}
let resultInt = Int(result)
return resultInt!
}
func rngString(len: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((1..<len).map{ _ in letters.randomElement()! })
}

View File

@@ -5,14 +5,182 @@
// Created by Nihaal on 30/05/2024.
//
//////////////////////////// IMPLEMENT A WORKING BUTTON
//////////////////////////// current one does nothing lol
import SwiftUI
struct PasswordView: View {
@State var viewType = "simple"
@State var passType = "code"
@State var codeLen = 4
@State var customLen: Double = 0
@State var customLenStr = ""
@State var generated = ""
@State var result = ""
@State var history: [Int] = []
@State var showPassCodeWordHelpSheet = false
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
VStack {
List {
Section("View mode") {
Picker("simple", selection: $viewType) {
Text("Simple").tag("simple")
Text("Advanced").tag("advanced")
}
.pickerStyle(.segmented)
}
Section("Generate a...") {
HStack {
Picker("code", selection: $passType) {
Text("Passcode").tag("code")
Text("Password").tag("word")
}
.pickerStyle(.segmented)
}
HStack {
Spacer()
Text("Only numbers 0-9")
Spacer()
Divider()
Spacer()
Text("Letters a-z, A-Z and numbers 0-9")
Spacer()
}
}
Section("Length") {
Picker("", selection: $codeLen) {
Text("4").tag(4)
Text("6").tag(6)
Text(customLen == 0 ? "Custom" : String(Int(customLen))).tag(-1)
}
.pickerStyle(.segmented)
if codeLen == -1 {
HStack {
Slider(value: $customLen, in: 1...20, step: 1)
TextField("Enter a num", text: $customLenStr)
.keyboardType(.numberPad)
.onChange(of: customLen) { newValue in
customLenStr = String(Int(customLen))
}
.onChange(of: customLenStr) { newValue in
customLen = Double(newValue)!
}
.textFieldStyle(RoundedBorderTextFieldStyle())
}
} else {
}
}
Section("Result") {
HStack {
Spacer()
Text(String(result))
Spacer()
}
HStack {
Spacer()
Text(String(generated))
Spacer()
}
}
Button("Generate") {
if viewType == "simple" {
if passType == "code" {
if codeLen == 4 {
generated = genPass(type: "4Int", min: nil, max: nil)
} else if codeLen == 6 {
generated = genPass(type: "6Int", min: nil, max: nil)
}
} else if passType == "word" {
if codeLen == 4 {
generated = genPass(type: "4String", min: nil, max: nil)
} else if codeLen == 6 {
generated = genPass(type: "6String", min: nil, max: nil)
}
}
} else if viewType == "word" {
}
}
}
}
}
}
struct PassCodeWordHelpView: View {
@Environment(\.presentationMode) var presentationMode
@Environment(\.dismiss) var dismiss
var body: some View {
VStack {
Text(" ")
HStack {
Spacer()
Text("Passcode")
.font(.headline)
Spacer()
Text("Password")
.font(.headline)
Spacer()
}
HStack {
List {
Text("iopgt")
Text("eiou")
}
.listStyle(GroupedListStyle())
.padding(-10)
List {
Text("wi")
}
.listStyle(GroupedListStyle())
.padding(-10)
}
}
}
}
func genPass(type: String, min: Int?, max: Int?) -> String {
var result = ""
if type == "4Int" {
result = String(rngInt(len: 4))
} else if type == "6Int" {
result = String(rngInt(len: 6))
} else if type == "4String" {
result = rngString(len: 4)
} else if type == "6String" {
result = rngString(len: 6)
} else if min != nil && max != nil {
if type == "Int" {
result = String(rngInt(len: max! - min!) + min!)
} else if type == "String" {
if min == nil {
//if there no min, use max as length
result = rngString(len: max!)
} else {
print("when type is String, leave min empty and use max as a length.")
print("returning -1")
}
}
} else {
print("invalid input")
print("type = Int: give min and max")
print("type = String: min = nil, max = length")
print("Preset: (type = 4Int/String, 6Int/String) and leave min, max = nil")
print(" 4Int: 0000-9999; 4 digit")
print(" 6Int: 000000-999999; 6 digit")
print(" 4String: a-z/0-9: _ _ _ _; 4 digit alphanumeric")
print(" 6String: a-z/0-9: _ _ _ _ _ _; 6 digit alphanumeric")
print("returning -1")
result = "-1"
}
return result
}
#Preview {
PasswordView()
}
#Preview {
PassCodeWordHelpView()
}