(Windows Forms) Button only available if bool is true - forms

Is it possible to set a button so that it is only clickable if a bool is true?
This isn't my actual code but:
bool details_accepted = false;
public bool check_bank_details()
{
//this is simplified it's actually using an SQL query
if (bank_details = "123456789")
{
return true;
}
else
{
return false;
}
}
if (check_bank_details == true)
{
//CODE FOR SETTING BUTTON TO CLICKABLE
}
else if (check_bank_details == false)
{
//Code making Button not clickable
}

use the Enabled property
Button1.Enabled = check_bank_details();

Related

What is the best way to wrap `purchaserInfo` for easy comparisons throughout your code using RevenueCat

What would be the best way to wrap Purchases.shared.purchaserInfo to just return true or false when doing multiple comparisons in an If statement.
What I want to be able to do is to quickly check if the user has access to PRO features by having a method or a property that return true or false since I need to make multiple checks in an if statement, something like... if userHasAccess && someOtherCheck { // do something}
Is this a valid solution?
Singleton Class:
class IAPHelper {
static let shared = IAPHelper()
func hasAccessToProFeatures()->Bool{
var hasAccess:Bool?
Purchases.shared.purchaserInfo { (purchaserInfo, error) in
if let purchaserInfo = purchaserInfo {
if purchaserInfo.activeEntitlements.contains("myEntitlement") {
hasAccess = true
}else{
hasAccess = false
}
}
}
return hasAccess!
}
}
Usage:
if IAPHelper.shared.hasAccessToProFeatures() && someOtherCheck{
// unlock PRO features
}
Shared instances are not bad, however for such a small class you could declare hasAccessToProFeatures as a static variable instead.
class IAPHelper {
static var hasAccessToProFeatures: Bool {
var hasAccess:Bool?
Purchases.shared.purchaserInfo { (purchaserInfo, error) in
if let purchaserInfo = purchaserInfo {
if purchaserInfo.activeEntitlements.contains("myEntitlement") {
hasAccess = true
}else{
hasAccess = false
}
}
}
guard let isProMember = hasAccess else {
return false
}
return isProMember
}
}
Then you could call it like:
if IAPHelper.hasAccessToProFeatures && someOtherCheck {
// unlock PRO features
}

Xcode UI Test How to handle notification permissions generated by UNUserNotificationCenter

In my app, I ask for notification permissions like this:
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
Now while testing, I need to handle this permission popup but it is not working, I tried these code:
XCUIApplication().alerts["“AppName” Would Like to Send You Notifications"].buttons["Allow"].tap() //didn't work.
addUIInterruptionMonitorWithDescription("Notification Dialog") { (alert) -> Bool in
alert.buttons["Allow"].tap()
return true
}
addUIInterruptionMonitorWithDescription("“AppName” Would Like to Send You Notifications") { (alert) -> Bool in
alert.buttons["Allow"].tap()
return true
}
addUIInterruptionMonitorWithDescription("Notifications may include alerts, sounds, and icon badges. These can be configured in Settings.") { (alert) -> Bool in
alert.buttons["Allow"].tap()
return true
}
But nothing works.
Try this
let app2 = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let button = app2.alerts.firstMatch.buttons["Allow"]
button.waitForExistence(timeout: 10)
button.tap()
1/ Try recording the path with XCode test recorder to alert button and make sure, you have the right one.
XCUIApplication().alerts["“AppName” Would Like to Send You Notifications"].buttons["Allow"].tap() //didn't work.
might not be correct.
(note: Using test recorder for things, like finding the path to element is ok and very newbie-friendly.)
2/ You don't have to use addUIInterruptionMonitorWithDescription in case, you have a system alert that will happen every time or in case, you know that it will happen.
Just use waitForExistance and tap it if it exists.
(note: I've found that waiting & tapping on system notification is better than addUIInterruptionMonitorWithDescription, which is unstable/complicated sometimes)
3/ alert.buttons["Allow"].tap() doesn't seem to be correct to me, shouldn't it be XCUIApplication().alerts.buttons["Allow"]? Alternatively, you can use XCUIApplication().alerts.buttons.element(boundBy: 0 to tap on first button.
import XCTest
// For multiple permission requests
var isPhotoPermissionAvailable: Bool?
var isLocationPermissionAvailable: Bool?
var isNotificationsPermissionAvailable: Bool?
extension XCTestCase {
func allowPhotos(_ enabled: Bool = true) {
addUIInterruptionMonitor(withDescription: "Photo Permissions") { (alert) -> Bool in
isPhotoPermissionAvailable = enabled
var alertButton = alert.buttons["Don't Allow"]
if enabled == true {
alertButton = alert.buttons["OK"]
}
if alertButton.exists {
alertButton.tap()
return true
}
XCUIApplication().tap()
return false
}
if isPhotoPermissionAvailable == nil {
XCUIApplication().swipeUp() //with this code, alert.buttons["OK"] exsists
}
}
func allowLocation(_ enabled: Bool = true) {
addUIInterruptionMonitor(withDescription: "Location Dialog") { (alert) -> Bool in
isLocationPermissionAvailable = enabled
var alertButton = alert.buttons["Don't Allow"]
if enabled == true {
alertButton = alert.buttons["Always Allow"]
}
if alertButton.exists {
alertButton.tap()
return true
}
XCUIApplication().tap()
return false
}
if isLocationPermissionAvailable == nil {
XCUIApplication().swipeUp()
}
}
func allowNotifications(_ enabled: Bool = true) {
addUIInterruptionMonitor(withDescription: "Notification Dialog") { (alert) -> Bool in
isNotificationsPermissionAvailable = enabled
var alertButton = alert.buttons["Don't Allow"]
if enabled == true {
alertButton = alert.buttons["Allow"]
}
if alertButton.exists {
alertButton.tap()
return true
}
XCUIApplication().tap()
return false
}
if isNotificationsPermissionAvailable == nil {
XCUIApplication().swipeUp()
}
}
}

Login Item - cocoa

Is there a way to check if the login item already exists (with bundleIdentifier of the app?) I want to be able to see if there is a login item and if it is enable.
I was trying to check my checkbox in applicationDidFinishLuanching when the login item is enabled using this:
if (SMLoginItemSetEnabled(("bundleIDOfMyApp" as CFStringRef), true)) {
self.startAtLoginButton.state = 1
} else {
self.startAtLoginButton.state = 0
}
It does its thing, but it also launches my helper application.
Another thing is this:
#IBAction func startAtLoginButtonChecked(sender: NSButton) {
var enabled = false
if sender.state == 0 { enabled = false }
if sender.state == 1 { enabled = true }
if !SMLoginItemSetEnabled(("bundleIDOfMyApp" as CFStringRef), enabled) {
print("Login was not successful")
}
}
As far as I am concerned this is the way you implement checkbox to enable/disable login item.
What it does in my app is every time I check the box it launches the helper app (which launches my app again).
Although the method SMCopyAllJobDictionaries() is deprecated this is the usual way to check if the job is enabled, SMLoginItemSetEnabled is only be used to set the value
import ServiceManagement
let jobDicts = SMCopyAllJobDictionaries( kSMDomainUserLaunchd ).takeRetainedValue() as NSArray as! [[String:AnyObject]]
let label = "bundleIDOfMyApp"
let jobEnabled = jobDicts.filter { $0["Label"] as! String == label }.isEmpty == false
The double casting is needed to cast CFArray to NSArray and then to Array<String,AnyObject>
Also usually the checkbox is bound to a property via KVC. The lines above are the getter and SMLoginItemSetEnabled is the setter for example
let helperBundleIdentifier = "bundleIDOfMyApp"
#available(OSX, deprecated=10.10) // this line suppresses the 'deprecated' warning
dynamic var startAtLogin : Bool {
get {
guard let jobDicts = SMCopyAllJobDictionaries( kSMDomainUserLaunchd ).takeRetainedValue() as NSArray as? [[String:AnyObject]] else { return false }
return jobDicts.filter { $0["Label"] as! String == helperBundleIdentifier }.isEmpty == false
} set {
if !SMLoginItemSetEnabled(helperBundleIdentifier, newValue) {
print("SMLoginItemSetEnabled failed.")
}
}
}
Swift 3:
#available(OSX, deprecated: 10.10)
dynamic var startAtLogin : Bool {
get {
guard let jobDicts = SMCopyAllJobDictionaries( kSMDomainUserLaunchd ).takeRetainedValue() as? [[String:Any]] else { return false }
return jobDicts.first(where: { $0["Label"] as! String == helperBundleIdentifier }) != nil
} set {
if !SMLoginItemSetEnabled(helperBundleIdentifier as CFString, newValue) {
print("SMLoginItemSetEnabled failed.")
}
}
}
Side note: A launchd job requires the key Label so it's 100% safe to unwrap the optional in the filter function.

How to choose the background keyboard?

I work on a custom keyboard (My Keyboard is no xib or StoryBoard),
I want to know if I can choose the background to remain constant
according to the choice The custom keyboard on
For example: When I choose the keyboard to keyboard Dark then stay in the selected mode "Dark".
And when I want to change the background again to be in the Light then again to stay in the selected mode "Light".
This would look like:
KeyboardInputTraits.swift
func pollTraits() {
if let proxy = (self.textDocumentProxy as? UITextInputTraits) {
if let layout = self.layout {
let appearanceIsDark = (proxy.keyboardAppearance == UIKeyboardAppearance.Dark)
if appearanceIsDark != layout.darkMode {
self.updateAppearances(appearanceIsDark)
}
}
}
}
KeyboardViewController.swift
func darkMode() -> Bool {
var darkMode = { () -> Bool in
if let proxy = self.textDocumentProxy as? UITextDocumentProxy {
return proxy.keyboardAppearance == UIKeyboardAppearance.Dark
}
else {
return false
}
}()
return darkMode
}

Unexpected char error

Can someone tell me what is wrong with this script?
#pragma strict
private var dead = false;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "Respawn")
{
dead = true;
}
}
function Update ()
{
if(dead)
{
transform.position = Vector3(0,0,0);
dead = false;
}
}
}
Remove the last } from the last line or either copy and paste this one:
#pragma strict
private var dead = false;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "Respawn")
{
dead = true;
}
}
function Update ()
{
if(dead)
{
transform.position = Vector3(0,0,0);
dead = false;
}
}