Skip to content

Commit

Permalink
just fixes + initial new method for apis request + loading
Browse files Browse the repository at this point in the history
  • Loading branch information
cranci1 committed May 13, 2024
1 parent 05fec18 commit 238a1c1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
Binary file not shown.
27 changes: 18 additions & 9 deletions AnimeGen/APIs Requests/pic-re.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extension ViewController {
func loadImageFromPicRe() {
startLoadingIndicator()

let startTime = DispatchTime.now()

DispatchQueue.global().async {
guard let url = URL(string: "https://pic.re/image") else {
print("Invalid URL")
Expand Down Expand Up @@ -49,18 +51,25 @@ extension ViewController {
let tags = imageTagsString.components(separatedBy: ",")

DispatchQueue.main.async {
self.addImageToHistory(image: newImage, tags: tags)
self.currentImageURL = imageUrlString
self.updateUIWithTags(tags)
self.addToHistory(image: newImage)
self.tagsLabel.isHidden = false
self.imageView.image = newImage
self.animateImageChange(with: newImage)
self.stopLoadingIndicator()
self.incrementCounter()
self.handleImageLoadingCompletion(with: newImage, tags: tags, imageUrlString: imageUrlString)

let endTime = DispatchTime.now()
let executionTime = endTime.uptimeNanoseconds - startTime.uptimeNanoseconds
print("Execution time: \(Double(executionTime) / 1_000_000_000) seconds")
}
}.resume()
}
}

private func handleImageLoadingCompletion(with newImage: UIImage, tags: [String], imageUrlString: String) {
addImageToHistory(image: newImage, tags: tags)
currentImageURL = imageUrlString
updateUIWithTags(tags)
addToHistory(image: newImage)
tagsLabel.isHidden = false
imageView.image = newImage
animateImageChange(with: newImage)
stopLoadingIndicator()
incrementCounter()
}
}
10 changes: 5 additions & 5 deletions AnimeGen/Extension/Tags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ extension ViewController {
if !tagsString.isEmpty {
tagsString += "\n"
}

tagsString += "Author: \(author)"
}

if let category = category, !category.isEmpty {
if !tagsString.isEmpty || (author != nil && !author!.isEmpty) {
if !tagsString.isEmpty || author != nil && !author!.isEmpty {
tagsString += "\n"
}

tagsString += "Category: \(category)"
}

Expand All @@ -45,6 +43,7 @@ extension ViewController {
tagsLabel.attributedText = attributedString
}


func startLoadingIndicator() {
activityIndicator.startAnimating()
}
Expand All @@ -53,6 +52,7 @@ extension ViewController {
activityIndicator.stopAnimating()
}


func animateFeedback() {
guard UserDefaults.standard.bool(forKey: "enableAnimations") else {
return
Expand All @@ -62,14 +62,14 @@ extension ViewController {
self.imageView.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
}) { _ in
UIView.animate(withDuration: 0.3) {
self.imageView.transform = CGAffineTransform.identity
self.imageView.transform = .identity
}
}
}

func animateImageChange(with newImage: UIImage) {
guard UserDefaults.standard.bool(forKey: "enableAnimations") else {
self.imageView.image = newImage
imageView.image = newImage
return
}

Expand Down
4 changes: 2 additions & 2 deletions AnimeGen/Settings/Developer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ class DeveloperPref: UITableViewController {

@IBAction func waifuit(_ sender: UITapGestureRecognizer) {
if let navigationController = self.navigationController,
!(navigationController.topViewController is UIHostingController<APIsSuppport>) {
!(navigationController.topViewController is UIHostingController<waifuitView>) {
let swiftUIView = waifuitView()
navigationController.pushViewController(UIHostingController(rootView: swiftUIView), animated: true)
}
}

@IBAction func hmtaipage(_ sender: UITapGestureRecognizer) {
if let navigationController = self.navigationController,
!(navigationController.topViewController is UIHostingController<APIsSuppport>) {
!(navigationController.topViewController is UIHostingController<HmtaiView>) {
let swiftUIView = HmtaiView()
navigationController.pushViewController(UIHostingController(rootView: swiftUIView), animated: true)
}
Expand Down
7 changes: 1 addition & 6 deletions AnimeGen/Settings/Developer/Hmtai-pref.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ struct HmtaiView: View {
var body: some View {
VStack {
VStack(spacing: 20) {
Text("Hmtai Preferences")
.font(.largeTitle)
.fontWeight(.semibold)
.frame(maxWidth: .infinity, alignment: .leading)

TextField("Bot DiscordTokenHere", text: $apiToken)
.padding()
.background(RoundedRectangle(cornerRadius: 10).stroke(Color.gray, lineWidth: 1))
Expand Down Expand Up @@ -73,7 +68,7 @@ struct HmtaiView: View {
Spacer()
}
.padding()
.navigationBarTitle("Hmtai Settings", displayMode: .inline)
.navigationBarTitle("Hmtai Settings")
}

private func saveValues() {
Expand Down
9 changes: 2 additions & 7 deletions AnimeGen/Settings/Developer/waifu-it-pref.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ struct waifuitView: View {

var body: some View {
VStack {
VStack(spacing: 20) {
Text("Waifu.it Preferences")
.font(.largeTitle)
.fontWeight(.semibold)
.frame(maxWidth: .infinity, alignment: .leading)

VStack(spacing: 20) {
TextField("API Token", text: $waifuittoken)
.padding()
.background(RoundedRectangle(cornerRadius: 10).stroke(Color.gray, lineWidth: 1))
Expand Down Expand Up @@ -52,7 +47,7 @@ struct waifuitView: View {
Spacer()
}
.padding()
.navigationBarTitle("Waifu.it Settings", displayMode: .inline)
.navigationBarTitle("Waifu.it Settings")

}

Expand Down

0 comments on commit 238a1c1

Please sign in to comment.