Skip to content

Commit

Permalink
feat: added username
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandram-Dutta committed Jan 12, 2024
1 parent 8b43668 commit 214a1c1
Show file tree
Hide file tree
Showing 25 changed files with 89 additions and 49 deletions.
6 changes: 3 additions & 3 deletions VITTY/VITTY.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@
path = View;
sourceTree = "<group>";
};
524B84302B46EF07006D18BD /* Community */ = {
524B84302B46EF07006D18BD /* Circle */ = {
isa = PBXGroup;
children = (
525AA7A62B4B014C003C6A12 /* Search */,
Expand All @@ -739,7 +739,7 @@
524B84372B46F550006D18BD /* AddFriends */,
524B84312B46EF28006D18BD /* View */,
);
path = Community;
path = Circle;
sourceTree = "<group>";
};
524B84312B46EF28006D18BD /* View */ = {
Expand Down Expand Up @@ -935,7 +935,7 @@
5D3D924F2A1816980013803E /* Core */ = {
isa = PBXGroup;
children = (
524B84302B46EF07006D18BD /* Community */,
524B84302B46EF07006D18BD /* Circle */,
524B842C2B46EBA6006D18BD /* Home */,
5DB03A312A85553800DB7E60 /* SideMenu */,
5DE8C00C2A8FC7B100AE3995 /* FriendCircle */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct CommunityPageHeader: View {

var body: some View {
HStack {
Text("Community")
Text("Circle")
Spacer()
if !(friendRequestViewModel.error) && !(friendRequestViewModel.loading) {

Expand Down
2 changes: 1 addition & 1 deletion VITTY/VITTY/Core/Home/View/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct HomeView: View {
}
CommunityPage()
.tabItem {
Label("Friends", systemImage: "person.2")
Label("Circle", systemImage: "person.2")
}
}
}
Expand Down
81 changes: 42 additions & 39 deletions VITTY/VITTY/Core/Username/View/UserName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,52 +78,55 @@ extension UserName {

private func continueButton() -> some View {
Button {
if vm.isRegistrationNumberValid() {
if !vm.usernameTF.isEmpty && !vm.regNoTF.isEmpty {
print(authVM.loggedInUser?.uid ?? "no uid from username")
vm.isUsernameValid { isValidUsername in
if vm.isRegistrationNumberValid() && isValidUsername {
if !vm.usernameTF.isEmpty && !vm.regNoTF.isEmpty {
print(authVM.loggedInUser?.uid ?? "no uid from username")

API.shared.signInUser(
with: AuthReqBody(
uuid: authVM.loggedInUser?.uid ?? "",
reg_no: vm.regNoTF,
username: vm.usernameTF
)
) { result in
switch result {
case let .success(response):
API.shared.signInUser(
with: AuthReqBody(
uuid: authVM.loggedInUser?.uid ?? "",
reg_no: vm.regNoTF,
username: vm.usernameTF
)
) { result in
switch result {
case let .success(response):

DispatchQueue.main.async {
authVM.myUser = response
DispatchQueue.main.async {
authVM.myUser = response

UserDefaults.standard.set(
authVM.myUser.token,
forKey: AuthService.tokenKey
)
UserDefaults.standard.set(
authVM.myUser.username,
forKey: AuthService.userKey
)
UserDefaults.standard.set(
authVM.myUser.name,
forKey: AuthService.nameKey
)
UserDefaults.standard.set(
authVM.myUser.picture,
forKey: AuthService.imageKey
)
UserDefaults.standard.set(
authVM.myUser.token,
forKey: AuthService.tokenKey
)
UserDefaults.standard.set(
authVM.myUser.username,
forKey: AuthService.userKey
)
UserDefaults.standard.set(
authVM.myUser.name,
forKey: AuthService.nameKey
)
UserDefaults.standard.set(
authVM.myUser.picture,
forKey: AuthService.imageKey
)

print("created new user")
}
case let .failure(error):
print("error while creating the user ", error.localizedDescription)
print("created new user")
}
case let .failure(error):
print("error while creating the user ", error.localizedDescription)
}
}
}

authVM.isNewUser = false
authVM.isNewUser = false
}
}
else {
vm.regNoTF = ""
vm.usernameTF = ""
}
}
else {
vm.regNoTF = ""
}
} label: {
Text("Continue")
Expand Down
37 changes: 37 additions & 0 deletions VITTY/VITTY/Core/Username/ViewModel/UserNameViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,41 @@ class UserNameViewModel: ObservableObject {
let range = NSRange(location: 0, length: regNoTF.utf16.count)
return regex.firstMatch(in: regNoTF, options: [], range: range) != nil
}

func isUsernameValid(completion: @escaping (Bool) -> Void) {
let parameters = "{\n \"username\": \"\(usernameTF)\"\n}\n\n\n\n"
let postData = parameters.data(using: .utf8)

var request = URLRequest(url: URL(string: "\(APIConstants.base_url)/api/v2/auth/check-username")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

request.httpMethod = "POST"
request.httpBody = postData

let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
completion(false)
return
}

do {
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
if let detail = json["detail"] as? String, detail == "Username is valid" {
print("Username is valid")
completion(true)
} else {
print("Username is not valid")
completion(false)
}
}
} catch let error {
print("Error parsing JSON: \(error)")
completion(false)
}
}

task.resume()
}

}
4 changes: 2 additions & 2 deletions VITTY/VITTY/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.266303676876-77duk9tr18717lspccrvjuqcnuv0dp2s</string>
<string>com.googleusercontent.apps.272763363329-i8n51oo9m30h9it7qq9ufmd0lahnmm63</string>
</array>
</dict>
</array>
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
<key>GIDClientID</key>
<string>266303676876-77duk9tr18717lspccrvjuqcnuv0dp2s.apps.googleusercontent.com</string>
<string>272763363329-i8n51oo9m30h9it7qq9ufmd0lahnmm63.apps.googleusercontent.com</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
Expand Down
2 changes: 1 addition & 1 deletion VITTY/VITTY/Services/AuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class AuthService: NSObject, ObservableObject {
static let notifsSetupKey = "notifsSetupKey"

static let tokenKey = "token"
static let userKey = "username"
static let userKey = "userName"
static let nameKey = "name"
static let imageKey = "image"

Expand Down
2 changes: 1 addition & 1 deletion VITTY/VITTY/Utilities/Constants/APIConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import Foundation

struct APIConstants {
static let base_url = "https://vitty-api.dhruvshah.live"
static let base_url = "https://vitty-api.dscvit.com"
}

0 comments on commit 214a1c1

Please sign in to comment.