diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..a3f44b64 Binary files /dev/null and b/.DS_Store differ diff --git a/Todoey.xcodeproj/project.pbxproj b/Todoey.xcodeproj/project.pbxproj index 423123ef..5bb1a6d2 100644 --- a/Todoey.xcodeproj/project.pbxproj +++ b/Todoey.xcodeproj/project.pbxproj @@ -3,21 +3,43 @@ archiveVersion = 1; classes = { }; - objectVersion = 50; + objectVersion = 54; objects = { /* Begin PBXBuildFile section */ + 5F377D972D304EAE00632B27 /* CategoryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F377D962D304EAE00632B27 /* CategoryViewController.swift */; }; + 5FEC83D22D36D2E60068DC66 /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 5FEC83D12D36D2E60068DC66 /* RealmSwift */; }; + 5FEC83D62D36E2100068DC66 /* Item.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FEC83D52D36E2100068DC66 /* Item.swift */; }; + 5FEC83D82D36E21A0068DC66 /* Category.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FEC83D72D36E21A0068DC66 /* Category.swift */; }; + 5FEC83DB2D3AD9180068DC66 /* Chameleon in Frameworks */ = {isa = PBXBuildFile; productRef = 5FEC83DA2D3AD9180068DC66 /* Chameleon */; }; + 5FEC83DD2D3AD9180068DC66 /* ChameleonSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 5FEC83DC2D3AD9180068DC66 /* ChameleonSwift */; }; EB2BE4FA239524DB00FB933B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB2BE4F9239524DB00FB933B /* AppDelegate.swift */; }; - EB2BE4FE239524DB00FB933B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB2BE4FD239524DB00FB933B /* ViewController.swift */; }; + EB2BE4FE239524DB00FB933B /* TodoListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB2BE4FD239524DB00FB933B /* TodoListViewController.swift */; }; EB2BE501239524DB00FB933B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EB2BE4FF239524DB00FB933B /* Main.storyboard */; }; EB2BE503239524DC00FB933B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = EB2BE502239524DC00FB933B /* Assets.xcassets */; }; EB2BE506239524DC00FB933B /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EB2BE504239524DC00FB933B /* LaunchScreen.storyboard */; }; /* End PBXBuildFile section */ +/* Begin PBXCopyFilesBuildPhase section */ + 5FEC83CA2D36D01F0068DC66 /* Embed Frameworks */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + ); + name = "Embed Frameworks"; + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + /* Begin PBXFileReference section */ + 5F377D962D304EAE00632B27 /* CategoryViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryViewController.swift; sourceTree = ""; }; + 5FEC83D52D36E2100068DC66 /* Item.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Item.swift; sourceTree = ""; }; + 5FEC83D72D36E21A0068DC66 /* Category.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Category.swift; sourceTree = ""; }; EB2BE4F6239524DB00FB933B /* Todoey.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Todoey.app; sourceTree = BUILT_PRODUCTS_DIR; }; EB2BE4F9239524DB00FB933B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - EB2BE4FD239524DB00FB933B /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + EB2BE4FD239524DB00FB933B /* TodoListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoListViewController.swift; sourceTree = ""; }; EB2BE500239524DB00FB933B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; EB2BE502239524DC00FB933B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; EB2BE505239524DC00FB933B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; @@ -29,17 +51,63 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 5FEC83D22D36D2E60068DC66 /* RealmSwift in Frameworks */, + 5FEC83DB2D3AD9180068DC66 /* Chameleon in Frameworks */, + 5FEC83DD2D3AD9180068DC66 /* ChameleonSwift in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 5F072C512CEFD88B00019815 /* Controller */ = { + isa = PBXGroup; + children = ( + 5F377D962D304EAE00632B27 /* CategoryViewController.swift */, + EB2BE4FD239524DB00FB933B /* TodoListViewController.swift */, + ); + path = Controller; + sourceTree = ""; + }; + 5F072C522CEFD90500019815 /* View */ = { + isa = PBXGroup; + children = ( + EB2BE4FF239524DB00FB933B /* Main.storyboard */, + ); + path = View; + sourceTree = ""; + }; + 5F072C532CEFD92500019815 /* Supporting Files */ = { + isa = PBXGroup; + children = ( + EB2BE502239524DC00FB933B /* Assets.xcassets */, + EB2BE504239524DC00FB933B /* LaunchScreen.storyboard */, + ); + path = "Supporting Files"; + sourceTree = ""; + }; + 5F7C43722CEFD84D00701C14 /* Model */ = { + isa = PBXGroup; + children = ( + 5FEC83D72D36E21A0068DC66 /* Category.swift */, + 5FEC83D52D36E2100068DC66 /* Item.swift */, + ); + path = Model; + sourceTree = ""; + }; + 5FEC83CC2D36D1850068DC66 /* Frameworks */ = { + isa = PBXGroup; + children = ( + ); + name = Frameworks; + sourceTree = ""; + }; EB2BE4ED239524DB00FB933B = { isa = PBXGroup; children = ( EB2BE4F8239524DB00FB933B /* Todoey */, EB2BE4F7239524DB00FB933B /* Products */, + 5FEC83CC2D36D1850068DC66 /* Frameworks */, ); sourceTree = ""; }; @@ -55,10 +123,10 @@ isa = PBXGroup; children = ( EB2BE4F9239524DB00FB933B /* AppDelegate.swift */, - EB2BE4FD239524DB00FB933B /* ViewController.swift */, - EB2BE4FF239524DB00FB933B /* Main.storyboard */, - EB2BE502239524DC00FB933B /* Assets.xcassets */, - EB2BE504239524DC00FB933B /* LaunchScreen.storyboard */, + 5F7C43722CEFD84D00701C14 /* Model */, + 5F072C522CEFD90500019815 /* View */, + 5F072C512CEFD88B00019815 /* Controller */, + 5F072C532CEFD92500019815 /* Supporting Files */, EB2BE507239524DC00FB933B /* Info.plist */, ); path = Todoey; @@ -74,12 +142,18 @@ EB2BE4F2239524DB00FB933B /* Sources */, EB2BE4F3239524DB00FB933B /* Frameworks */, EB2BE4F4239524DB00FB933B /* Resources */, + 5FEC83CA2D36D01F0068DC66 /* Embed Frameworks */, ); buildRules = ( ); dependencies = ( ); name = Todoey; + packageProductDependencies = ( + 5FEC83D12D36D2E60068DC66 /* RealmSwift */, + 5FEC83DA2D3AD9180068DC66 /* Chameleon */, + 5FEC83DC2D3AD9180068DC66 /* ChameleonSwift */, + ); productName = Todoey; productReference = EB2BE4F6239524DB00FB933B /* Todoey.app */; productType = "com.apple.product-type.application"; @@ -108,6 +182,10 @@ Base, ); mainGroup = EB2BE4ED239524DB00FB933B; + packageReferences = ( + 5FEC83D02D36D2E60068DC66 /* XCRemoteSwiftPackageReference "realm-swift" */, + 5FEC83D92D3AD9180068DC66 /* XCRemoteSwiftPackageReference "Chameleon" */, + ); productRefGroup = EB2BE4F7239524DB00FB933B /* Products */; projectDirPath = ""; projectRoot = ""; @@ -135,8 +213,11 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - EB2BE4FE239524DB00FB933B /* ViewController.swift in Sources */, + 5FEC83D62D36E2100068DC66 /* Item.swift in Sources */, + 5F377D972D304EAE00632B27 /* CategoryViewController.swift in Sources */, + EB2BE4FE239524DB00FB933B /* TodoListViewController.swift in Sources */, EB2BE4FA239524DB00FB933B /* AppDelegate.swift in Sources */, + 5FEC83D82D36E21A0068DC66 /* Category.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -281,14 +362,16 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = TN4525L258; INFOPLIST_FILE = Todoey/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 13.0; + IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); PRODUCT_BUNDLE_IDENTIFIER = "com.londonappbrewery.todoey-ios13.Todoey"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_REFLECTION_METADATA_LEVEL = all; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; @@ -299,14 +382,16 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = TN4525L258; INFOPLIST_FILE = Todoey/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 13.0; + IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); PRODUCT_BUNDLE_IDENTIFIER = "com.londonappbrewery.todoey-ios13.Todoey"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_REFLECTION_METADATA_LEVEL = all; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; @@ -334,6 +419,43 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ + +/* Begin XCRemoteSwiftPackageReference section */ + 5FEC83D02D36D2E60068DC66 /* XCRemoteSwiftPackageReference "realm-swift" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/realm/realm-swift.git"; + requirement = { + branch = master; + kind = branch; + }; + }; + 5FEC83D92D3AD9180068DC66 /* XCRemoteSwiftPackageReference "Chameleon" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/dwsdolce/Chameleon"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 2.2.1; + }; + }; +/* End XCRemoteSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + 5FEC83D12D36D2E60068DC66 /* RealmSwift */ = { + isa = XCSwiftPackageProductDependency; + package = 5FEC83D02D36D2E60068DC66 /* XCRemoteSwiftPackageReference "realm-swift" */; + productName = RealmSwift; + }; + 5FEC83DA2D3AD9180068DC66 /* Chameleon */ = { + isa = XCSwiftPackageProductDependency; + package = 5FEC83D92D3AD9180068DC66 /* XCRemoteSwiftPackageReference "Chameleon" */; + productName = Chameleon; + }; + 5FEC83DC2D3AD9180068DC66 /* ChameleonSwift */ = { + isa = XCSwiftPackageProductDependency; + package = 5FEC83D92D3AD9180068DC66 /* XCRemoteSwiftPackageReference "Chameleon" */; + productName = ChameleonSwift; + }; +/* End XCSwiftPackageProductDependency section */ }; rootObject = EB2BE4EE239524DB00FB933B /* Project object */; } diff --git a/Todoey.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Todoey.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 2e5e05f2..919434a6 100644 --- a/Todoey.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ b/Todoey.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -2,6 +2,6 @@ + location = "self:"> diff --git a/Todoey.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/Todoey.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 00000000..0c67376e --- /dev/null +++ b/Todoey.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,5 @@ + + + + + diff --git a/Todoey.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Todoey.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 00000000..1e9ea11b --- /dev/null +++ b/Todoey.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -0,0 +1,33 @@ +{ + "originHash" : "f20af25adf705f95489f8888bb2a87a8355da69c24e7bd346d0f7ccf60dbc9bb", + "pins" : [ + { + "identity" : "chameleon", + "kind" : "remoteSourceControl", + "location" : "https://github.com/dwsdolce/Chameleon", + "state" : { + "revision" : "916fb01bec2b8c7b7a274ba16be4341ee73b74f8", + "version" : "2.2.1" + } + }, + { + "identity" : "realm-core", + "kind" : "remoteSourceControl", + "location" : "https://github.com/realm/realm-core.git", + "state" : { + "revision" : "85eeca41654cc9070ad81a21a4b1d153ad467c33", + "version" : "14.13.1" + } + }, + { + "identity" : "realm-swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/realm/realm-swift.git", + "state" : { + "branch" : "master", + "revision" : "1f09fb14dd9d275b5686016fe8508d491b87cb0b" + } + } + ], + "version" : 3 +} diff --git a/Todoey.xcodeproj/project.xcworkspace/xcuserdata/migue.xcuserdatad/UserInterfaceState.xcuserstate b/Todoey.xcodeproj/project.xcworkspace/xcuserdata/migue.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 00000000..7bcd6e2d Binary files /dev/null and b/Todoey.xcodeproj/project.xcworkspace/xcuserdata/migue.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Todoey.xcodeproj/project.xcworkspace/xcuserdata/migue.xcuserdatad/WorkspaceSettings.xcsettings b/Todoey.xcodeproj/project.xcworkspace/xcuserdata/migue.xcuserdatad/WorkspaceSettings.xcsettings new file mode 100644 index 00000000..bbfef027 --- /dev/null +++ b/Todoey.xcodeproj/project.xcworkspace/xcuserdata/migue.xcuserdatad/WorkspaceSettings.xcsettings @@ -0,0 +1,14 @@ + + + + + BuildLocationStyle + UseAppPreferences + CustomBuildLocationType + RelativeToDerivedData + DerivedDataLocationStyle + Default + ShowSharedSchemesAutomaticallyEnabled + + + diff --git a/Todoey.xcodeproj/xcuserdata/migue.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Todoey.xcodeproj/xcuserdata/migue.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 00000000..48911b82 --- /dev/null +++ b/Todoey.xcodeproj/xcuserdata/migue.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,6 @@ + + + diff --git a/Todoey.xcodeproj/xcuserdata/migue.xcuserdatad/xcschemes/xcschememanagement.plist b/Todoey.xcodeproj/xcuserdata/migue.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 00000000..931ff4eb --- /dev/null +++ b/Todoey.xcodeproj/xcuserdata/migue.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,77 @@ + + + + + SchemeUserState + + GettingStarted (Playground) 1.xcscheme + + isShown + + orderHint + 2 + + GettingStarted (Playground) 2.xcscheme + + isShown + + orderHint + 3 + + GettingStarted (Playground) 3.xcscheme + + isShown + + orderHint + 4 + + GettingStarted (Playground) 4.xcscheme + + isShown + + orderHint + 5 + + GettingStarted (Playground) 5.xcscheme + + isShown + + orderHint + 6 + + GettingStarted (Playground) 6.xcscheme + + isShown + + orderHint + 7 + + GettingStarted (Playground) 7.xcscheme + + isShown + + orderHint + 8 + + GettingStarted (Playground) 8.xcscheme + + isShown + + orderHint + 9 + + GettingStarted (Playground).xcscheme + + isShown + + orderHint + 0 + + Todoey.xcscheme_^#shared#^_ + + orderHint + 0 + + + + diff --git a/Todoey/AppDelegate.swift b/Todoey/AppDelegate.swift index 9fbce18a..d4917660 100644 --- a/Todoey/AppDelegate.swift +++ b/Todoey/AppDelegate.swift @@ -7,40 +7,43 @@ // import UIKit +import RealmSwift @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - + var window: UIWindow? - - + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { - // Override point for customization after application launch. + +// print(Realm.Configuration.defaultConfiguration.fileURL) + migrate() + do { + _ = try Realm() + } catch { + print("Error initialising with Realm, \(error)") + } + return true } - - func applicationWillResignActive(_ application: UIApplication) { - // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. - // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. - } - - func applicationDidEnterBackground(_ application: UIApplication) { - // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. - // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. - } - - func applicationWillEnterForeground(_ application: UIApplication) { - // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. + + func migrate() { + let config = Realm.Configuration( + // Set the new schema version. This must be greater than the previously used + // version (if you've never set a schema version before, the version is 0). + schemaVersion: 1, + + // Set the block which will be called automatically when opening a Realm with + // a schema version lower than the one set above + migrationBlock: { migration, oldSchemaVersion in + + if oldSchemaVersion < 1 { + migration.enumerateObjects(ofType: Item.className(), { oldObject, newObject in + newObject?["dateCreated"] = Date() + }) + } + } + ) + Realm.Configuration.defaultConfiguration = config } - - func applicationDidBecomeActive(_ application: UIApplication) { - // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. - } - - func applicationWillTerminate(_ application: UIApplication) { - // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. - } - - } - diff --git a/Todoey/Base.lproj/Main.storyboard b/Todoey/Base.lproj/Main.storyboard deleted file mode 100644 index 25a76385..00000000 --- a/Todoey/Base.lproj/Main.storyboard +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Todoey/Controller/CategoryViewController.swift b/Todoey/Controller/CategoryViewController.swift new file mode 100644 index 00000000..087b4d27 --- /dev/null +++ b/Todoey/Controller/CategoryViewController.swift @@ -0,0 +1,159 @@ +// +// CategoryViewController.swift +// Todoey +// +// Created by Miguel Angel Reyes Sánchez on 09/01/25. +// Copyright © 2025 App Brewery. All rights reserved. +// + +import UIKit +import RealmSwift +import ChameleonSwift + +class CategoryViewController: UITableViewController { + + lazy var realm = try! Realm() + + var categories: Results? + + override func viewDidLoad() { + super.viewDidLoad() + loadCategories() + } + + override func viewWillAppear(_ animated: Bool) { + guard let navBar = navigationController?.navigationBar else { + fatalError("Navigation Controller does not exist yet.") + } + navBar.backgroundColor = FlatNavyBlueDark().lighten(byPercentage: 0.45) + navBar.tintColor = ContrastColorOf(navBar.backgroundColor!, returnFlat: true) + } + + //MARK: - TableView Datasource Methods + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return categories?.count ?? 1 + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath) + let categoryName = categories?[indexPath.row].name ?? "No Categories added yet" + + if let category = categories?[indexPath.row] { + if #available(iOS 14.0, *) { + var content = cell.defaultContentConfiguration() + content.text = categoryName + + guard let cellColor = UIColor(hexString: category.color) else { + fatalError("Could not load the cell color") + } + + cell.backgroundColor = cellColor + content.textProperties.color = ContrastColorOf(cellColor, returnFlat: true) + + cell.contentConfiguration = content + } else { + // Fallback on earlier versions + cell.textLabel?.text = categoryName + } + } + return cell + } + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + + self.performSegue(withIdentifier: "goToItems", sender: self) + + tableView.deselectRow(at: indexPath, animated: true) + } + + override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { + if editingStyle == .delete { + if let category = categories?[indexPath.row] { + do { + try realm.write { + realm.delete(category) + tableView.deleteRows(at: [indexPath], with: .fade) + } + } catch { + print("Error deleting category, \(error)") + } + } + self.tableView.reloadData() + } + } + +// override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?{ +// let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { [self](action, sourceView, completionHandler) in +// if let deleteCategory = categories?[indexPath.row]{ +// do { +// try realm.write { +// realm.delete(deleteCategory) +// tableView.deleteRows(at: [indexPath], with: .automatic) +// } +// } catch { +// fatalError("Faild to delete Category from Realm") +// } +// completionHandler(true) +// tableView.reloadData() +// } +// } +// let actions = UISwipeActionsConfiguration(actions: [deleteAction]) +// return actions +// } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + if segue.identifier == "goToItems" { + let destinationVC = segue.destination as! TodoListViewController + + if let indexPath = tableView.indexPathForSelectedRow { + destinationVC.selectedCategory = categories?[indexPath.row] + } + } + } + + //MARK: - Data Manipulation Methods + func loadCategories(){ + categories = realm.objects(Category.self) + + self.tableView.reloadData() + } + + func save(category: Category){ + do { + try realm.write { + realm.add(category) + } + } catch { + print("Error saving data from context, \(error)") + } + self.tableView.reloadData() + } + + //MARK: - Add New Categories + @IBAction func addButtonPressed(_ sender: UIBarButtonItem) { + var alertTextField = UITextField() + + let alert = UIAlertController(title: "Add new List", message: "", preferredStyle: .alert) + + let action = UIAlertAction(title: "Add List", style: .default) { UIAlertAction in + if alertTextField.text != "" { + let newCategory = Category() + newCategory.name = alertTextField.text! + newCategory.color = RandomFlatColorWithShade(.light).hexValue() + + self.save(category: newCategory) + print("List added succesfully") + } + } + + alert.addTextField { UITextField in + alertTextField = UITextField + alertTextField.placeholder = "Name the new list" + } + + alert.addAction(action) + present(alert, animated: true, completion: nil) + } + + //MARK: - TableView Delegate Methods +} diff --git a/Todoey/Controller/TodoListViewController.swift b/Todoey/Controller/TodoListViewController.swift new file mode 100644 index 00000000..1b0df5ff --- /dev/null +++ b/Todoey/Controller/TodoListViewController.swift @@ -0,0 +1,209 @@ +// +// ViewController.swift +// Todoey +// +// Created by Philipp Muellauer on 02/12/2019. +// Copyright © 2019 App Brewery. All rights reserved. +// + +import UIKit +import RealmSwift +import ChameleonSwift + +class TodoListViewController: UITableViewController { + + @IBOutlet weak var searchBar: UISearchBar! + + let realm = try! Realm() + + var toDoItems: Results? + + var selectedCategory: Category? { + didSet{ + loadItems() + } + } + + override func viewDidLoad() { + super.viewDidLoad() + } + + override func viewWillAppear(_ animated: Bool) { + if let colorHex = selectedCategory?.color { + title = selectedCategory!.name + + guard let navBar = navigationController?.navigationBar else { fatalError("Navigation Controller does not exist yet.")} + if let navBarColor = UIColor(hexString: colorHex) { + + let contrastColor = ContrastColorOf(navBarColor, returnFlat: true) + navBar.barTintColor = navBarColor + navBar.backgroundColor = navBarColor + + navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: contrastColor] + navBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: contrastColor] + +// navBar.standardAppearance.backgroundColor = navBarColor +// navBar.standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: contrastColor] +// navBar.scrollEdgeAppearance?.backgroundColor = navBarColor + + navBar.tintColor = contrastColor + + searchBar.barTintColor = navBarColor + searchBar.searchTextField.backgroundColor = FlatWhite() + } + } + } + + //MARK: - UITableView Datasource + + // Return the number of rows for the table + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return toDoItems?.count ?? 1 + } + + // Provide a cell object for each row. + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + + let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath) + + if let item = toDoItems?[indexPath.row] { + if #available(iOS 14.0, *) { + var content = cell.defaultContentConfiguration() + content.text = item.title + + if let color = UIColor(hexString: selectedCategory!.color)?.darken(byPercentage: + CGFloat(indexPath.row) * 0.3 / CGFloat(toDoItems!.count)) { + cell.backgroundColor = color + content.textProperties.color = ContrastColorOf(color, returnFlat: true) + } + + cell.contentConfiguration = content + } + else { + cell.textLabel?.text = item.title + } + + // value = condition ? valueIfTrue: valueIfFalse + cell.accessoryType = item.done ? .checkmark : .none + } else { + cell.textLabel?.text = "No Items Added yet" + } + + return cell + } + + //MARK: - UITableView Delegate Methods + + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + + if let item = toDoItems?[indexPath.row] { + do { + try realm.write { +// item.done = !item.done + item.done.toggle() + } + } catch { + print("Error saving done status, \(error)") + } + } + self.tableView.reloadData() + + tableView.deselectRow(at: indexPath, animated: true) + } + + override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { + if editingStyle == .delete { + if let item = toDoItems?[indexPath.row] { + do { + try realm.write { + realm.delete(item) + tableView.deleteRows(at: [indexPath], with: .fade) + } + } catch { + print("Error deleting item, \(error)") + } + } + } + self.tableView.reloadData() + } + + //MARK: - Add New Items + + @IBAction func addButtonPressed(_ sender: UIBarButtonItem) { + + var textfield = UITextField() + + let alert = UIAlertController(title: "Add new Todoey item", message: "", preferredStyle: .alert) + + let action = UIAlertAction(title: "Add Item", style: .default) { action in + if textfield.text != "" { + if let currentCategory = self.selectedCategory { + do { + try self.realm.write { + let newItem = Item() + newItem.title = textfield.text! + newItem.dateCreated = Date() + + currentCategory.items.append(newItem) + } + } catch { + print("Error saving new item, \(error)") + } + } + } + self.tableView.reloadData() + } + + alert.addTextField { (alertTextfield) in + alertTextfield.placeholder = "Create new item" + textfield = alertTextfield + } + + alert.addAction(action) + present(alert, animated: true, completion: nil) + } + + //MARK: - Model Manipulation Methods + func loadItems() { + toDoItems = realm.objects(Item.self) + toDoItems = selectedCategory?.items.sorted(byKeyPath: "title", ascending: true) + tableView.reloadData() + } +} + +//MARK: - UISearchBar Delegate Methods +extension TodoListViewController: UISearchBarDelegate { + + func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { +// let items = realm.objects(Item.self) +// let filterItems = items.where { +// $0.title.contains(searchBar.text!) +// } + if searchBar.text?.count == 0 { + loadItems() + print("loading items") + } else { + toDoItems = toDoItems?.filter("title CONTAINS[cd] %@", searchBar.text!).sorted(byKeyPath: "dateCreated", ascending: true) + self.tableView.reloadData() + print("filtering...") + } + } + + func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { + searchBar.resignFirstResponder() + searchBar.text = "" + loadItems() + } + + func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { + searchBarSearchButtonClicked(searchBar) + } + + func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { + searchBar.showsCancelButton = true + } + + func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { + searchBar.showsCancelButton = false + } +} diff --git a/Todoey/Model/Category.swift b/Todoey/Model/Category.swift new file mode 100644 index 00000000..439ca566 --- /dev/null +++ b/Todoey/Model/Category.swift @@ -0,0 +1,17 @@ +// +// Category.swift +// Todoey +// +// Created by Miguel Angel Reyes Sánchez on 14/01/25. +// Copyright © 2025 App Brewery. All rights reserved. +// + +import Foundation +import RealmSwift +import ChameleonSwift + +class Category: Object { + @Persisted var name: String = "" + @Persisted var items = List() + @Persisted var color: String = FlatWhite().hexValue() +} diff --git a/Todoey/Model/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents b/Todoey/Model/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents new file mode 100644 index 00000000..b21e83c5 --- /dev/null +++ b/Todoey/Model/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Todoey/Model/Item.swift b/Todoey/Model/Item.swift new file mode 100644 index 00000000..ca0234a2 --- /dev/null +++ b/Todoey/Model/Item.swift @@ -0,0 +1,17 @@ +// +// Item.swift +// Todoey +// +// Created by Miguel Angel Reyes Sánchez on 14/01/25. +// Copyright © 2025 App Brewery. All rights reserved. +// + +import Foundation +import RealmSwift + +class Item: Object { + @Persisted var title: String = "" + @Persisted var done: Bool = false + @Persisted var dateCreated: Date? + @Persisted var parentCategory = LinkingObjects(fromType: Category.self, property: "items") +} diff --git a/Todoey/Assets.xcassets/AppIcon.appiconset/Contents.json b/Todoey/Supporting Files/Assets.xcassets/AppIcon.appiconset/Contents.json similarity index 100% rename from Todoey/Assets.xcassets/AppIcon.appiconset/Contents.json rename to Todoey/Supporting Files/Assets.xcassets/AppIcon.appiconset/Contents.json diff --git a/Todoey/Assets.xcassets/Contents.json b/Todoey/Supporting Files/Assets.xcassets/Contents.json similarity index 100% rename from Todoey/Assets.xcassets/Contents.json rename to Todoey/Supporting Files/Assets.xcassets/Contents.json diff --git a/Todoey/Base.lproj/LaunchScreen.storyboard b/Todoey/Supporting Files/Base.lproj/LaunchScreen.storyboard similarity index 100% rename from Todoey/Base.lproj/LaunchScreen.storyboard rename to Todoey/Supporting Files/Base.lproj/LaunchScreen.storyboard diff --git a/Todoey/View/Base.lproj/Main.storyboard b/Todoey/View/Base.lproj/Main.storyboard new file mode 100644 index 00000000..7466dc62 --- /dev/null +++ b/Todoey/View/Base.lproj/Main.storyboard @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Todoey/ViewController.swift b/Todoey/ViewController.swift deleted file mode 100644 index 4a475a34..00000000 --- a/Todoey/ViewController.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// ViewController.swift -// Todoey -// -// Created by Philipp Muellauer on 02/12/2019. -// Copyright © 2019 App Brewery. All rights reserved. -// - -import UIKit - -class ViewController: UIViewController { - - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view. - } - - -} -