diff --git a/Evidencias/1.png b/Evidencias/1.png new file mode 100644 index 0000000..2817a95 Binary files /dev/null and b/Evidencias/1.png differ diff --git a/Evidencias/2.png b/Evidencias/2.png new file mode 100644 index 0000000..e0ba5da Binary files /dev/null and b/Evidencias/2.png differ diff --git a/Evidencias/3.png b/Evidencias/3.png new file mode 100644 index 0000000..712013f Binary files /dev/null and b/Evidencias/3.png differ diff --git a/README.md b/README.md index 0ef43ea..75b6dd3 100644 --- a/README.md +++ b/README.md @@ -1 +1,43 @@ # tesffullstack + +# frontend_meneses + +Aplicación iOS desarrollada en **SwiftUI** que consume un servicio REST creado en Spring Boot para la gestión de personas (CRUD). + +--- + +# Descripción + +La aplicación permite: + +- Visualizar listado de personas +- Crear nuevas personas +- Editar personas existentes +- Eliminar personas +- Consumir API REST desarrollada en Spring Boot +- Mostrar información almacenada en MySQL + +Se implementó el patrón de arquitectura **MVVM (Model – View – ViewModel)**. + +--- + +# Requisitos + +Antes de ejecutar el proyecto asegúrate de tener instalado: + +- Xcode 15+ +- Swift 5+ +- macOS +- Backend Spring Boot ejecutándose en: http://localhost:8080 + +## Evidencias de Funcionamiento + +A continuación se muestran las pantallas del funcionamiento de la aplicacion. + +* ![Evidencia 1](./Evidencias/1.png) + +* ![Evidencia 2](./Evidencias/2.png) + +* ![Evidencia 3](./Evidencias/3.png) + + diff --git a/frontend_meneses/frontend_meneses.xcodeproj/project.pbxproj b/frontend_meneses/frontend_meneses.xcodeproj/project.pbxproj new file mode 100644 index 0000000..06c1a50 --- /dev/null +++ b/frontend_meneses/frontend_meneses.xcodeproj/project.pbxproj @@ -0,0 +1,337 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 77; + objects = { + +/* Begin PBXFileReference section */ + 6ED7DCBB2F5296BE0066D774 /* frontend_meneses.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = frontend_meneses.app; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFileSystemSynchronizedRootGroup section */ + 6ED7DCBD2F5296BE0066D774 /* frontend_meneses */ = { + isa = PBXFileSystemSynchronizedRootGroup; + path = frontend_meneses; + sourceTree = ""; + }; +/* End PBXFileSystemSynchronizedRootGroup section */ + +/* Begin PBXFrameworksBuildPhase section */ + 6ED7DCB82F5296BE0066D774 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 6ED7DCB22F5296BE0066D774 = { + isa = PBXGroup; + children = ( + 6ED7DCBD2F5296BE0066D774 /* frontend_meneses */, + 6ED7DCBC2F5296BE0066D774 /* Products */, + ); + sourceTree = ""; + }; + 6ED7DCBC2F5296BE0066D774 /* Products */ = { + isa = PBXGroup; + children = ( + 6ED7DCBB2F5296BE0066D774 /* frontend_meneses.app */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 6ED7DCBA2F5296BE0066D774 /* frontend_meneses */ = { + isa = PBXNativeTarget; + buildConfigurationList = 6ED7DCC62F5296BF0066D774 /* Build configuration list for PBXNativeTarget "frontend_meneses" */; + buildPhases = ( + 6ED7DCB72F5296BE0066D774 /* Sources */, + 6ED7DCB82F5296BE0066D774 /* Frameworks */, + 6ED7DCB92F5296BE0066D774 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + fileSystemSynchronizedGroups = ( + 6ED7DCBD2F5296BE0066D774 /* frontend_meneses */, + ); + name = frontend_meneses; + packageProductDependencies = ( + ); + productName = frontend_meneses; + productReference = 6ED7DCBB2F5296BE0066D774 /* frontend_meneses.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 6ED7DCB32F5296BE0066D774 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 2620; + LastUpgradeCheck = 2620; + TargetAttributes = { + 6ED7DCBA2F5296BE0066D774 = { + CreatedOnToolsVersion = 26.2; + }; + }; + }; + buildConfigurationList = 6ED7DCB62F5296BE0066D774 /* Build configuration list for PBXProject "frontend_meneses" */; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 6ED7DCB22F5296BE0066D774; + minimizedProjectReferenceProxies = 1; + preferredProjectObjectVersion = 77; + productRefGroup = 6ED7DCBC2F5296BE0066D774 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 6ED7DCBA2F5296BE0066D774 /* frontend_meneses */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 6ED7DCB92F5296BE0066D774 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 6ED7DCB72F5296BE0066D774 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 6ED7DCC42F5296BF0066D774 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + DEVELOPMENT_TEAM = S3526625G8; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 26.2; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 6ED7DCC52F5296BF0066D774 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + DEVELOPMENT_TEAM = S3526625G8; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 26.2; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 6ED7DCC72F5296BF0066D774 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = S3526625G8; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.steff.frontend-meneses"; + PRODUCT_NAME = "$(TARGET_NAME)"; + STRING_CATALOG_GENERATE_SYMBOLS = YES; + SWIFT_APPROACHABLE_CONCURRENCY = YES; + SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 6ED7DCC82F5296BF0066D774 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = S3526625G8; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.steff.frontend-meneses"; + PRODUCT_NAME = "$(TARGET_NAME)"; + STRING_CATALOG_GENERATE_SYMBOLS = YES; + SWIFT_APPROACHABLE_CONCURRENCY = YES; + SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 6ED7DCB62F5296BE0066D774 /* Build configuration list for PBXProject "frontend_meneses" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 6ED7DCC42F5296BF0066D774 /* Debug */, + 6ED7DCC52F5296BF0066D774 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 6ED7DCC62F5296BF0066D774 /* Build configuration list for PBXNativeTarget "frontend_meneses" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 6ED7DCC72F5296BF0066D774 /* Debug */, + 6ED7DCC82F5296BF0066D774 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 6ED7DCB32F5296BE0066D774 /* Project object */; +} diff --git a/frontend_meneses/frontend_meneses.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/frontend_meneses/frontend_meneses.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/frontend_meneses/frontend_meneses.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/frontend_meneses/frontend_meneses.xcodeproj/project.xcworkspace/xcuserdata/americomq.xcuserdatad/UserInterfaceState.xcuserstate b/frontend_meneses/frontend_meneses.xcodeproj/project.xcworkspace/xcuserdata/americomq.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..7e42105 Binary files /dev/null and b/frontend_meneses/frontend_meneses.xcodeproj/project.xcworkspace/xcuserdata/americomq.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/frontend_meneses/frontend_meneses.xcodeproj/xcuserdata/americomq.xcuserdatad/xcschemes/xcschememanagement.plist b/frontend_meneses/frontend_meneses.xcodeproj/xcuserdata/americomq.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..0d4e4c3 --- /dev/null +++ b/frontend_meneses/frontend_meneses.xcodeproj/xcuserdata/americomq.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + frontend_meneses.xcscheme_^#shared#^_ + + orderHint + 0 + + + + diff --git a/frontend_meneses/frontend_meneses/Assets.xcassets/AccentColor.colorset/Contents.json b/frontend_meneses/frontend_meneses/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 0000000..eb87897 --- /dev/null +++ b/frontend_meneses/frontend_meneses/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/frontend_meneses/frontend_meneses/Assets.xcassets/AppIcon.appiconset/Contents.json b/frontend_meneses/frontend_meneses/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2305880 --- /dev/null +++ b/frontend_meneses/frontend_meneses/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,35 @@ +{ + "images" : [ + { + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "tinted" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/frontend_meneses/frontend_meneses/Assets.xcassets/Contents.json b/frontend_meneses/frontend_meneses/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/frontend_meneses/frontend_meneses/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/frontend_meneses/frontend_meneses/Model/Person.swift b/frontend_meneses/frontend_meneses/Model/Person.swift new file mode 100644 index 0000000..d0467a9 --- /dev/null +++ b/frontend_meneses/frontend_meneses/Model/Person.swift @@ -0,0 +1,26 @@ +// +// Person.swift +// frontend_meneses +// +// Created by Américo MQ on 27/02/26. +// + +import Foundation + +struct Person: Identifiable, Codable { + var id: Int? + var firstName: String + var lastName: String + var birthDate: String + var position: String + var salary: Double + + enum CodingKeys: String, CodingKey { + case id + case firstName = "nombre" + case lastName = "apellido" + case birthDate = "fechaNacimiento" + case position = "puesto" + case salary = "sueldo" + } +} diff --git a/frontend_meneses/frontend_meneses/Networking/NetworkManager.swift b/frontend_meneses/frontend_meneses/Networking/NetworkManager.swift new file mode 100644 index 0000000..a14fefd --- /dev/null +++ b/frontend_meneses/frontend_meneses/Networking/NetworkManager.swift @@ -0,0 +1,61 @@ +// +// NetworkManager.swift +// frontend_meneses +// +// Created by Américo MQ on 27/02/26. +// + +import Foundation + +class NetworkManager { + private let baseURL = "http://localhost:8080/person" + + func fetchAll() async throws -> [Person] { + let url = URL(string: baseURL)! + let (data, _) = try await URLSession.shared.data(from: url) + + let decoder = JSONDecoder() + decoder.dateDecodingStrategy = .formatted(dateFormatter) + + let response = try decoder.decode([Person].self, from: data) + return response + } + + func delete(id: Int) async throws { + let url = URL(string: "\(baseURL)/\(id)")! + var request = URLRequest(url: url) + request.httpMethod = "DELETE" + _ = try await URLSession.shared.data(for: request) + } + + func save(person: Person) async throws { + + let url: URL + + if let id = person.id { + url = URL(string: "\(baseURL)/\(id)")! + } else { + url = URL(string: baseURL)! + } + + var request = URLRequest(url: url) + request.httpMethod = person.id == nil ? "POST" : "PUT" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + + request.httpBody = try JSONEncoder().encode(person) + + let (_, response) = try await URLSession.shared.data(for: request) + + guard let httpResponse = response as? HTTPURLResponse, + 200..<300 ~= httpResponse.statusCode else { + throw URLError(.badServerResponse) + } + } + + private var dateFormatter: DateFormatter { + let formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd" + formatter.locale = Locale(identifier: "en_US_POSIX") + return formatter + } +} diff --git a/frontend_meneses/frontend_meneses/ViewModel/PersonViewModel.swift b/frontend_meneses/frontend_meneses/ViewModel/PersonViewModel.swift new file mode 100644 index 0000000..4c2efbf --- /dev/null +++ b/frontend_meneses/frontend_meneses/ViewModel/PersonViewModel.swift @@ -0,0 +1,31 @@ +// +// PersonViewModel.swift +// frontend_meneses +// +// Created by Américo MQ on 27/02/26. +// + +import Combine +import SwiftUI + +@MainActor +class PersonViewModel: ObservableObject { + + @Published var persons: [Person] = [] + @Published var isLoading = false + + let service = NetworkManager() + + func load() async { + isLoading = true + persons = try! await service.fetchAll() + isLoading = false + } + + func delete(at offsets: IndexSet) async { + for index in offsets { + try? await service.delete(id: persons[index].id!) + } + await load() + } +} diff --git a/frontend_meneses/frontend_meneses/Views/PersonFormView.swift b/frontend_meneses/frontend_meneses/Views/PersonFormView.swift new file mode 100644 index 0000000..cbbf660 --- /dev/null +++ b/frontend_meneses/frontend_meneses/Views/PersonFormView.swift @@ -0,0 +1,92 @@ +// +// PersonFormView.swift +// frontend_meneses +// +// Created by Américo MQ on 27/02/26. +// + +import SwiftUI + +struct PersonFormView: View { + + @Environment(\.dismiss) var dismiss + + @State var person: Person? + + @State private var firstName = "" + @State private var lastName = "" + @State private var birthDate = Date() + @State private var position = "" + @State private var salary = "" + + private let service = NetworkManager() + + var body: some View { + NavigationStack { + Form { + TextField("Nombre", text: $firstName) + TextField("Apellido", text: $lastName) + DatePicker("Fecha Nacimiento", + selection: $birthDate, + displayedComponents: .date) + TextField("Puesto", text: $position) + TextField("Sueldo", text: $salary) + .keyboardType(.decimalPad) + } + .navigationTitle(person == nil ? "Crear Persona" : "Editar Persona") + .toolbar { + ToolbarItem(placement: .confirmationAction) { + Button("Guardar") { + Task { + await save() + } + } + } + + ToolbarItem(placement: .cancellationAction) { + Button("Cancelar") { + dismiss() + } + } + } + .onAppear { + if let person = person { + firstName = person.firstName + lastName = person.lastName + let formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd" + formatter.locale = Locale(identifier: "en_US_POSIX") + if let date = formatter.date(from: person.birthDate) { + birthDate = date + } + position = person.position + salary = String(person.salary) + } + } + } + } + + func save() async { + let formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd" + formatter.locale = Locale(identifier: "en_US_POSIX") + + let fechaString = formatter.string(from: birthDate) + + let newPerson = Person( + id: person?.id, + firstName: firstName, + lastName: lastName, + birthDate: fechaString, + position: position, + salary: Double(salary) ?? 0 + ) + + try? await service.save(person: newPerson) + dismiss() + } +} + +#Preview { + PersonFormView() +} diff --git a/frontend_meneses/frontend_meneses/Views/PersonListView.swift b/frontend_meneses/frontend_meneses/Views/PersonListView.swift new file mode 100644 index 0000000..52d1f9b --- /dev/null +++ b/frontend_meneses/frontend_meneses/Views/PersonListView.swift @@ -0,0 +1,67 @@ +// +// PersonListView.swift +// frontend_meneses +// +// Created by Américo MQ on 27/02/26. +// + +import SwiftUI + +struct PersonListView: View { + @StateObject private var viewModel = PersonViewModel() + @State private var showForm = false + @State private var selectedPerson: Person? + + var body: some View { + NavigationStack { + + List { + ForEach(viewModel.persons, id: \.id) { person in + HStack { + VStack(alignment: .leading) { + Text("\(person.firstName) \(person.lastName)") + .font(.headline) + Text(person.position) + } + + Spacer() + + Button("Editar") { + selectedPerson = person + showForm = true + } + .buttonStyle(.bordered) + + Button("Borrar") { + Task { + try? await viewModel.service.delete(id: person.id!) + await viewModel.load() + } + } + .buttonStyle(.borderedProminent) + } + } + } + .navigationTitle("Personas") + .toolbar { + Button("Crear") { + selectedPerson = nil + showForm = true + } + } + .task { + await viewModel.load() + } + .sheet(isPresented: $showForm, onDismiss: { + Task { + await viewModel.load() + } + }) { + PersonFormView(person: selectedPerson) + } + } + } +} +#Preview { + PersonListView() +} diff --git a/frontend_meneses/frontend_meneses/frontend_menesesApp.swift b/frontend_meneses/frontend_meneses/frontend_menesesApp.swift new file mode 100644 index 0000000..3f79285 --- /dev/null +++ b/frontend_meneses/frontend_meneses/frontend_menesesApp.swift @@ -0,0 +1,17 @@ +// +// frontend_menesesApp.swift +// frontend_meneses +// +// Created by Américo MQ on 27/02/26. +// + +import SwiftUI + +@main +struct frontend_menesesApp: App { + var body: some Scene { + WindowGroup { + PersonListView() + } + } +}