Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Refactoring v0.0.1 #16

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
52c5c48
update xcuserstat
MohamedSaiko Apr 16, 2024
c20b9da
add .ignore
MohamedSaiko Apr 16, 2024
931588a
update productDetailsView
MohamedSaiko Apr 16, 2024
3cde985
update BrandView and DescriptionView
MohamedSaiko Apr 16, 2024
327da91
update CartView
MohamedSaiko Apr 16, 2024
312fd40
Update CartNetworkManager
MohamedSaiko Apr 16, 2024
dd5e4a2
update loginView
MohamedSaiko Apr 16, 2024
c65accd
update constants
MohamedSaiko Apr 16, 2024
96fe18a
update NetworkManager
MohamedSaiko Apr 16, 2024
3a17003
update ContentView
MohamedSaiko Apr 16, 2024
286086e
update ViewManager
MohamedSaiko Apr 16, 2024
c7602d2
update LocationDataManager
MohamedSaiko Apr 16, 2024
75de36c
delete CategoriesView
MohamedSaiko Apr 16, 2024
145b14d
update project
MohamedSaiko Apr 16, 2024
ed01257
add .gitignore
MohamedSaiko Apr 17, 2024
ba652e1
update LocationDataManager
MohamedSaiko Apr 17, 2024
e58d45f
add xcuserState to .ignore
MohamedSaiko Apr 17, 2024
28157da
Update ProfileView
MohamedSaiko Apr 20, 2024
802257c
update ProductDetailsView
MohamedSaiko Apr 20, 2024
1024691
Update ContentView
MohamedSaiko Apr 20, 2024
725ee9d
Update CartView
MohamedSaiko Apr 20, 2024
7312503
Update CartView
MohamedSaiko Apr 22, 2024
bd61ac0
Update ContentView
MohamedSaiko Apr 22, 2024
89e51ea
Update HomeView
MohamedSaiko Apr 22, 2024
5b5015b
Update HomeViewModel
MohamedSaiko Apr 22, 2024
da2dfa2
Update iStoreApp
MohamedSaiko Apr 22, 2024
14720ae
Update LoginView
MohamedSaiko Apr 22, 2024
3c308f3
Update NavigationCoordinator
MohamedSaiko Apr 22, 2024
403608a
Update ProductDetailsView
MohamedSaiko Apr 22, 2024
c2bd6a1
Update ProfileView
MohamedSaiko Apr 22, 2024
4a55813
Update ViewManager
MohamedSaiko Apr 22, 2024
09a7ec8
Update ProductDetailsView
MohamedSaiko Apr 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

iStore.xcodeproj/project.xcworkspace/xcuserdata/mohamedsayed.xcuserdatad/UserInterfaceState.xcuserstate
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you add this change?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It caused Problems with Applying stashed or merging other branches into the branch.

*.xcuserstate
*.xcuserstate
*.xcuserstate
20 changes: 0 additions & 20 deletions iStore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
41F8DC102B871D82000582A1 /* UserAddressButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41F8DC0C2B871D82000582A1 /* UserAddressButton.swift */; };
41F8DC112B871D82000582A1 /* ProductsGrid.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41F8DC0D2B871D82000582A1 /* ProductsGrid.swift */; };
41F8DC132B871DB0000582A1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 41F8DC122B871DB0000582A1 /* Assets.xcassets */; };
41F8DC182B871DCF000582A1 /* CategoriesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41F8DC152B871DCF000582A1 /* CategoriesViewModel.swift */; };
41F8DC192B871DCF000582A1 /* CategoriesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41F8DC162B871DCF000582A1 /* CategoriesView.swift */; };
41F8DC1A2B871DCF000582A1 /* CategoriesGrid.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41F8DC172B871DCF000582A1 /* CategoriesGrid.swift */; };
41F8DC1C2B871E09000582A1 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41F8DC1B2B871E09000582A1 /* Constants.swift */; };
41F8DC202B871E1F000582A1 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41F8DC1E2B871E1F000582A1 /* User.swift */; };
41F8DC212B871E1F000582A1 /* ProductsData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41F8DC1F2B871E1F000582A1 /* ProductsData.swift */; };
Expand Down Expand Up @@ -82,9 +79,6 @@
41F8DC0C2B871D82000582A1 /* UserAddressButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UserAddressButton.swift; path = Home/UserAddressButton.swift; sourceTree = "<group>"; };
41F8DC0D2B871D82000582A1 /* ProductsGrid.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ProductsGrid.swift; path = Home/ProductsGrid.swift; sourceTree = "<group>"; };
41F8DC122B871DB0000582A1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
41F8DC152B871DCF000582A1 /* CategoriesViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CategoriesViewModel.swift; sourceTree = "<group>"; };
41F8DC162B871DCF000582A1 /* CategoriesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CategoriesView.swift; sourceTree = "<group>"; };
41F8DC172B871DCF000582A1 /* CategoriesGrid.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CategoriesGrid.swift; sourceTree = "<group>"; };
41F8DC1B2B871E09000582A1 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
41F8DC1E2B871E1F000582A1 /* User.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = "<group>"; };
41F8DC1F2B871E1F000582A1 /* ProductsData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProductsData.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -137,7 +131,6 @@
41F8DBF62B871D45000582A1 /* NetworkLayer */,
419612472BA993F800938360 /* Profile */,
4193243E2B8CCC530015C45D /* Cart */,
41F8DC142B871DCF000582A1 /* Categories */,
41F8DC322B871EDC000582A1 /* ProductDetails */,
41F8DC092B871D82000582A1 /* Home */,
4161881E2BA1682700A96EB6 /* Login */,
Expand Down Expand Up @@ -220,16 +213,6 @@
name = Home;
sourceTree = "<group>";
};
41F8DC142B871DCF000582A1 /* Categories */ = {
isa = PBXGroup;
children = (
41F8DC152B871DCF000582A1 /* CategoriesViewModel.swift */,
41F8DC162B871DCF000582A1 /* CategoriesView.swift */,
41F8DC172B871DCF000582A1 /* CategoriesGrid.swift */,
);
path = Categories;
sourceTree = "<group>";
};
41F8DC1D2B871E1F000582A1 /* Models */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -346,15 +329,13 @@
419612442BA98B2300938360 /* AuthenticatedUser.swift in Sources */,
41F8DC212B871E1F000582A1 /* ProductsData.swift in Sources */,
419324422B8CDBDE0015C45D /* CartData.swift in Sources */,
41F8DC192B871DCF000582A1 /* CategoriesView.swift in Sources */,
41C96C3E2B9C795C004511E8 /* CheckOutButton.swift in Sources */,
415A0D5D2BA17F1A00ECC38A /* PasswordView.swift in Sources */,
4193243D2B8CCC2F0015C45D /* CartView.swift in Sources */,
410BCA502BA5B38A0013F568 /* ViewManager.swift in Sources */,
410BCA4D2BA5B1CB0013F568 /* NavigationCoordinator.swift in Sources */,
DB208D472BC8479400D3B71D /* LocationView.swift in Sources */,
41F8DC202B871E1F000582A1 /* User.swift in Sources */,
41F8DC1A2B871DCF000582A1 /* CategoriesGrid.swift in Sources */,
41F8DC1C2B871E09000582A1 /* Constants.swift in Sources */,
419324402B8CDBA30015C45D /* CartNetworkManager.swift in Sources */,
41F8DC112B871D82000582A1 /* ProductsGrid.swift in Sources */,
Expand All @@ -373,7 +354,6 @@
41F8DC0F2B871D82000582A1 /* HomeView.swift in Sources */,
41F8DC0E2B871D82000582A1 /* HomeViewModel.swift in Sources */,
41F8DC3E2B871EDD000582A1 /* ProductDetailsView.swift in Sources */,
41F8DC182B871DCF000582A1 /* CategoriesViewModel.swift in Sources */,
41F8DC102B871D82000582A1 /* UserAddressButton.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file not shown.
1 change: 0 additions & 1 deletion iStore/Cart/CartList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct CartList: View {

private let carts: [CartData]

init(carts: [CartData]) {
Expand Down
24 changes: 18 additions & 6 deletions iStore/Cart/CartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import SwiftUI

struct CartView: View {
@StateObject private var cartViewModel = CartViewModel(networkManager: NetworkManager(), cartManager: CartNetworkManager())
private let id: Int

@StateObject var cartViewModel = CartViewModel(networkManager: NetworkManager(), cartManager: CartNetworkManager())

@EnvironmentObject var authenticationViewModel: AuthenticationViewModel
init(id: Int) {
self.id = id
}

var body: some View {
NavigationView {
NavigationStack {
VStack() {
if cartViewModel.carts.isEmpty {
ProgressView()
ContentUnavailableView("No products", systemImage: "cart", description: Text("You don't have any productss yet."))
} else {
CartList(carts: cartViewModel.carts)
.listStyle(.plain)
Expand All @@ -36,7 +38,17 @@ struct CartView: View {
.navigationBarTitleDisplayMode(.inline)
}
.task {
cartViewModel.loadUserCarts(withUserId: authenticationViewModel.user.id)
cartViewModel.loadUserCarts(withUserId: id) { result in
switch result {
case .success(let data):
DispatchQueue.main.async {
cartViewModel.carts.removeAll()
cartViewModel.carts.append(contentsOf: data.carts)
}
case .failure(let error):
print(NetworkError.unknownError(error))
}
}
}
}
}
15 changes: 5 additions & 10 deletions iStore/Cart/CartViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation

final class CartViewModel: ObservableObject {

private let networkManager: NetworkManager
private let cartManager: CartNetworkManager

Expand All @@ -19,19 +18,15 @@ final class CartViewModel: ObservableObject {
self.cartManager = cartManager
}

func loadUserCarts(withUserId userID: Int) {

func loadUserCarts(withUserId userID: Int, completion: @escaping (Result<Cart, NetworkError>) -> Void) {
let url = userCartURL + "\(userID)"

networkManager.loadData(withURL: url) { (result: Result<Cart,NetworkError>) in
switch result {
case .success(let data):
DispatchQueue.main.async {
self.carts.removeAll()
self.carts.append(contentsOf: data.carts)
}
case .failure(let error):
print(NetworkError.unknownError(error))
case .success(let data):
completion(.success(data))
case .failure(let error):
completion(.failure(error))
}
}
}
Expand Down
1 change: 0 additions & 1 deletion iStore/Cart/CheckOutButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct CheckOutButton: View {

private let total: Int
private let action: () -> Void

Expand Down
1 change: 0 additions & 1 deletion iStore/CartNetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct CartNetworkManager {
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

let task = URLSession.shared.uploadTask(with: request, from: uploadData) { data, response , error in

if error != nil {
print (CartRequestError.invalidCart)
return
Expand Down
20 changes: 0 additions & 20 deletions iStore/Categories/CategoriesGrid.swift

This file was deleted.

32 changes: 0 additions & 32 deletions iStore/Categories/CategoriesView.swift

This file was deleted.

43 changes: 0 additions & 43 deletions iStore/Categories/CategoriesViewModel.swift

This file was deleted.

5 changes: 0 additions & 5 deletions iStore/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
//

import Foundation

// MARK: Colors

// MARK: NavigationBar Titles

let iStore = "iStore"
let categories = "Categories"

// MARK: Networking Constants

Expand All @@ -26,4 +22,3 @@ let userURL = "https://dummyjson.com/auth/me"

let addToCartURL = "https://dummyjson.com/carts/add"
let userCartURL = "https://dummyjson.com/carts/user/"

20 changes: 15 additions & 5 deletions iStore/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@
import SwiftUI

struct ContentView: View {
@EnvironmentObject private var locationDataManager: LocationDataManager

private let user: AuthenticatedUser
private let isGuest: Bool

init(user: AuthenticatedUser, isGuest: Bool) {
self.user = user
self.isGuest = isGuest
}

var body: some View {
TabView {
HomeView()
HomeView(id: user.id, homeAddress: user.address.address, homePostalCode: user.address.postalCode, homeCity: user.address.city, homeState: user.address.state, homeLatitude: user.address.coordinates.lat, homeLongitude: user.address.coordinates.lng, currentAddress: locationDataManager.address, currentPostalCode: locationDataManager.postalCode, currentCity: locationDataManager.city, currentState: locationDataManager.state, currentLatitude: locationDataManager.latitude, currentLongitude: locationDataManager.longitude, isCurrentLocation: locationDataManager.isCurrentLocation, isGuest: isGuest, getLocationPermission: locationDataManager.requestUserLocationPermission)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to create a custom object with all passing properties and pass it directly to the HomeView? just an idea!

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I will try it and push the changes.

.tabItem {
Image(systemName: "house.fill")
}

CartView()
CartView(id: user.id)
.tabItem {
Image(systemName: "bag.fill")
Image(systemName: "cart")
}

ProfileView()
ProfileView(username: user.username, firstName: user.firstName, lastName: user.lastName, maidenName: user.maidenName, email: user.email, phone: user.phone, image: user.image, homeAddress: user.address.address, homePostalCode: user.address.postalCode, homeCity: user.address.city, homeState: user.address.state, isGuest: isGuest)
.tabItem {
Image(systemName: "person.fill")
}
}
.accentColor(.pink)
.tint(.pink)
}
}
Loading