Mobile App Dev 2022W: Assignment 1: Difference between revisions

From Soma-notes
Created page with "'''This assignment is still being developed. Please answer all of the following questions in the [https://homeostasis.scs.carleton.ca/~soma/mad-2022w/templates/assign1-templa..."
 
No edit summary
Line 1: Line 1:
'''This assignment is still being developed.
'''This assignment is still being developed.


Please answer all of the following questions in the [https://homeostasis.scs.carleton.ca/~soma/mad-2022w/templates/assign1-template.txt supplied template], all of which are based on the code in [[Mobile App Dev 2022W: Tutorial 2|Tutorial 2]].  There are ?? questions worth ?? points.
Please answer all of the following questions in the [https://homeostasis.scs.carleton.ca/~soma/mad-2022w/templates/assign1-template.txt supplied template].  The questions are based on [https://homeostasis.scs.carleton.ca/~soma/mad-2022w/code/textanalyzer-2/ContentView.swift textanalyzer-2], which is an enhanced version of textanalyzer-1 from [[Mobile App Dev 2022W: Tutorial 2|Tutorial 2]].  There are ?? questions worth ?? points.


Do not turn in modified applications.  Instead, when answering code writing questions, include code snippets and explain how they should be used to modify the application.  Use complete sentences when answering.  ''Be sure to list collaborators and outside sources that you used.''
Do not turn in modified applications.  Instead, when answering code writing questions, include code snippets and explain how they should be used to modify the application.  Use complete sentences when answering.  ''Be sure to list collaborators and outside sources that you used.''
Line 12: Line 12:
<li></li>
<li></li>
</ol>
</ol>
==Code==
<syntaxhighlight lang="swift" line>
//
//  ContentView.swift for textanalyzer-2
//
//  Created by Anil Somayaji on 2022-01-26.
//
import SwiftUI
struct ContentView: View {
    @State private var t = ""
    @State private var analysisMode = "None"
    var body: some View {
        VStack{
            ModeMenu(analysisMode: $analysisMode)
            AppTitle(title: "Text Analyzer")
            TextField("Enter Text", text: $t).padding()
            AnalysisResult(mode: $analysisMode,
                          userInput: $t)
            Spacer()
        }
    }
}
struct AppTitle: View {
    var title: String
   
    var body: some View {
        Spacer()
        Text(title).font(.title).bold().padding()
        Spacer()
    }
}
struct AnalysisResult: View {
    @Binding var mode: String
    @Binding var userInput: String
   
    var body: some View {
        Spacer()
        if let analysisFunc = analysis[mode] {
            Text(mode + ": " + analysisFunc(userInput))
        } else {
            Text("Please Select a Mode")
        }
        Spacer()
    }
}
struct ModeMenu: View {
    @Binding var analysisMode: String
    var body: some View {
        let availModes = [String](analysis.keys)
        Menu("Analysis menu") {
            ForEach(availModes, id: \.self) {
                mode in
                Button(mode, action: {
                    analysisMode = mode
                })
            }
        }
    }
}
func countUpper(s: String) -> String {
    var count = 0
    let upperCase: Set<Character> =
    ["A","B","C","D","E","F","G","H","I","J","K","L","M",
    "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
   
    for c in s {
        if (upperCase.contains(c)) {
            count += 1
        }
    }
   
    return String(count)
}
func countPetsMentioned(s: String) -> String {
    var count = 0
    let pets = ["Roshi", "Tab", "Shift"]
   
    for p in pets {
        if (s.contains(p)) {
            count += 1
        }
    }
    return String(count)
}
let analysis: [String: (String) -> String] = [
    "Count": {s in return String(s.count)},
    "Empty": {s in return (s == "") ? "Yes" : "No"},
    "Upper Case": countUpper,
    "Pets Mentioned": countPetsMentioned
]
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
</syntaxhighlight>

Revision as of 22:46, 26 January 2022

This assignment is still being developed.

Please answer all of the following questions in the supplied template. The questions are based on textanalyzer-2, which is an enhanced version of textanalyzer-1 from Tutorial 2. There are ?? questions worth ?? points.

Do not turn in modified applications. Instead, when answering code writing questions, include code snippets and explain how they should be used to modify the application. Use complete sentences when answering. Be sure to list collaborators and outside sources that you used.

Submit your answers via Brightspace by February 3, 2021 by 11:59 PM.

Questions

Code

//
//  ContentView.swift for textanalyzer-2
//
//  Created by Anil Somayaji on 2022-01-26.
//

import SwiftUI

struct ContentView: View {
    @State private var t = ""
    @State private var analysisMode = "None"
    var body: some View {
        VStack{
            ModeMenu(analysisMode: $analysisMode)
            AppTitle(title: "Text Analyzer")
            TextField("Enter Text", text: $t).padding()
            AnalysisResult(mode: $analysisMode,
                           userInput: $t)
            Spacer()
        }
    }
}

struct AppTitle: View {
    var title: String
    
    var body: some View {
        Spacer()
        Text(title).font(.title).bold().padding()
        Spacer()
    }
}

struct AnalysisResult: View {
    @Binding var mode: String
    @Binding var userInput: String
    
    var body: some View {
        Spacer()
        if let analysisFunc = analysis[mode] {
            Text(mode + ": " + analysisFunc(userInput))
        } else {
            Text("Please Select a Mode")
        }
        Spacer()
    }
}

struct ModeMenu: View {
    @Binding var analysisMode: String

    var body: some View {
        let availModes = [String](analysis.keys)

        Menu("Analysis menu") {
            ForEach(availModes, id: \.self) {
                mode in
                Button(mode, action: {
                    analysisMode = mode
                })
            }
        }

    }
}

func countUpper(s: String) -> String {
    var count = 0
    let upperCase: Set<Character> =
    ["A","B","C","D","E","F","G","H","I","J","K","L","M",
     "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    
    for c in s {
        if (upperCase.contains(c)) {
            count += 1
        }
    }
    
    return String(count)
}

func countPetsMentioned(s: String) -> String {
    var count = 0
    let pets = ["Roshi", "Tab", "Shift"]
    
    for p in pets {
        if (s.contains(p)) {
            count += 1
        }
    }
    return String(count)
}

let analysis: [String: (String) -> String] = [
    "Count": {s in return String(s.count)},
    "Empty": {s in return (s == "") ? "Yes" : "No"},
    "Upper Case": countUpper,
    "Pets Mentioned": countPetsMentioned
]

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}