Skip to content

Commit

Permalink
default api selector + image save fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cranci1 committed Apr 26, 2024
1 parent 0ba0f7b commit 0616472
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
6 changes: 2 additions & 4 deletions AnimeGen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,11 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = AnimeGen/AnimeGen.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 30;
CURRENT_PROJECT_VERSION = 31;
DEVELOPMENT_TEAM = 399LMK6Q2Y;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = AnimeGen/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.entertainment";
INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = "The gallery addition is needed to save images.";
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UIMainStoryboardFile = Main;
Expand Down Expand Up @@ -524,12 +523,11 @@
CODE_SIGN_ENTITLEMENTS = AnimeGen/AnimeGen.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 30;
CURRENT_PROJECT_VERSION = 31;
DEVELOPMENT_TEAM = 399LMK6Q2Y;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = AnimeGen/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.entertainment";
INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = "The gallery addition is needed to save images.";
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UIMainStoryboardFile = Main;
Expand Down
Binary file not shown.
5 changes: 4 additions & 1 deletion AnimeGen/Buttons/HearthButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extension ViewController {
self.animateFeedback()
} else {
print("Error saving GIF image: \(error?.localizedDescription ?? "")")
self.showAlert(withTitle: "Error Saving Image!", message: "You didn't allow AnimeGen to access the Photo Library.", viewController: self)
}
}
return
Expand All @@ -46,10 +47,12 @@ extension ViewController {
}
}

@objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
print("Error saving image: \(error.localizedDescription)")
self.showAlert(withTitle: "Error Saving Image!", message: "You didn't allow AnimeGen to access the Photo Library.", viewController: self)
} else {
self.animateFeedback()
print("Image saved successfully")
}
}
Expand Down
36 changes: 36 additions & 0 deletions AnimeGen/Settings/SettingsPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ struct SettingsPage: View {
// Tutorial
@State private var isShowingTutorial = false

// Default API
@State private var isPresentingActionSheet = false
@State private var selectedChoiceIndex = 0

let choices = ["waifu.im", "pic.re", "waifu.pics", "waifu.it", "nekos.best", "Nekos api", "nekos.moe", "NekoBot", "kyoko", "Purr"]

var body: some View {
NavigationView {
Form {
Expand Down Expand Up @@ -67,6 +73,36 @@ struct SettingsPage: View {
}

Section(header: Text("Api Preferences"), footer: Text("An app restart is necessary to enable or disable the changes.")) {
Button(action: {
isPresentingActionSheet = true
}) {
HStack {
Text("Default API")
.foregroundColor(.primary)
Spacer()
Text(choices[selectedChoiceIndex])
.foregroundColor(.accentColor)
}
}.actionSheet(isPresented: $isPresentingActionSheet) {
ActionSheet(title: Text("Choose Default API"), buttons: [
.default(Text("Purr")) { selectedChoiceIndex = 9 },
.default(Text("kyoko")) { selectedChoiceIndex = 8 },
.default(Text("NekoBot")) { selectedChoiceIndex = 7 },
.default(Text("nekos.moe")) { selectedChoiceIndex = 6 },
.default(Text("Nekos api")) { selectedChoiceIndex = 5 },
.default(Text("nekos.best")) { selectedChoiceIndex = 4 },
.default(Text("waifu.it")) { selectedChoiceIndex = 3 },
.default(Text("waifu.pics")) { selectedChoiceIndex = 2 },
.default(Text("waifu.im")) { selectedChoiceIndex = 0 },
.default(Text("pic.re")) { selectedChoiceIndex = 1 },
.cancel()
])
}
.onDisappear {
UserDefaults.standard.set(selectedChoiceIndex, forKey: "SelectedChoiceIndex")
NotificationCenter.default.post(name: Notification.Name("SelectedChoiceChanged"), object: selectedChoiceIndex)
}

Toggle("Display Tags", isOn: Binding(
get: { self.tags },
set: { newValue in
Expand Down
21 changes: 20 additions & 1 deletion AnimeGen/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class ViewController: UIViewController {

var alert = UserDefaults.standard.bool(forKey: "enableDeveloperAlert")

let choices = ["waifu.im", "pic.re", "waifu.pics", "waifu.it", "nekos.best", "Nekos api", "nekos.moe", "NekoBot", "kyoko", "Purr"]

var counter: Int = 0

override func viewDidLoad() {
Expand Down Expand Up @@ -88,7 +90,7 @@ class ViewController: UIViewController {

// Api Button
apiButton = UIButton(type: .system)
apiButton.setTitle("pic.re", for: .normal)
apiButton.setTitle("", for: .normal)
apiButton.addTarget(self, action: #selector(apiButtonTapped), for: .touchUpInside)
apiButton.translatesAutoresizingMaskIntoConstraints = false
apiButton.backgroundColor = gradient ? UIColor(red: 0.4, green: 0.3, blue: 0.6, alpha: 1.0) : UIColor.darkGray
Expand Down Expand Up @@ -288,9 +290,26 @@ class ViewController: UIViewController {
animateGradient()
}

let selectedChoiceIndex = UserDefaults.standard.integer(forKey: "SelectedChoiceIndex")
apiButton.setTitle(choices[selectedChoiceIndex], for: .normal)

NotificationCenter.default.addObserver(self, selector: #selector(selectedChoiceChanged(_:)), name: Notification.Name("SelectedChoiceChanged"), object: nil)

loadImageAndTagsFromSelectedAPI()
}


deinit {
NotificationCenter.default.removeObserver(self)
}

@objc func selectedChoiceChanged(_ notification: Notification) {
guard let selectedIndex = notification.object as? Int else { return }
guard selectedIndex >= 0 && selectedIndex < choices.count else { return }
apiButton.setTitle(choices[selectedIndex], for: .normal)
}


func loadImageAndTagsFromSelectedAPI() {
guard let title = apiButton.title(for: .normal) else {
return
Expand Down

0 comments on commit 0616472

Please sign in to comment.