Skip to content
Snippets Groups Projects
Commit 6f10ee6d authored by Tormod Mork Müller's avatar Tormod Mork Müller :computer:
Browse files

Added support for getting user info of the logged in user

parent 2973ee67
Branches
No related tags found
1 merge request!110Made changes for the Mobile application
......@@ -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
}
......
//
// 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
}
//
// 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()
}
}
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment