From 6f10ee6db2024881a74eff55693158c0de131d23 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tormod=20Mork=20Mu=CC=88ller?= <tormodmm@stud.ntnu.no>
Date: Wed, 11 May 2022 22:29:42 +0200
Subject: [PATCH] Added support for getting user info of the logged in user

---
 .../Login&Signup/AppViewModel.swift           |  7 ++
 .../Views/Model/Profile.swift                 | 23 +++++
 .../Views/Model/ProfileData.swift             | 35 ++++++++
 .../Views/ProfileView/ProfileView.swift       | 88 ++++++++++++++++++-
 4 files changed, 152 insertions(+), 1 deletion(-)
 create mode 100644 stillasMobileApplication/stillasMobileApplication/Views/Model/Profile.swift
 create mode 100644 stillasMobileApplication/stillasMobileApplication/Views/Model/ProfileData.swift

diff --git a/stillasMobileApplication/stillasMobileApplication/Login&Signup/AppViewModel.swift b/stillasMobileApplication/stillasMobileApplication/Login&Signup/AppViewModel.swift
index 99886bc..c5706ee 100644
--- a/stillasMobileApplication/stillasMobileApplication/Login&Signup/AppViewModel.swift
+++ b/stillasMobileApplication/stillasMobileApplication/Login&Signup/AppViewModel.swift
@@ -7,12 +7,19 @@
 
 import FirebaseAuth
 
+/// https://www.youtube.com/watch?v=vPCEIPL0U_k
+/// https://firebase.google.com/docs/auth/ios/start
 class AppViewModel: ObservableObject {
     
     let auth = Auth.auth()
     
     @Published var signedIn: Bool = false
     
+    var userID: String {
+        get { return auth.currentUser?.uid ?? "" }
+        set { self.userID = newValue }
+    }
+    
     var isSignedIn: Bool {
         return auth.currentUser != nil
     }
diff --git a/stillasMobileApplication/stillasMobileApplication/Views/Model/Profile.swift b/stillasMobileApplication/stillasMobileApplication/Views/Model/Profile.swift
new file mode 100644
index 0000000..6347641
--- /dev/null
+++ b/stillasMobileApplication/stillasMobileApplication/Views/Model/Profile.swift
@@ -0,0 +1,23 @@
+//
+//  Profile.swift
+//  stillasMobileApplication
+//
+//  Created by Tormod Mork Muller on 10/05/2022.
+//
+
+import Foundation
+
+// MARK: - Profile
+struct Profile: Codable {
+    let employeeID: String
+    var name: Name
+    let dateOfBirth, role: String
+    let phone: Int
+    let email: String
+    let admin: Bool
+}
+
+// MARK: - Name
+struct Name: Codable {
+    let firstName, lastName: String
+}
diff --git a/stillasMobileApplication/stillasMobileApplication/Views/Model/ProfileData.swift b/stillasMobileApplication/stillasMobileApplication/Views/Model/ProfileData.swift
new file mode 100644
index 0000000..fa9b2b4
--- /dev/null
+++ b/stillasMobileApplication/stillasMobileApplication/Views/Model/ProfileData.swift
@@ -0,0 +1,35 @@
+//
+//  ProfileData.swift
+//  stillasMobileApplication
+//
+//  Created by Tormod Mork Muller on 10/05/2022.
+//
+
+import SwiftUI
+import Foundation
+
+class ProfileData: ObservableObject {
+    @Published private var _isLoadingProfile: Bool = false
+    
+    var isLoadingProfile: Bool {
+        get { return _isLoadingProfile}
+    }
+    
+    func loadData(userID: String, completion:@escaping (Profile) -> ()) async {
+        _isLoadingProfile = true
+        print("One = \(_isLoadingProfile)")
+        
+        guard let url = URL(string: "http://10.212.138.205:8080/stillastracking/v1/api/user?id=\(userID)") else {
+            print("Invalid url...")
+            return
+        }
+        URLSession.shared.dataTask(with: url) { [self] data, response, error in
+            let profile = try! JSONDecoder().decode(Profile.self, from: data!)
+            DispatchQueue.main.async {
+                completion(profile)
+                self._isLoadingProfile = false
+                print("Two = \(self._isLoadingProfile)")
+            }
+        }.resume()
+    }
+}
diff --git a/stillasMobileApplication/stillasMobileApplication/Views/ProfileView/ProfileView.swift b/stillasMobileApplication/stillasMobileApplication/Views/ProfileView/ProfileView.swift
index 60b3c53..219f4e0 100644
--- a/stillasMobileApplication/stillasMobileApplication/Views/ProfileView/ProfileView.swift
+++ b/stillasMobileApplication/stillasMobileApplication/Views/ProfileView/ProfileView.swift
@@ -7,6 +7,92 @@
 
 import SwiftUI
 
+/**
+    ProfileView - Calls the ProfileDetails view containing information about a user
+ */
+struct ProfileView: View {
+    var body: some View {
+        VStack {
+            ProfileDetails()
+        }
+    }
+}
+
+/**
+    ProfileDetails - A view responsible for the layout of the user information and showing the details about the user
+ */
+struct ProfileDetails: View {
+    @EnvironmentObject var viewModel: AppViewModel
+    @ObservedObject var profileModel: ProfileData = ProfileData()
+
+    @State var user: [Profile] = [Profile]()
+    
+    var body: some View {
+        ScrollView {
+            /// MapView displaying the map in the top of the screen
+            MapView()
+                .ignoresSafeArea(edges: .top)
+                .frame(height: 300)
+            /// CircleImage responsible for displaying the user profile image
+            CircleImage(image: Image("UserProfile"))
+                .offset(y: -130)
+                .padding(.bottom, -130)
+        
+            if (!user.isEmpty) {
+            /// A VStack used to display all the user profile data
+            VStack(alignment: .leading) {
+                HStack {
+                    Text("\(user[0].name.firstName) \(user[0].name.lastName)")
+                       .font(.largeTitle)
+               }
+                
+                HStack {
+                    Text("MBStillas")
+                        //.font(.subheadline)
+                    Spacer()
+                    Text("Rolle: \(user[0].role)")
+                        //.font(.subheadline)
+                }
+                //.font(.subheadline)
+                .foregroundColor(.secondary)
+                
+                Divider()
+
+                VStack {
+                    Text("Fødselsdato")
+                        .font(.title2)
+                    Text("\(user[0].dateOfBirth)")
+                        .foregroundColor(.secondary)
+                }
+            }
+            .padding()
+            
+            Spacer()
+            
+            Button (action: {
+                viewModel.signOut()
+            }) {
+                Text("Logg av")
+                    .frame(width: 150, height: 50, alignment: .center)
+            }
+            .foregroundColor(.white)
+            .background(Color.blue)
+            .cornerRadius(10)
+            
+            Spacer()
+                .frame(height:50)  // limit spacer size by applying a frame
+            }
+        }
+        .task {
+            await profileModel.loadData(userID: viewModel.userID) { (user) in
+                self.user.append(user)
+            }
+        }
+        .ignoresSafeArea(edges: .top)
+    }
+}
+
+/*
 /**
     ProfileView - Calls the ProfileDetails view containing information about a user
  */
@@ -92,7 +178,7 @@ struct ProfileDetails: View {
         .navigationBarTitleDisplayMode(.inline)
         .ignoresSafeArea(edges: .top)
     }
-}
+}*/
 
 struct ProfileView_Previews: PreviewProvider {
     static var previews: some View {
-- 
GitLab