|
| 1 | +// |
| 2 | +// ReferralSettingsController.swift |
| 3 | +// Jotify |
| 4 | +// |
| 5 | +// Created by Harrison Leath on 5/22/22. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | +import MessageUI |
| 10 | + |
| 11 | +class ReferralSettingsController: UIViewController, MFMessageComposeViewControllerDelegate { |
| 12 | + |
| 13 | + let detailText: UITextView = { |
| 14 | + let tv = UITextView() |
| 15 | + tv.font = UIFont.systemFont(ofSize: 18, weight: .regular) |
| 16 | + tv.textAlignment = .center |
| 17 | + tv.backgroundColor = .clear |
| 18 | + tv.isUserInteractionEnabled = false |
| 19 | + tv.translatesAutoresizingMaskIntoConstraints = false |
| 20 | + return tv |
| 21 | + }() |
| 22 | + |
| 23 | + lazy var nextButton: UIButton = { |
| 24 | + let button = UIButton() |
| 25 | + button.setTitleColor(.white, for: .normal) |
| 26 | + button.backgroundColor = UIColor.jotifyBlue |
| 27 | + button.layer.cornerRadius = 10 |
| 28 | + button.translatesAutoresizingMaskIntoConstraints = false |
| 29 | + button.addTarget(self, action: #selector(shareReferral), for: .touchUpInside) |
| 30 | + button.setTitle("Invite Someone", for: .normal) |
| 31 | + return button |
| 32 | + }() |
| 33 | + |
| 34 | + let wrapper: UIView = { |
| 35 | + let view = UIView() |
| 36 | + view.backgroundColor = .clear |
| 37 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 38 | + return view |
| 39 | + }() |
| 40 | + |
| 41 | + //press a button to send referral link |
| 42 | + |
| 43 | + override func viewDidLoad() { |
| 44 | + super.viewDidLoad() |
| 45 | + |
| 46 | + view.backgroundColor = ColorManager.bgColor |
| 47 | + |
| 48 | + let referrals = User.settings?.referrals ?? 0 |
| 49 | + |
| 50 | + detailText.text = "You have \(referrals) referrals. Once you reach 3 referrals, Jotify premium is given automatically!" |
| 51 | + |
| 52 | + view.addSubview(wrapper) |
| 53 | + view.addSubview(nextButton) |
| 54 | + |
| 55 | + wrapper.addSubview(detailText) |
| 56 | + |
| 57 | + wrapper.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true |
| 58 | + wrapper.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true |
| 59 | + wrapper.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.85).isActive = true |
| 60 | + wrapper.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.80).isActive = true |
| 61 | + |
| 62 | + |
| 63 | + detailText.centerXAnchor.constraint(equalTo: wrapper.centerXAnchor).isActive = true |
| 64 | + detailText.topAnchor.constraint(equalTo: wrapper.centerYAnchor).isActive = true |
| 65 | + detailText.heightAnchor.constraint(equalToConstant: 400).isActive = true |
| 66 | + |
| 67 | + //to make the text fit relatively ok on iPad |
| 68 | + if view.bounds.width * 0.85 > 500 { |
| 69 | + detailText.widthAnchor.constraint(equalToConstant: 500).isActive = true |
| 70 | + } else { |
| 71 | + detailText.widthAnchor.constraint(equalTo: wrapper.widthAnchor).isActive = true |
| 72 | + } |
| 73 | + |
| 74 | + nextButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true |
| 75 | + nextButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -75).isActive = true |
| 76 | + nextButton.widthAnchor.constraint(equalToConstant: 250).isActive = true |
| 77 | + nextButton.heightAnchor.constraint(equalToConstant: 60).isActive = true |
| 78 | + } |
| 79 | + |
| 80 | + @objc func shareReferral() { |
| 81 | + let invitationLink = User.settings?.referralLink |
| 82 | + let subject = "You should start using Jotify! Use my referreral link: \(invitationLink ?? "nil")" |
| 83 | + |
| 84 | + let messageComposer = MFMessageComposeViewController() |
| 85 | + messageComposer.messageComposeDelegate = self |
| 86 | + messageComposer.body = subject |
| 87 | + |
| 88 | + if MFMessageComposeViewController.canSendText() { |
| 89 | + present(messageComposer, animated: true) |
| 90 | + } else { |
| 91 | + //TODO: Copy the link to the clipboard and present alert to user telling them this. |
| 92 | + UIPasteboard.general.string = subject |
| 93 | + let alertController = UIAlertController(title: "Unable to Send", message: "You cannot message from this device, so Jotify copied the referral link to your clipboard.", preferredStyle: .alert) |
| 94 | + alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {(action) in })) |
| 95 | + self.present(alertController, animated: true, completion: nil) |
| 96 | + } |
| 97 | + |
| 98 | + self.playHapticFeedback() |
| 99 | + } |
| 100 | + |
| 101 | + func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { |
| 102 | + if result == .sent { |
| 103 | + dismiss(animated: true) |
| 104 | + } else if result == .cancelled { |
| 105 | + dismiss(animated: true) |
| 106 | + } else { |
| 107 | + dismiss(animated: true) |
| 108 | + //copy link to clipboard and alert user |
| 109 | + let invitationLink = User.settings?.referralLink |
| 110 | + UIPasteboard.general.string = "You should start using Jotify! Use my referreral link: \(invitationLink ?? "nil")" |
| 111 | + let alertController = UIAlertController(title: "Unable to Send", message: "The message failed to send, so Jotify copied the link to your clipboard.", preferredStyle: .alert) |
| 112 | + alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: {(action) in })) |
| 113 | + self.present(alertController, animated: true, completion: nil) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments