Skip to content

Commit

Permalink
add ns
Browse files Browse the repository at this point in the history
  • Loading branch information
YellowCat98 committed May 12, 2024
1 parent 9b7d95b commit c58bd33
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 94 deletions.
10 changes: 5 additions & 5 deletions src/iGeodeLib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#pragma once
#include <functional>
#import <UIKit/UIKit.h>

void showAlert(const char *title, const char *message, const char *Btn);
NSString* iOSVersion();
void CShowAlert(const char *title, const char *message, const char *Btn, std::function<void()> callback, const char *Btn2, std::function<void()> callback2);

namespace iGeodeLib {
void showAlert(const char *title, const char *message, const char *Btn);
std::string iOSVersion();
void CShowAlert(const char *title, const char *message, const char *Btn, std::function<void()> callback, const char *Btn2, std::function<void()> callback2);
}
#endif // IGEODELIB_HPP
170 changes: 82 additions & 88 deletions src/iGeodeLib.mm
Original file line number Diff line number Diff line change
@@ -1,105 +1,99 @@
#include <functional>
#include "iGeodeLib.hpp"
namespace iGeodeLib {

/*
very simple. converts NSString to std::string, although most functions here convert to char or std::string, this would probably come in handy in some situations.
usage:
auto str
std::string NS2S(NSString *str) {
return nsString ? [nsString UTF8String] : "";
}
*/
/*
shows a system level alert.
usage:
showAlert("Insert title here", "insert message here...", "insert button here.");
you cannot create a callback with this, use the showAlertWithCallback() function for callbacks.
*/
void showAlert(const char *title, const char *message, const char *Btn) {
NSString *titleString = [NSString stringWithUTF8String:title];
NSString *messageString = [NSString stringWithUTF8String:message];
NSString *btnString = [NSString stringWithUTF8String:Btn];

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:titleString
message:messageString
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:btnString
style:UIAlertActionStyleDefault
handler:nil];

[alertController addAction:okAction];

UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
/*
shows a system level alert.
usage:
showAlert("Insert title here", "insert message here...", "insert button here.");
you cannot create a callback with this, use the showAlertWithCallback() function for callbacks.
*/
void showAlert(const char *title, const char *message, const char *Btn) {
NSString *titleString = [NSString stringWithUTF8String:title];
NSString *messageString = [NSString stringWithUTF8String:message];
NSString *btnString = [NSString stringWithUTF8String:Btn];

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:titleString
message:messageString
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:btnString
style:UIAlertActionStyleDefault
handler:nil];

[alertController addAction:okAction];

[rootViewController presentViewController:alertController animated:YES completion:nil];
}
UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];

/*
ShowAlert but with a function callback.
usage:
CShowAlert("Title", "Message", "go away", MyCoolFunction());
- this function allows you to add 2 buttons!!!
usage: (again)
CShowAlert("Title", "Message", "go away", nullptr, "go away again", nullptr);
this should give you an alert with 2 buttons and no callback at al!!!!;
or alternatively
CShowAlert("Title", "Message", "go away", nullptr, nullptr, nullptr);
this should give you one button, with NO callback...
you should probably use showAlert() instead for that.
*/
[rootViewController presentViewController:alertController animated:YES completion:nil];
}

void CShowAlert(const char *title, const char *message, const char *Btn, std::function<void()> callback = nullptr, const char *Btn2 = nullptr, std::function<void()> callback2 = nullptr) {
NSString *titleString = [NSString stringWithUTF8String:title];
NSString *messageString = [NSString stringWithUTF8String:message];
NSString *btnString = [NSString stringWithUTF8String:Btn];

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:titleString
message:messageString
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *ButtonAction = [UIAlertAction actionWithTitle:btnString
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
if (callback != nullptr) {
callback();
}
}];
/*
ShowAlert but with a function callback.
usage:
CShowAlert("Title", "Message", "go away", MyCoolFunction());
- this function allows you to add 2 buttons!!!
usage: (again)
CShowAlert("Title", "Message", "go away", nullptr, "go away again", nullptr);
this should give you an alert with 2 buttons and no callback at al!!!!;
or alternatively
CShowAlert("Title", "Message", "go away", nullptr, nullptr, nullptr);
this should give you one button, with NO callback...
you should probably use showAlert() instead for that.
*/


[alertController addAction:ButtonAction];

if (Btn2 != nullptr) {
NSString *btnString2 = [NSString stringWithUTF8String:Btn2];
UIAlertAction *ButtonAction2 = [UIAlertAction ActionWithTitle:btnString2
style:UIAlertActionStyleDefault
void CShowAlert(const char *title, const char *message, const char *Btn, std::function<void()> callback = nullptr, const char *Btn2 = nullptr, std::function<void()> callback2 = nullptr) {
NSString *titleString = [NSString stringWithUTF8String:title];
NSString *messageString = [NSString stringWithUTF8String:message];
NSString *btnString = [NSString stringWithUTF8String:Btn];

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:titleString
message:messageString
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *ButtonAction = [UIAlertAction actionWithTitle:btnString
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
if (callback2 != nullptr) {
callback2();
if (callback != nullptr) {
callback();
}
}];
[alertController addAction:ButtonAction2];
}

UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];

[alertController addAction:ButtonAction];

if (Btn2 != nullptr) {
NSString *btnString2 = [NSString stringWithUTF8String:Btn2];
UIAlertAction *ButtonAction2 = [UIAlertAction ActionWithTitle:btnString2
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
if (callback2 != nullptr) {
callback2();
}
}];
[alertController addAction:ButtonAction2];
}

[rootViewController presentViewController:alertController animated:YES completion:nil];
}
UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];

/* self explanatory, get the ios version.
usage: iOSVersion();
you can also do something like this
if (iOSVersion == "17.4") {
showAlert("iOS 17.4", "You are on iOS 17.4", "go away");
} else {
showAlert("Update Required", "Update to iOS 17.4", "shut up");
[rootViewController presentViewController:alertController animated:YES completion:nil];
}

you might need to use the NS2S function on this to convert it to a std::string
/* self explanatory, get the ios version.
usage: iOSVersion();
you can also do something like this
if (iOSVersion == "17.4") {
showAlert("iOS 17.4", "You are on iOS 17.4", "go away");
} else {
showAlert("Update Required", "Update to iOS 17.4", "shut up");
}
*/
NSString* iOSVersion() {
UIDevice *device = [UIDevice currentDevice];
NSString *iOSVersion = [device systemVersion];
return iOSVersion;
*/
std::string iOSVersion() {
UIDevice *device = [UIDevice currentDevice];
NSString *iOSVersion = [device systemVersion];
return [iOSVersion UTF8String];
}
}
2 changes: 1 addition & 1 deletion src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ using namespace geode::prelude;

class $modify(MenuLayer) {
void onMoreGames(CCObject*) {
CShowAlert("Hello", "hi", "go away", nullptr, "hi", nullptr);
iGeodeLib::CShowAlert("Hello", "hi", "go away", nullptr, "hi", nullptr);
}
};

0 comments on commit c58bd33

Please sign in to comment.