Skip to content

Commit

Permalink
better code and improved orgnization
Browse files Browse the repository at this point in the history
  • Loading branch information
cranci1 committed Feb 17, 2024
1 parent 380156d commit 5cec023
Show file tree
Hide file tree
Showing 11 changed files with 501 additions and 379 deletions.
71 changes: 69 additions & 2 deletions AnimeGen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
130A805A2B78C02E0028985F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 130A80582B78C02E0028985F /* Main.storyboard */; };
130A805C2B78C0300028985F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 130A805B2B78C0300028985F /* Assets.xcassets */; };
130A805F2B78C0300028985F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 130A805D2B78C0300028985F /* LaunchScreen.storyboard */; };
13910EBE2B80D380009BF17E /* ImageExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13910EBD2B80D380009BF17E /* ImageExtensions.swift */; };
13910EC02B80D396009BF17E /* UIExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13910EBF2B80D396009BF17E /* UIExtensions.swift */; };
13910EC52B80D5A6009BF17E /* pic-re.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13910EC42B80D5A6009BF17E /* pic-re.swift */; };
13910EC72B80D5B9009BF17E /* waifu-im.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13910EC62B80D5B9009BF17E /* waifu-im.swift */; };
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 */; };
13910ECF2B80D90D009BF17E /* SDWebImage in Frameworks */ = {isa = PBXBuildFile; productRef = 13910ECE2B80D90D009BF17E /* SDWebImage */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -24,13 +31,20 @@
130A805B2B78C0300028985F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
130A805E2B78C0300028985F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
130A80602B78C0300028985F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
13910EBD2B80D380009BF17E /* ImageExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageExtensions.swift; sourceTree = "<group>"; };
13910EBF2B80D396009BF17E /* UIExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIExtensions.swift; sourceTree = "<group>"; };
13910EC42B80D5A6009BF17E /* pic-re.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "pic-re.swift"; sourceTree = "<group>"; };
13910EC62B80D5B9009BF17E /* waifu-im.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "waifu-im.swift"; sourceTree = "<group>"; };
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>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
130A804C2B78C02E0028985F /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
13910ECF2B80D90D009BF17E /* SDWebImage in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -56,17 +70,39 @@
130A80512B78C02E0028985F /* AnimeGen */ = {
isa = PBXGroup;
children = (
13910ECC2B80D61F009BF17E /* Extensions */,
13910EC32B80D595009BF17E /* APIs */,
130A80522B78C02E0028985F /* AppDelegate.swift */,
130A80542B78C02E0028985F /* SceneDelegate.swift */,
130A80562B78C02E0028985F /* ViewController.swift */,
130A80582B78C02E0028985F /* Main.storyboard */,
130A805B2B78C0300028985F /* Assets.xcassets */,
130A80582B78C02E0028985F /* Main.storyboard */,
130A805D2B78C0300028985F /* LaunchScreen.storyboard */,
130A80562B78C02E0028985F /* ViewController.swift */,
130A80602B78C0300028985F /* Info.plist */,
);
path = AnimeGen;
sourceTree = "<group>";
};
13910EC32B80D595009BF17E /* APIs */ = {
isa = PBXGroup;
children = (
13910EC42B80D5A6009BF17E /* pic-re.swift */,
13910EC62B80D5B9009BF17E /* waifu-im.swift */,
13910ECA2B80D5C8009BF17E /* waifu-pics.swift */,
13910EC82B80D5C2009BF17E /* nekos-best.swift */,
);
path = APIs;
sourceTree = "<group>";
};
13910ECC2B80D61F009BF17E /* Extensions */ = {
isa = PBXGroup;
children = (
13910EBD2B80D380009BF17E /* ImageExtensions.swift */,
13910EBF2B80D396009BF17E /* UIExtensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -83,6 +119,9 @@
dependencies = (
);
name = AnimeGen;
packageProductDependencies = (
13910ECE2B80D90D009BF17E /* SDWebImage */,
);
productName = AnimeGen;
productReference = 130A804F2B78C02E0028985F /* AnimeGen.app */;
productType = "com.apple.product-type.application";
Expand Down Expand Up @@ -111,6 +150,9 @@
Base,
);
mainGroup = 130A80462B78C02E0028985F;
packageReferences = (
13910ECD2B80D90D009BF17E /* XCRemoteSwiftPackageReference "SDWebImage" */,
);
productRefGroup = 130A80502B78C02E0028985F /* Products */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -138,9 +180,15 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
13910EC72B80D5B9009BF17E /* waifu-im.swift in Sources */,
130A80572B78C02E0028985F /* ViewController.swift in Sources */,
130A80532B78C02E0028985F /* AppDelegate.swift in Sources */,
130A80552B78C02E0028985F /* SceneDelegate.swift in Sources */,
13910ECB2B80D5C8009BF17E /* waifu-pics.swift in Sources */,
13910EC52B80D5A6009BF17E /* pic-re.swift in Sources */,
13910EC02B80D396009BF17E /* UIExtensions.swift in Sources */,
13910EC92B80D5C2009BF17E /* nekos-best.swift in Sources */,
13910EBE2B80D380009BF17E /* ImageExtensions.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -366,6 +414,25 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
13910ECD2B80D90D009BF17E /* XCRemoteSwiftPackageReference "SDWebImage" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/SDWebImage/SDWebImage";
requirement = {
branch = master;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
13910ECE2B80D90D009BF17E /* SDWebImage */ = {
isa = XCSwiftPackageProductDependency;
package = 13910ECD2B80D90D009BF17E /* XCRemoteSwiftPackageReference "SDWebImage" */;
productName = SDWebImage;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 130A80472B78C02E0028985F /* Project object */;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "SDWebImage",
"repositoryURL": "https://github.com/SDWebImage/SDWebImage",
"state": {
"branch": "master",
"revision": "80c8b2023a5efb4415a2c615acfec075e5c243d2",
"version": null
}
}
]
},
"version": 1
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "F794EE07-4BB3-4C6A-95AB-0C0581DD0FA9"
type = "1"
version = "2.0">
</Bucket>
82 changes: 82 additions & 0 deletions AnimeGen/APIs/nekos-best.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// nekos-best.swift
// AnimeGen
//
// Created by cranci on 17/02/24.
//

import UIKit

extension ViewController {

func loadImageAndTagsFromNekosBest() {
startLoadingIndicator()

let categories = ["neko", "waifu", "kitsune"]
let randomCategory = categories.randomElement() ?? "waifu"

let apiEndpoint = "https://nekos.best/api/v2/\(randomCategory)"

guard let url = URL(string: apiEndpoint) else {
print("Invalid URL")
stopLoadingIndicator()
return
}

var request = URLRequest(url: url)
request.httpMethod = "GET"

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
if let error = error {
print("Error: \(error)")
self.stopLoadingIndicator()
return
}

guard let httpResponse = response as? HTTPURLResponse else {
print("Invalid HTTP response")
self.stopLoadingIndicator()
return
}

guard httpResponse.statusCode == 200 else {
print("Invalid status code: \(httpResponse.statusCode)")
self.stopLoadingIndicator()
return
}

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

self.currentImageURL = imageUrlString

let author = result["artist_name"] as? String
let category = randomCategory

if let data = try? Data(contentsOf: imageUrl), let newImage = UIImage(data: data) {
self.imageView.image = newImage
self.animateImageChange(with: newImage)
self.updateUIWithTags([], author: author, category: category)
self.stopLoadingIndicator()
} else {
print("Failed to load image data.")
self.stopLoadingIndicator()
}
} else {
print("Failed to parse JSON response or missing necessary data.")
self.stopLoadingIndicator()
}
}
}
}

task.resume()
}

}
76 changes: 76 additions & 0 deletions AnimeGen/APIs/pic-re.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// pic-re.swift
// AnimeGen
//
// Created by cranci on 17/02/24.
//

import UIKit

extension ViewController {

func loadImageAndTagsFromPicRe() {
startLoadingIndicator()

let apiEndpoint = "https://pic.re/image"

guard let url = URL(string: apiEndpoint) else {
print("Invalid URL")
stopLoadingIndicator()
return
}

var request = URLRequest(url: url)
request.httpMethod = "GET"

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Error: \(error)")
self.stopLoadingIndicator()
return
}

guard let httpResponse = response as? HTTPURLResponse else {
print("Invalid HTTP response")
self.stopLoadingIndicator()
return
}

guard httpResponse.statusCode == 200 else {
print("Invalid status code: \(httpResponse.statusCode)")
self.stopLoadingIndicator()
return
}

if let imageTagsString = httpResponse.allHeaderFields["image_tags"] as? String,
let imageUrlString = httpResponse.allHeaderFields["image_source"] as? String {
let tags = imageTagsString.components(separatedBy: ",")

self.currentImageURL = imageUrlString

DispatchQueue.main.async {
self.updateUIWithTags(tags)
}
} else {
print("No image tags found in response headers.")
self.stopLoadingIndicator()
return
}

guard let data = data, let newImage = UIImage(data: data) else {
print("Invalid image data")
self.stopLoadingIndicator()
return
}

DispatchQueue.main.async {
self.imageView.image = newImage
self.animateImageChange(with: newImage)
self.stopLoadingIndicator()
}
}

task.resume()
}

}
Loading

0 comments on commit 5cec023

Please sign in to comment.