Skip to content

Commit

Permalink
imUrlDataApp - Fix app sender to send last-tapped emoji (was always "…
Browse files Browse the repository at this point in the history
…happy")

MessagesViewController.swift
- Mood enum moved out to its own file
- local let moodKey moved to Mood enum
- subscript added

MessageComposingHelper
- displayMessageInterface take a mood param instead of using hardcoded strings

ViewController
- onAppSendButton pass mood to displayMessageInterface
- buttonFor(mood) helper added
- tap(mood) added
- viewDidLoad
  - call tap(mood) to get highlighted
  - set button isEnabled state (now we're not using them to toggle back on, should only be tappable when enabled)
- onHappy…Angry - call tap(mood:) instead of toggle, so tapping emoji sets the one to send
- matchButtonsToToggles - set isEnabled state
  • Loading branch information
AndyDentFree committed Aug 6, 2022
1 parent 7db1b77 commit 9e30a15
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 20 deletions.
24 changes: 24 additions & 0 deletions imUrlDataApp/Common/Mood.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Mood.swift
// imUrlDataApp
//
// Created by AndyDent on 6/8/2022.
// Copyright © 2022 Touchgram Pty Ltd. All rights reserved.
//

import Foundation

enum Mood : String, CaseIterable {
case happy
case quizzical
case distraught
case angry

static let moodKey = "mood"

static subscript(i: Int) -> Mood? {
guard i >= 0 && i < 4 else {return nil}
return Mood.allCases[i]
}

}
25 changes: 24 additions & 1 deletion imUrlDataApp/imUrlDataApp Code Change Diary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,27 @@ ViewController.swift
- onAppSendButton call messager to display

MessageComposerInApp.swift
- added as NSObject, MFMessageComposeViewControllerDelegate
- added as NSObject, MFMessageComposeViewControllerDelegate


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Fix app sender to send last-tapped emoji (was always "happy")
2022-08-06

MessagesViewController.swift
- Mood enum moved out to its own file
- local let moodKey moved to Mood enum
- subscript added

MessageComposingHelper
- displayMessageInterface take a mood param instead of using hardcoded strings

ViewController
- onAppSendButton pass mood to displayMessageInterface
- buttonFor(mood) helper added
- tap(mood) added
- viewDidLoad
- call tap(mood) to get highlighted
- set button isEnabled state (now we're not using them to toggle back on, should only be tappable when enabled)
- onHappy…Angry - call tap(mood:) instead of toggle, so tapping emoji sets the one to send
- matchButtonsToToggles - set isEnabled state
13 changes: 2 additions & 11 deletions imUrlDataApp/imUrlDataApp IM/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ import UIKit
import os
import Messages


enum Mood : String {
case happy
case quizzical
case distraught
case angry
}

class MessagesViewController: MSMessagesAppViewController {

@IBOutlet fileprivate weak var happyBtn: UIButton!
Expand All @@ -27,7 +19,6 @@ class MessagesViewController: MSMessagesAppViewController {
@IBOutlet fileprivate weak var statusLabel: UILabel!

var receivedMood:Mood? = nil
let moodKey = "mood"
let responseKey = "respondingTo"
let senderTimestampKey = "sentTS"
var enabled = [Bool]()
Expand Down Expand Up @@ -62,7 +53,7 @@ class MessagesViewController: MSMessagesAppViewController {
}
os_log("hasIncoming parsing message")
guard let comps = URLComponents(url: url, resolvingAgainstBaseURL: true) else { return }
if let msgMood = comps.queryItems?.first(where: { $0.name == moodKey })?.value {
if let msgMood = comps.queryItems?.first(where: { $0.name == Mood.moodKey })?.value {
if let parsedMood = Mood(rawValue: msgMood) {
receivedMood = parsedMood
statusLabel.text = "Respond to '\(msgMood)'"
Expand Down Expand Up @@ -155,7 +146,7 @@ class MessagesViewController: MSMessagesAppViewController {
fatalError("Invalid base URL")
}
var qi = [
URLQueryItem(name:moodKey, value:mood.rawValue)
URLQueryItem(name: Mood.moodKey, value:mood.rawValue)
]
if receivedMood != nil {
qi.append(URLQueryItem(name:responseKey, value:receivedMood!.rawValue))
Expand Down
6 changes: 6 additions & 0 deletions imUrlDataApp/imUrlDataApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
4201A9D521F0763B0043E369 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4201A9D321F0763B0043E369 /* Main.storyboard */; };
4201A9D721F0763C0043E369 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4201A9D621F0763C0043E369 /* Assets.xcassets */; };
4201A9DA21F0763C0043E369 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4201A9D821F0763C0043E369 /* LaunchScreen.storyboard */; };
42089152289E15FB0036C790 /* Mood.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42089151289E15FB0036C790 /* Mood.swift */; };
42089153289E15FB0036C790 /* Mood.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42089151289E15FB0036C790 /* Mood.swift */; };
42199E9021F1CA5600D4377E /* Messages.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 42199E8F21F1CA5600D4377E /* Messages.framework */; };
42199E9321F1CA5600D4377E /* MessagesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42199E9221F1CA5600D4377E /* MessagesViewController.swift */; };
42199E9621F1CA5600D4377E /* MainInterface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 42199E9421F1CA5600D4377E /* MainInterface.storyboard */; };
Expand Down Expand Up @@ -54,6 +56,7 @@
4201A9D621F0763C0043E369 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
4201A9D921F0763C0043E369 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
4201A9DB21F0763C0043E369 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
42089151289E15FB0036C790 /* Mood.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mood.swift; sourceTree = "<group>"; };
42199E8D21F1CA5600D4377E /* imUrlDataApp IM.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = "imUrlDataApp IM.appex"; sourceTree = BUILT_PRODUCTS_DIR; };
42199E8F21F1CA5600D4377E /* Messages.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Messages.framework; path = System/Library/Frameworks/Messages.framework; sourceTree = SDKROOT; };
42199E9221F1CA5600D4377E /* MessagesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessagesViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -144,6 +147,7 @@
isa = PBXGroup;
children = (
42199EA421F22CA400D4377E /* SharedData.swift */,
42089151289E15FB0036C790 /* Mood.swift */,
);
path = Common;
sourceTree = "<group>";
Expand Down Expand Up @@ -261,6 +265,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
42089152289E15FB0036C790 /* Mood.swift in Sources */,
42199EA521F22CA400D4377E /* SharedData.swift in Sources */,
4201A9D221F0763B0043E369 /* ViewController.swift in Sources */,
4276811F289D2D8B008FDF6C /* MessageComposerInApp.swift in Sources */,
Expand All @@ -273,6 +278,7 @@
buildActionMask = 2147483647;
files = (
42199EA621F22CA400D4377E /* SharedData.swift in Sources */,
42089153289E15FB0036C790 /* Mood.swift in Sources */,
42199E9321F1CA5600D4377E /* MessagesViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
5 changes: 3 additions & 2 deletions imUrlDataApp/imUrlDataApp/MessageComposerInApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import MessageUI
import Messages

class MessageComposingHelper: NSObject, MFMessageComposeViewControllerDelegate {

func canSendText() -> Bool {
MFMessageComposeViewController.canSendText()
}

func displayMessageInterface(onVC vc: UIViewController) {
func displayMessageInterface(onVC vc: UIViewController, mood:Mood) {
let composeVC = MFMessageComposeViewController()
composeVC.messageComposeDelegate = self

Expand All @@ -36,7 +37,7 @@ class MessageComposingHelper: NSObject, MFMessageComposeViewControllerDelegate {
guard var urlComps = URLComponents(string:"data:,") else {
fatalError("Invalid base URL")
}
urlComps.queryItems = [URLQueryItem(name:"mood", value:"happy")]
urlComps.queryItems = [URLQueryItem(name: Mood.moodKey, value:mood.rawValue)]
message.url = urlComps.url
composeVC.message = message
}
Expand Down
46 changes: 40 additions & 6 deletions imUrlDataApp/imUrlDataApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class ViewController: UIViewController {
@IBOutlet fileprivate weak var angrySwitch: UISwitch!
@IBOutlet fileprivate weak var appSendButton: UIButton!

lazy var toggles = [happySwitch, quizzicalSwitch, distraughtSwitch, angrySwitch]
lazy var toggles = [happySwitch, quizzicalSwitch, distraughtSwitch, angrySwitch] // maintains same order as Mood
lazy var buttons = [happyBtn, quizzicalBtn, distraughtBtn, angryBtn]
var lastToggled : controlIndexes? = nil
var messager = MessageComposingHelper()
var lastTappedMood = Mood.happy

// array of flags instead of storing state in the switch so can easily save and load
var enabled = [Bool]()
Expand All @@ -40,25 +41,58 @@ class ViewController: UIViewController {
enabled = SharedData.current.loadEnabled()
// initial loading loop - if read at least one false
if enabled.contains(false) {
var firstOn: Mood? = nil
for (i, isOn) in enabled.enumerated() {
if isOn && firstOn == nil {
firstOn = Mood[i]
}
buttons[i]?.alpha = isOn ? 1.0 : 0.3
buttons[i]?.isEnabled = isOn
toggles[i]?.isOn = isOn
// bit hacky, just set lastToggled to last off
lastToggled = isOn ? lastToggled : controlIndexes(rawValue: i)
}
lastTappedMood = firstOn ?? .happy
}
if !messager.canSendText() {
appSendButton.isEnabled = false
appSendButton.titleLabel?.text = "Not allowed to send texts"
}
tap(mood: lastTappedMood) // just to get it highlighted
}

func matchButtonsToToggles() {
for (i, isOn) in enabled.enumerated() {
buttons[i]?.alpha = isOn ? 1.0 : 0.3
buttons[i]?.isEnabled = isOn
}
}


func buttonFor(mood: Mood) -> UIButton {
switch mood {
case .happy:
return happyBtn
case .quizzical:
return quizzicalBtn
case .distraught:
return distraughtBtn
case .angry:
return angryBtn
}
}

func tap(mood: Mood) {
buttonFor(mood: lastTappedMood).layer.borderWidth = 0
buttonFor(mood: mood).layer.borderWidth = 4
if #available(iOS 13.0, *) {
buttonFor(mood: mood).layer.borderColor = CGColor(red: 099, green: 0.6, blue: 0.05, alpha: 1.0)
} else {
// Fallback on earlier versions
}
lastTappedMood = mood
}

func allOff() -> Bool {
return !enabled.contains(true)
}
Expand Down Expand Up @@ -98,25 +132,25 @@ class ViewController: UIViewController {


@IBAction func onHappy(_ sender: Any) {
toggle(.happy)
tap(mood: .happy)
}

@IBAction func onQuizzical(_ sender: Any) {
toggle(.quizzical)
tap(mood: .quizzical)
}

@IBAction func onDistraught(_ sender: Any) {
toggle(.distraught)
tap(mood: .distraught)
}

@IBAction func onAngry(_ sender: Any) {
toggle(.angry)
tap(mood: .angry)
}


/// see Readme and https://developer.apple.com/documentation/messageui/mfmessagecomposeviewcontroller
@IBAction func onAppSendButton(_ sender: Any) {
messager.displayMessageInterface(onVC: self)
messager.displayMessageInterface(onVC: self, mood: lastTappedMood)
}

}
Expand Down

0 comments on commit 9e30a15

Please sign in to comment.