RxSwift trigger observable on button tap - rx-swift

I am creating a custom observable that will present a UIAlertController which would like to trigger an API Call when pressed. However, I can't seem to get the Alert to pop unless I manually subscribe to it. Is there a way to get this triggered from the viewModel?
MyController.swift
class MyController: UIViewController {
#IBOutlet weak var nextBarButton: UIBarButtonItem!
var viewModel: ViewModel!
override func viewDidLoad() {
super.viewDidLoad()
viewModel = ViewModel( nextBarButton.rx.tap.asDriver(), alertController()asDriver(onErrorJustReturn: ""))
}
func alertController() -> Observable<String> {
return Observable.create { [weak alert = self] observer in
guard let alert = alert else {
observer.on(.completed)
return Disposables.create()
}
let alertVc = UIAlertController(title: "My Title", message: "My Message", preferredStyle: .alert)
let submit = UIAlertAction(title: "Continue", style: .default) { _ in
observer.onNext("Test")
observer.on(.completed)
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ in
observer.on(.completed)
}
alertVc.addAction(cancel)
alertVc.addAction(submit)
alert.present(alertVc, animated: true, completion: nil)
return Disposables.create {
alertVc.dismiss(animated: true, completion: nil)
}
}
}
}
ViewModel.swift
public final class ViewModel {
init(_ manager: SessionManager, _ trigger: Driver<Void>, _ alert: Driver<String>) {
let _ = trigger.withLatestFrom(alert)
.flatMap { text in
return manager.rx
.request(urlRequest: Api.test)
.retry(3)
.asDriverOnErrorJustComplete()
}
}
}

You forgot to subscribe() in your ViewModel and you should flatMapLatest when chaining events from UIButton. It should look like:
public final class ViewModel {
init(_ manager: SessionManager, _ trigger: Driver<Void>, _ alert: Driver<String>) {
let _ = trigger.flatMapLatest{ alert }
.flatMap { text in
return manager.rx
.request(urlRequest: Api.test)
.retry(3)
.asDriverOnErrorJustComplete()
}
.subscribe()
}
}

Related

How to remove Top Safe Area in Zstack With ScrollView Image SwiftUI

I'm trying to remove top safe area. Is there any way to remove top safe area from top and image?
Code:-
struct ContentView22: View {
#State private var showDialog = false
var body: some View {
ZStack {
ScrollView {
VStack {
Image("CentrImg.jpeg")
.resizable()
.scaledToFill()
.frame(width:UIScreen.screenWidth,height: 180, alignment: .center)
.clipped()
.ignoresSafeArea()
.edgesIgnoringSafeArea(.top)
VStack(alignment:.leading,spacing:25) {
Text("Some text")
.onTapGesture {
showDialog = true
}
}
}
}
.alert(isPresented: $showDialog,TextAlert(title: "Title",message: "Message") { result in
print(result as Any)
if let _ = result {
} else {
}
})
}.edgesIgnoringSafeArea(.top)
.background(Color.red)
.foregroundColor(.white)
.navigationBarHidden(true)
.edgesIgnoringSafeArea(/*#START_MENU_TOKEN#*/.all/*#END_MENU_TOKEN#*/)
.navigationBarBackButtonHidden(true)
.navigationBarTitle("", displayMode: .inline)
}
}
Alert Control Class:-
import SwiftUI
import Combine
public struct TextAlert {
public var title: String // Title of the dialog
public var message: String // Dialog message
public var placeholder: String = "" // Placeholder text for the TextField
public var accept: String = "OK" // The left-most button label
public var cancel: String? = "Cancel" // The optional cancel (right-most) button label
public var secondaryActionTitle: String? = nil // The optional center button label
public var action: (String?) -> Void // Triggers when either of the two buttons closes the dialog
public var secondaryAction: (() -> Void)? = nil // Triggers when the optional center button is tapped
}
extension UIAlertController {
convenience init(alert: TextAlert) {
self.init(title: alert.title, message: alert.message, preferredStyle: .alert)
addTextField {
$0.placeholder = alert.placeholder
$0.returnKeyType = .done
}
if let cancel = alert.cancel {
addAction(UIAlertAction(title: cancel, style: .cancel) { _ in
alert.action(nil)
})
}
if let secondaryActionTitle = alert.secondaryActionTitle {
addAction(UIAlertAction(title: secondaryActionTitle, style: .default, handler: { _ in
alert.secondaryAction?()
}))
}
let textField = self.textFields?.first
addAction(UIAlertAction(title: alert.accept, style: .default) { _ in
alert.action(textField?.text)
})
}
}
struct AlertWrapper<Content: View>: UIViewControllerRepresentable {
#Binding var isPresented: Bool
let alert: TextAlert
let content: Content
func makeUIViewController(context: UIViewControllerRepresentableContext<AlertWrapper>) -> UIHostingController<Content> {
UIHostingController(rootView: content)
}
final class Coordinator {
var alertController: UIAlertController?
init(_ controller: UIAlertController? = nil) {
self.alertController = controller
}
}
func makeCoordinator() -> Coordinator {
return Coordinator()
}
func updateUIViewController(_ uiViewController: UIHostingController<Content>, context: UIViewControllerRepresentableContext<AlertWrapper>) {
uiViewController.rootView = content
if isPresented && uiViewController.presentedViewController == nil {
var alert = self.alert
alert.action = {
self.isPresented = false
self.alert.action($0)
}
context.coordinator.alertController = UIAlertController(alert: alert)
uiViewController.present(context.coordinator.alertController!, animated: true)
}
if !isPresented && uiViewController.presentedViewController == context.coordinator.alertController {
uiViewController.dismiss(animated: true)
}
}
}
extension View {
public func alert(isPresented: Binding<Bool>, _ alert: TextAlert) -> some View {
AlertWrapper(isPresented: isPresented, alert: alert, content: self)
}
}
Output with alert code
Output without alert code:-
Can someone please explain to me how to remove top safe area from image with alert code, I've tried to implement by above but no results yet.
Any help would be greatly appreciated.
Thanks in advance.
I removed your Alert code. You can do the same with a much simpler function.
Value
#State var testText: String = ""
Alert Func
func alertView() {
let alert = UIAlertController(title: "Test", message: "Test Message", preferredStyle: .alert)
alert.addTextField { (testTextField) in
testTextField.placeholder = "Test TextField"
}
let okButton = UIAlertAction(title: "OK", style: .default) { (_) in
self.testText = alert.textFields?[0].text ?? ""
}
let cancellButton = UIAlertAction(title: "Cancel", style: .destructive) { (_) in
}
alert.addAction(okButton)
alert.addAction(cancellButton)
UIApplication.shared.windows.first?.rootViewController?.present(alert, animated: true, completion: {
})
}
Using:
Text("Some text")
.onTapGesture {
alertView()
}

How to implement UIAlertController in tvOS

I am implementing the UIAlertController in tvOS platform. It's working fine but when i am tap on the button action then the action is not call. But when move up and down with two button and tap any one the action calls.
Here is my code.
func showAlertMessage() {
let alertController = UIAlertController(title: kAlertMessage, message: "", preferredStyle: UIAlertControllerStyle.Alert)
let cancelButton = UIAlertAction(title: kCancelButton, style: UIAlertActionStyle.Default) { (action) -> Void in
self.dismissView()
}
let retryButton = UIAlertAction(title: kRetryButton, style: UIAlertActionStyle.Default) { (action) -> Void in
if self.playerObj != nil{
if self.controlFlag == false {
self.playerItem?.removeObserver(self, forKeyPath: "status")
}else{
self.playerItem?.removeObserver(self, forKeyPath: "status")
}
self.playerObj = nil
self.playerItem = nil
self.asset = nil
}
self.parseJSON()
}
alertController.addAction(retryButton)
alertController.addAction(cancelButton)
if self.alertMessageFlag != true {
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.presentViewController(alertController, animated: true, completion: nil)
}
}
}
please find out the issue and suggest me.
thank you.

Error when loading UIAlertController

I want to load an alert when internet connection is not available. The function for checking internet connection is ready but I cannot load the alert. So I just put the alert code in viewDidLoad without any conditions etc. and got this error:
Warning: Attempt to present UIAlertController: 0x12752d400 on x.ViewController: 0x127646f00 whose view is not in the window hierarchy!
Code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Delegates
verificationCode.delegate = self
let alert = UIAlertController(title: "Oops!", message:"This feature isn't available right now", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { _ in }
alert.addAction(action)
self.presentViewController(alert, animated: true) {}
if (!Util.isConnectedToNetwork()) {
self.isConnected = false
}
}
Could you tell me how to fix it?
The error tells you what has gone wrong.
You are trying to present a view controller in viewDidLoad except the view, although loaded, is not in any hierarchy. Try putting the code in the viewDidAppear method, which is called after the view appears on screen, and is in a view hierarchy.
Swift 4 Update
I think this could help you for your problem :
override func viewDidLoad() {
super.viewDidLoad()
verificationCode.delegate = self
let alert = UIAlertController(title: "Oops!", message:"This feature isn't available right now", preferredStyle: .alert)
let delete = UIAlertAction(title: "OK", style: .default) { (_) in }
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alert.addAction(cancelAction)
alert.addAction(delete)
alert.popoverPresentationController?.sourceView = sender as UIView
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
if (!Util.isConnectedToNetwork()) {
self.isConnected = false
}
}
Move the code to viewDidAppear from viewDidLoad
override func viewDidAppear(animated: Bool) {
// Delegates
verificationCode.delegate = self
let alert = UIAlertController(title: "Oops!", message:"This feature isn't available right now", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { _ in }
alert.addAction(action)
self.presentViewController(alert, animated: true) {}
if (!Util.isConnectedToNetwork()) {
self.isConnected = false
}
}

EXC_BAD_ACCESS crash on didSelectRowAtIndexPath

My application suddenly started crashing and after four hours I still can't figure out why. It worked perfectly last Friday and I haven't changed anything to the code since.
I have tried:
Stepping through code with breakpoints.
Adding an Exception Breakpoint.
Enabling Zombie objects.
Turning static analyser on.
None of these made me any wiser or pointed me to a more exact error message.
The crash happens when I select a cell. It then points to the AppDelegate with the message "Thread 1: EXC_BAD_ACCESS". Continueing the program execution does not give a more clear error message.
Some images of the error and my code can be seen below. I am really at a loss here so any help will be much appreciated.
Code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
var dealer : DealerList
var alldealer : TotalDealerList
var id : String
if scAll.selectedSegmentIndex == 0
{
if scMatur.selectedSegmentIndex == 0
{
if (self.resultSearchController.active)
{
dealer = filtereddealers[indexPath.row]
}
else
{
dealer = dealers[indexPath.row]
}
}
else
{
if (self.resultSearchController.active)
{
dealer = filtereddealers[indexPath.row]
}
else
{
dealer = sorteddealers[indexPath.row]
}
}
//Create a message box with actions for the user.
let actionSheetController: UIAlertController = UIAlertController(title: "What to do with:", message: "'\(dealer.dlName)'?", preferredStyle: .ActionSheet)
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
//starts a phonecall to the selected dealer
let callAction: UIAlertAction = UIAlertAction(title: "Call", style: .Default) { action -> Void in
var phonenr = "0634563859"
let alert2 = UIAlertController(title: "Please confirm", message: "Call '\(dealer.dlName)' at \(phonenr)?", preferredStyle: UIAlertControllerStyle.Alert)
alert2.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Default, handler: nil))
alert2.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) { UIAlertAction in
self.callNumber(phonenr)
})
self.presentViewController(alert2, animated: true, completion: nil)
}
//action that opens the DealCustView. Pass data from this view to the next. ID is used in the next view.
let showcustomersAction: UIAlertAction = UIAlertAction(title: "Customers", style: .Default) { action -> Void in
let reportview = self.storyboard?.instantiateViewControllerWithIdentifier("dealcustList") as! DealCustView
reportview.id2 = dealer.dlSCode
reportview.sourceid = self.id
self.navigationController?.pushViewController(reportview, animated: true)
}
//add the actions to the messagebox
actionSheetController.addAction(cancelAction)
actionSheetController.addAction(showcustomersAction)
actionSheetController.addAction(callAction)
//present the messagebox
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(actionSheetController, animated: true, completion: nil)
})
}
else
{
if scMatur.selectedSegmentIndex == 0
{
if (self.resultSearchController.active)
{
alldealer = filteredalldealers[indexPath.row]
}
else
{
alldealer = alldealers[indexPath.row]
}
}
else
{
if (self.resultSearchController.active)
{
alldealer = filteredalldealers[indexPath.row]
}
else
{
alldealer = sortedalldealers[indexPath.row]
}
}
///
//Create a message box with actions for the user.
let actionSheetController: UIAlertController = UIAlertController(title: "What to do with:", message: "'\(alldealer.tdlName)'?", preferredStyle: .ActionSheet)
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
//starts a phonecall to the selected dealer
let callAction: UIAlertAction = UIAlertAction(title: "Call", style: .Default) { action -> Void in
var phonenr = "0634563859"
let alert2 = UIAlertController(title: "Please confirm", message: "Call '\(alldealer.tdlName)' at \(phonenr)?", preferredStyle: UIAlertControllerStyle.Alert)
alert2.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Default, handler: nil))
alert2.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) { UIAlertAction in
self.callNumber(phonenr)
})
self.presentViewController(alert2, animated: true, completion: nil)
}
//action that opens the DealCustView. Pass data from this view to the next. ID is used in the next view.
let showcustomersAction: UIAlertAction = UIAlertAction(title: "Customers", style: .Default) { action -> Void in
let reportview = self.storyboard?.instantiateViewControllerWithIdentifier("dealcustList") as! DealCustView
reportview.id2 = alldealer.tdlSCode
reportview.sourceid = self.id
self.navigationController?.pushViewController(reportview, animated: true)
}
//add the actions to the messagebox
actionSheetController.addAction(cancelAction)
actionSheetController.addAction(showcustomersAction)
actionSheetController.addAction(callAction)
//present the messagebox
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(actionSheetController, animated: true, completion: nil)
})
}
}
}

Display alert keeps logging me out after error message on xcode using swift

I'm creating an Instagram like app, and whenever I dismiss the display alert that is displayed when an image or text is missing, it logs me out and logs me back in. How can I make it so that when I click "ok", only the display alert goes away and doesn't log me out?
Here is my image posting view control code:
import UIKit
import Parse
import Foundation
class PostViewControllerPage1: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func displayAlert(title:String, error:String) {
var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
var photoSelected:Bool = false
#IBOutlet weak var imageToPost: UIImageView!
#IBAction func chooseImage(sender: AnyObject) {
var image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
println("Image selected")
self.dismissViewControllerAnimated(true, completion: nil)
imageToPost.image = image
photoSelected = true
}
#IBOutlet weak var imageDescription: UITextField!
#IBAction func postImage(sender: AnyObject) {
var error = ""
if (photoSelected == false) {
error = "Please select an image to post"
} else if (imageDescription.text == "") {
error = "Please enter a message"
}
if (error != "") {
displayAlert("Cannot Post Image", error: error)
}
else {
var post = PFObject(className: "Post")
post["Title"] = imageDescription.text
post.saveInBackgroundWithBlock{(success: Bool, error: NSError?) -> Void in
if success == false {
self.displayAlert("Could Not Post Image", error: "Please try again later")
} else {
let imageData = UIImagePNGRepresentation(self.imageToPost.image)
let imageFile = PFFile(name: "image.png", data: imageData)
post["imageFile"] = imageFile
post.saveInBackgroundWithBlock{(success: Bool, error: NSError?) -> Void in
if success == false {
self.displayAlert("Could Not Post Image", error: "Please try again later")
} else {
println("posted successfully")
}
}
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
What am I doing wrong?
Here is my login code, because I feel like that might be a part of the issue:
import Foundation
import Parse
import UIKit
import Bolts
class LoginViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var loginStatusLabel: UILabel!
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var loginButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
emailTextField.delegate = self
passwordTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func loginButtonPress(sender: AnyObject) {
login(emailTextField.text, password: passwordTextField.text)
}
func login(email: String, password: String)
{
PFUser.logInWithUsernameInBackground(email, password: password)
{
(user: PFUser?, error: NSError?) -> Void in
if user != nil
{
user!.fetchInBackground()
if user!.objectForKey("emailVerified") as! Bool
{
self.loginStatusLabel.text = "Success!"
println("successfulLogin")
self.performSegueWithIdentifier("login", sender: self)
}
else if !(user!.objectForKey("emailVerified") as! Bool)
{
self.loginStatusLabel.text = "Verify your email address!"
}
else // status is "missing"
{
//TODO: Handle this error better
self.loginStatusLabel.text = "Verification status: Missing"
}
}
else
{
if let errorField = error!.userInfo
{
self.loginStatusLabel.text = (errorField["error"] as! NSString) as String
}
else
{
// No userInfo dictionary present
// Help from http://stackoverflow.com/questions/25381338/nsobject-anyobject-does-not-have-a-member-named-subscript-error-in-xcode
}
}
}
}
override func viewDidAppear(animated: Bool) {
if PFUser.currentUser() != nil {
self.performSegueWithIdentifier("login", sender: self)
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
}
override func viewWillDisappear(animated: Bool) {
self.navigationController?.navigationBarHidden = false
}
}
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.dismissViewControllerAnimated in this line will dismiss view controller that show alert and not an alert itself. So all you have to do is comment or delete this line of code

Resources