Skip to content

Commit

Permalink
Developer Alert + Kyoko fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cranci1 committed Apr 25, 2024
1 parent e1266df commit 9526561
Show file tree
Hide file tree
Showing 24 changed files with 591 additions and 55 deletions.
28 changes: 22 additions & 6 deletions AnimeGen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
13910EC92B80D5C2009BF17E /* nekos-best.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13910EC82B80D5C2009BF17E /* nekos-best.swift */; };
13910ECB2B80D5C8009BF17E /* waifu-pics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13910ECA2B80D5C8009BF17E /* waifu-pics.swift */; };
139A2E0A2BA4D642003F2598 /* kyoko.swift in Sources */ = {isa = PBXBuildFile; fileRef = 139A2E092BA4D642003F2598 /* kyoko.swift */; };
13A2B7902BD1615600B79DF7 /* Developer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13A2B78F2BD1615600B79DF7 /* Developer.swift */; };
13A2B7902BD1615600B79DF7 /* Hmtai-pref.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13A2B78F2BD1615600B79DF7 /* Hmtai-pref.swift */; };
13A325922B94D8A100F1C357 /* Secrets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13A325912B94D8A100F1C357 /* Secrets.swift */; };
13BE98C02B828B8000379AB7 /* nekosmoe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13BE98BF2B828B8000379AB7 /* nekosmoe.swift */; };
13C6CA732BD990E30046923C /* waifu-it.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13C6CA722BD990E30046923C /* waifu-it.swift */; };
13C6CA752BDA2FDF0046923C /* waifu-it-pref.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13C6CA742BDA2FDF0046923C /* waifu-it-pref.swift */; };
13C8011D2B94C5E900BFD198 /* HmtaiSender.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13C8011C2B94C5E900BFD198 /* HmtaiSender.swift */; };
13C8011F2B94CAD900BFD198 /* HmtaiReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13C8011E2B94CAD900BFD198 /* HmtaiReader.swift */; };
13CC95082B8BA40100B5705E /* ApiPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13CC95072B8BA40100B5705E /* ApiPage.swift */; };
Expand Down Expand Up @@ -74,9 +76,11 @@
13910EC82B80D5C2009BF17E /* nekos-best.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "nekos-best.swift"; sourceTree = "<group>"; };
13910ECA2B80D5C8009BF17E /* waifu-pics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "waifu-pics.swift"; sourceTree = "<group>"; };
139A2E092BA4D642003F2598 /* kyoko.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = kyoko.swift; sourceTree = "<group>"; };
13A2B78F2BD1615600B79DF7 /* Developer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Developer.swift; sourceTree = "<group>"; };
13A2B78F2BD1615600B79DF7 /* Hmtai-pref.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Hmtai-pref.swift"; sourceTree = "<group>"; };
13A325912B94D8A100F1C357 /* Secrets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Secrets.swift; sourceTree = "<group>"; };
13BE98BF2B828B8000379AB7 /* nekosmoe.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = nekosmoe.swift; sourceTree = "<group>"; };
13C6CA722BD990E30046923C /* waifu-it.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "waifu-it.swift"; sourceTree = "<group>"; };
13C6CA742BDA2FDF0046923C /* waifu-it-pref.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "waifu-it-pref.swift"; sourceTree = "<group>"; };
13C8011C2B94C5E900BFD198 /* HmtaiSender.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HmtaiSender.swift; sourceTree = "<group>"; };
13C8011E2B94CAD900BFD198 /* HmtaiReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HmtaiReader.swift; sourceTree = "<group>"; };
13CC95072B8BA40100B5705E /* ApiPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiPage.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -176,10 +180,10 @@
13877B1A2B82017A00251A60 /* Settings */ = {
isa = PBXGroup;
children = (
13C6CA762BDA3ABC0046923C /* Developer-Pref */,
13877B182B82001800251A60 /* SettingsPage.swift */,
13877B1D2B82024A00251A60 /* AboutPage.swift */,
13CC95072B8BA40100B5705E /* ApiPage.swift */,
13A2B78F2BD1615600B79DF7 /* Developer.swift */,
13CC95092B8BA43600B5705E /* API.xcassets */,
);
path = Settings;
Expand All @@ -197,6 +201,7 @@
13BE98BF2B828B8000379AB7 /* nekosmoe.swift */,
139A2E092BA4D642003F2598 /* kyoko.swift */,
13E2DC832BA5D2CF00320E2F /* purr.swift */,
13C6CA722BD990E30046923C /* waifu-it.swift */,
);
path = APIs;
sourceTree = "<group>";
Expand All @@ -212,6 +217,15 @@
path = Extensions;
sourceTree = "<group>";
};
13C6CA762BDA3ABC0046923C /* Developer-Pref */ = {
isa = PBXGroup;
children = (
13A2B78F2BD1615600B79DF7 /* Hmtai-pref.swift */,
13C6CA742BDA2FDF0046923C /* waifu-it-pref.swift */,
);
path = "Developer-Pref";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -295,6 +309,7 @@
1375B3EA2BB813DA00E3D128 /* Activity.swift in Sources */,
139A2E0A2BA4D642003F2598 /* kyoko.swift in Sources */,
13E2DC842BA5D2CF00320E2F /* purr.swift in Sources */,
13C6CA752BDA2FDF0046923C /* waifu-it-pref.swift in Sources */,
13910EC72B80D5B9009BF17E /* waifu-im.swift in Sources */,
13877B192B82001800251A60 /* SettingsPage.swift in Sources */,
13BE98C02B828B8000379AB7 /* nekosmoe.swift in Sources */,
Expand All @@ -305,9 +320,10 @@
130A80552B78C02E0028985F /* SceneDelegate.swift in Sources */,
13910ECB2B80D5C8009BF17E /* waifu-pics.swift in Sources */,
13877B1E2B82024A00251A60 /* AboutPage.swift in Sources */,
13C6CA732BD990E30046923C /* waifu-it.swift in Sources */,
13910EC52B80D5A6009BF17E /* pic-re.swift in Sources */,
1365E8202BB0112D005E0548 /* HistoryView.swift in Sources */,
13A2B7902BD1615600B79DF7 /* Developer.swift in Sources */,
13A2B7902BD1615600B79DF7 /* Hmtai-pref.swift in Sources */,
1365E8222BB011F6005E0548 /* ImageHistory.swift in Sources */,
13C8011D2B94C5E900BFD198 /* HmtaiSender.swift in Sources */,
131515712BAB284A00A1C770 /* Refresh-API-Button.swift in Sources */,
Expand Down Expand Up @@ -469,7 +485,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = AnimeGen/AnimeGen.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 27;
CURRENT_PROJECT_VERSION = 28;
DEVELOPMENT_TEAM = 399LMK6Q2Y;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = AnimeGen/Info.plist;
Expand Down Expand Up @@ -504,7 +520,7 @@
CODE_SIGN_ENTITLEMENTS = AnimeGen/AnimeGen.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 27;
CURRENT_PROJECT_VERSION = 28;
DEVELOPMENT_TEAM = 399LMK6Q2Y;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = AnimeGen/Info.plist;
Expand Down
Binary file not shown.
103 changes: 68 additions & 35 deletions AnimeGen/APIs/kyoko.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,70 +28,50 @@ extension ViewController {
let apiEndpoint = "\(endpointPrefix)\(randomCategory)"

guard let url = URL(string: apiEndpoint) else {
showAlert(withTitle: "Invalid URL", message: "Please wait, the api may be down.", viewController: self)
print("Invalid URL")
stopLoadingIndicator()
return
}

let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
let task = URLSession.shared.dataTask(with: url) { [weak self] (data, response, error) in
guard let self = self else { return }
DispatchQueue.main.async {
if let error = error {
self.showAlert(withTitle: "Error!", message: "\(error)", viewController: self)
print("Error: \(error)")
self.stopLoadingIndicator()
return
}

guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
self.showAlert(withTitle: "Invalid HTTP response", message: "Please wait, the api may be down.", viewController: self)
print("Invalid HTTP response")
self.stopLoadingIndicator()
return
}

if let data = data {
do {
if let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
if let jsonResponse = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let requestResult = jsonResponse["RequestResult"] as? [String: Any],
let imageUrlString = requestResult["url"] as? String,
let imageUrl = URL(string: imageUrlString) {

if let imageData = try? Data(contentsOf: imageUrl) {
if imageUrlString.lowercased().hasSuffix(".gif") {
if let animatedImage = UIImage.animatedImage(with: UIImage.gifData(data: imageData) ?? [], duration: 1.0) {
self.imageView.image = animatedImage
self.imageView.image = animatedImage
self.animateImageChange(with: animatedImage)
} else {
print("Failed to create animated image from GIF data.")
}
} else {
if let newImage = UIImage(data: imageData) {
self.imageView.image = newImage
self.animateImageChange(with: newImage)
self.addToHistory(image: newImage)
} else {
print("Failed to load image data.")
}
}

self.currentImageURL = imageUrlString

self.tagsLabel.isHidden = false

self.updateUIWithTags([randomCategory])

self.stopLoadingIndicator()

self.incrementCounter()
} else {
print("Failed to load image data.")
self.stopLoadingIndicator()
}

self.loadImage(from: imageUrl, withCategory: randomCategory)

} else {
self.showAlert(withTitle: "Error!", message: "Failed to parse JSON response or missing necessary data.", viewController: self)
print("Failed to parse JSON response or missing necessary data.")
self.stopLoadingIndicator()
}
} catch {
self.showAlert(withTitle: "Error!", message: "Failed to parse JSON response.", viewController: self)
print("Failed to parse JSON response:", error)
self.stopLoadingIndicator()
}
} else {
self.showAlert(withTitle: "Error!", message: "No data received from server.", viewController: self)
print("No data received from server.")
self.stopLoadingIndicator()
}
Expand All @@ -101,4 +81,57 @@ extension ViewController {
task.resume()
}

func loadImage(from url: URL, withCategory category: String) {
URLSession.shared.dataTask(with: url) { [weak self] (data, response, error) in
guard let self = self else { return }
if let error = error {
self.showAlert(withTitle: "Error!", message: "\(error)", viewController: self)
print("Error loading image:", error)
self.stopLoadingIndicator()
return
}

guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
self.showAlert(withTitle: "Invalid HTTP response", message: "Failed to load image.", viewController: self)
print("Invalid HTTP response")
self.stopLoadingIndicator()
return
}

if let data = data {
DispatchQueue.main.async {
if let image = UIImage(data: data) {
if url.absoluteString.lowercased().hasSuffix(".gif") {
if let animatedImage = UIImage.animatedImage(with: UIImage.gifData(data: data) ?? [], duration: 1.0) {
self.imageView.image = animatedImage
self.addToHistory(image: animatedImage)
self.animateImageChange(with: animatedImage)
} else {
self.showAlert(withTitle: "Error!", message: "Failed to create animated image from GIF data.", viewController: self)
print("Failed to create animated image from GIF data.")
}
} else {
self.imageView.image = image
self.addToHistory(image: image)
self.animateImageChange(with: image)
}

self.tagsLabel.isHidden = false
self.updateUIWithTags([category])
self.currentImageURL = url.absoluteString
self.stopLoadingIndicator()
self.incrementCounter()
} else {
self.showAlert(withTitle: "Error!", message: "Failed to load image data.", viewController: self)
print("Failed to load image data.")
self.stopLoadingIndicator()
}
}
} else {
self.showAlert(withTitle: "Error!", message: "No image data received from server.", viewController: self)
print("No image data received from server.")
self.stopLoadingIndicator()
}
}.resume()
}
}
29 changes: 29 additions & 0 deletions AnimeGen/APIs/nekos-best.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ extension ViewController {
let apiEndpoint = "https://nekos.best/api/v2/\(randomCategory)"

guard let url = URL(string: apiEndpoint) else {

if self.alert {
self.showAlert(withTitle: "Invalid URL", message: "Please wait, the api may be down.", viewController: self)
}

print("Invalid URL")
stopLoadingIndicator()
return
Expand All @@ -29,18 +34,33 @@ extension ViewController {
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
if let error = error {

if self.alert {
self.showAlert(withTitle: "Error!", message: "\(error)", viewController: self)
}

print("Error: \(error)")
self.stopLoadingIndicator()
return
}

guard let httpResponse = response as? HTTPURLResponse else {

if self.alert {
self.showAlert(withTitle: "Invalid HTTP response", message: "Please wait, the api may be down.", viewController: self)
}

print("Invalid HTTP response")
self.stopLoadingIndicator()
return
}

guard httpResponse.statusCode == 200 else {

if self.alert {
self.showAlert(withTitle: "Invalid status code", message: "\(httpResponse.statusCode)", viewController: self)
}

print("Invalid status code: \(httpResponse.statusCode)")
self.stopLoadingIndicator()
return
Expand Down Expand Up @@ -68,12 +88,21 @@ extension ViewController {
self.stopLoadingIndicator()
self.incrementCounter()
} else {

if self.alert {
self.showAlert(withTitle: "Error!", message: "Failed to load image data.", viewController: self)
}

print("Failed to load image data.")
self.stopLoadingIndicator()
}
} else {
print("Failed to parse JSON response or missing necessary data.")
self.stopLoadingIndicator()

if self.alert {
self.showAlert(withTitle: "Error!", message: "Failed to parse JSON response or missing data.", viewController: self)
}
}
}
}
Expand Down
Loading

0 comments on commit 9526561

Please sign in to comment.