diff --git a/Blink/Blink.xcodeproj/project.pbxproj b/Blink/Blink.xcodeproj/project.pbxproj index 49257f8..90d705b 100644 --- a/Blink/Blink.xcodeproj/project.pbxproj +++ b/Blink/Blink.xcodeproj/project.pbxproj @@ -166,6 +166,7 @@ 4F5F12F424B63DBD00A7D9E7 /* Views */ = { isa = PBXGroup; children = ( + A418231B24B39DDF0082962F /* MenuView.swift */, ); path = Views; sourceTree = ""; @@ -190,6 +191,7 @@ 4F5F12F724B63DF700A7D9E7 /* Views */ = { isa = PBXGroup; children = ( + A43323D124B4F3620027B909 /* BrainstormView.swift */, ); path = Views; sourceTree = ""; @@ -214,6 +216,7 @@ 4F5F12FA24B63E2200A7D9E7 /* Views */ = { isa = PBXGroup; children = ( + A4CC2B2B24B635CC00C1B165 /* VotingView.swift */, ); path = Views; sourceTree = ""; @@ -328,13 +331,10 @@ 4F5F12FC24B63E8200A7D9E7 /* Multipeer.swift */, A4CC2B2F24B6491700C1B165 /* Grid */, A418231924B39DDF0082962F /* AppDelegate.swift */, - A418231B24B39DDF0082962F /* MenuView.swift */, A418231D24B39DE10082962F /* Assets.xcassets */, A418232224B39DE10082962F /* LaunchScreen.storyboard */, A418232524B39DE10082962F /* Info.plist */, A418231F24B39DE10082962F /* Preview Content */, - A43323D124B4F3620027B909 /* BrainstormView.swift */, - A4CC2B2B24B635CC00C1B165 /* VotingView.swift */, A4CC2B2D24B63AD300C1B165 /* RankingView.swift */, ); path = Blink; @@ -612,7 +612,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"Blink_iOS/Preview Content\""; - DEVELOPMENT_TEAM = 93JG72P729; + DEVELOPMENT_TEAM = 4LXC4A87QZ; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = Blink_iOS/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 13.4; @@ -634,7 +634,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"Blink_iOS/Preview Content\""; - DEVELOPMENT_TEAM = 93JG72P729; + DEVELOPMENT_TEAM = 4LXC4A87QZ; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = Blink_iOS/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 13.4; @@ -816,7 +816,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = "App Icon & Top Shelf Image"; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"Blink/Preview Content\""; - DEVELOPMENT_TEAM = L76X5KFZD9; + DEVELOPMENT_TEAM = 4LXC4A87QZ; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = Blink/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -836,7 +836,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = "App Icon & Top Shelf Image"; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"Blink/Preview Content\""; - DEVELOPMENT_TEAM = L76X5KFZD9; + DEVELOPMENT_TEAM = 4LXC4A87QZ; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = Blink/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( diff --git a/Blink/Blink/AppDelegate.swift b/Blink/Blink/AppDelegate.swift index 99cc510..b316f80 100644 --- a/Blink/Blink/AppDelegate.swift +++ b/Blink/Blink/AppDelegate.swift @@ -18,7 +18,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Create the SwiftUI view that provides the window contents. - let contentView = MenuView() + let contentView = BrainstormView() // Use a UIHostingController as window root view controller. let window = UIWindow(frame: UIScreen.main.bounds) diff --git a/Blink/Blink/Brainstorming/ViewModels/BrainstormingViewModel.swift b/Blink/Blink/Brainstorming/ViewModels/BrainstormingViewModel.swift index b9b5ef0..0816850 100644 --- a/Blink/Blink/Brainstorming/ViewModels/BrainstormingViewModel.swift +++ b/Blink/Blink/Brainstorming/ViewModels/BrainstormingViewModel.swift @@ -9,17 +9,45 @@ import Foundation import MultipeerConnectivity -class BrainstormingViewModel: NSObject, MCSessionDelegate { +class BrainstormingViewModel: NSObject, ObservableObject { + /// Shared instance of the Multipeer Class. let multipeerConnection = Multipeer.shared + /// Published variable of the idea Matrix. + /// Any changes that occur in this variable will make the view update. + @Published var ideasMatrix: [[String]] = [[String]]() + + /// String array variable to store ideas. + /// When an idea is sent through P2P connection, + /// It will be stored in this array. var ideas: [String] + /// Initialization of the Brainstorm ViewModel. override init() { ideas = [] super.init() multipeerConnection.delegate = self } + + /// Internal functional that converts the idea String array + /// in an idea 2D String matrix with 3 columns and N rows. + /// This function is called with the following parameters: + /// - Parameter ideas: The String array that contains the ideas sent through P2P connection. + func convertIdeasArrayInMatrix(ideas: [String]) -> [[String]] { + var matrixIdeas: [[String]] = [[String]]() + var colIndex: Int = 0 + var rowIndex: Int = 0 + for idea in ideas { + if colIndex == 3 { + colIndex = 0 + } + matrixIdeas[rowIndex].append(idea) + rowIndex += 1 + colIndex += 1 + } + return matrixIdeas + } } extension BrainstormingViewModel: MCSessionDelegate { @@ -38,9 +66,11 @@ extension BrainstormingViewModel: MCSessionDelegate { func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) { if let text = String(data: data, encoding: .utf8) { - DispatchQueue.main.async { + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } if !self.ideas.contains(text) { self.ideas.append(text) + self.ideasMatrix = self.convertIdeasArrayInMatrix(ideas: self.ideas) } } } diff --git a/Blink/Blink/BrainstormView.swift b/Blink/Blink/Brainstorming/Views/BrainstormView.swift similarity index 79% rename from Blink/Blink/BrainstormView.swift rename to Blink/Blink/Brainstorming/Views/BrainstormView.swift index a09f99f..df968bf 100644 --- a/Blink/Blink/BrainstormView.swift +++ b/Blink/Blink/Brainstorming/Views/BrainstormView.swift @@ -10,9 +10,10 @@ import SwiftUI /// Representation of the brainstorming screen. struct BrainstormView: View { - /// The 2D matrix containing the ideas as Strings. - var ideas: [[String]] - + + /// Observed ViewModel so BrainstormView can get its data. + @ObservedObject var brainstormVM = BrainstormingViewModel() + /// The topic set for the session. var topic: String @@ -20,17 +21,14 @@ struct BrainstormView: View { var timer: String /// Initialize a new instance of this type. - /// - Parameter ideas: 2D matrix containing the ideas as Strings. Empty by default. /// - Parameter topic: The topic set for the session. Empty by default. /// - Parameter timer: The timer set for the sessiion. Empty by default. - init(ideas: [[String]] = [[String]](), - topic: String = "", + init(topic: String = "", timer: String = "") { - self.ideas = ideas self.topic = topic self.timer = timer } - + /// The body of a `BrainstormingView` var body: some View { VStack { @@ -44,14 +42,15 @@ struct BrainstormView: View { Spacer() Text(timer).font(.title) Spacer() - Text("\(ideas.reduce(0) { $0 + $1.count })").font(.headline) + Text("\(brainstormVM.ideasMatrix.reduce(0) { $0 + $1.count })").font(.headline) Spacer() } Spacer() + /// The `GridView` used to layout the ideas in a /// 3-column grid. - GridView(items: ideas) + GridView(items: self.brainstormVM.ideasMatrix) Spacer() /// The HStack containing the buttons for @@ -79,3 +78,9 @@ struct BrainstormView: View { } } + +struct BrainstormView_Previews: PreviewProvider { + static var previews: some View { + /*@START_MENU_TOKEN@*/Text("Hello, World!")/*@END_MENU_TOKEN@*/ + } +} diff --git a/Blink/Blink/Menu/ViewModels/MenuViewModel.swift b/Blink/Blink/Menu/ViewModels/MenuViewModel.swift index d50023e..ef1cdae 100644 --- a/Blink/Blink/Menu/ViewModels/MenuViewModel.swift +++ b/Blink/Blink/Menu/ViewModels/MenuViewModel.swift @@ -9,7 +9,7 @@ import Foundation import MultipeerConnectivity -class MenuViewModel: NSObject, MCSessionDelegate { +class MenuViewModel: NSObject { let multipeerConnection = Multipeer.shared diff --git a/Blink/Blink/MenuView.swift b/Blink/Blink/Menu/Views/MenuView.swift similarity index 100% rename from Blink/Blink/MenuView.swift rename to Blink/Blink/Menu/Views/MenuView.swift diff --git a/Blink/Blink/VotingView.swift b/Blink/Blink/Voting/Views/VotingView.swift similarity index 91% rename from Blink/Blink/VotingView.swift rename to Blink/Blink/Voting/Views/VotingView.swift index d28ce1f..4eb3690 100644 --- a/Blink/Blink/VotingView.swift +++ b/Blink/Blink/Voting/Views/VotingView.swift @@ -60,3 +60,9 @@ struct VotingView: View { } } } + +struct VotingView_Previews: PreviewProvider { + static var previews: some View { + /*@START_MENU_TOKEN@*/Text("Hello, World!")/*@END_MENU_TOKEN@*/ + } +}