ios – Swift : FSCalendar and TableView Cell

[ad_1]

We’re devloping an app which permits one to work with FSCalendar and Realm + TableView.
We have succeeded in storing dates and issues to do. However, the issue is that, once we choose a date, it reveals not solely the issues to do but in addition all lists saved in Realm.
I believe we must always use didSelect(monthPosition) in FSCalendar. However, I’m a novice right here. Please assist me!

TableView : cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "TodoCell", for: indexPath) as? TodoCell else {
        return UITableViewCell()
    }
    let todo = checklist[indexPath.row]
    cell.configure(todo)
    
    cell.checkClosure = { [weak self] _ in
        guard let self = self else { return }
        let merchandise = self.checklist[indexPath.row]
        DataBaseManager.shared.updateData(id: merchandise.id, merchandise.dateCreated, merchandise.todocontent, !merchandise.isComplete)
    }
    
    cell.titleClosure = { [weak self, todo] _ in
        guard let uvc = AddController.getAddViewController(todo: todo) else { return }
        self?.current(uvc, animated: true)
    }
    
    return cell
}

FSCalendar : didSelect, monthPosition

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
    let didSelectDate = calendar.look.todayColor
    if didSelectDate == .black {
        calendar.look.todayColor = .white
        calendar.look.titleTodayColor = .black
    }
    let selectedDates = calendar.selectedDates.map({self.dateFormatter.string(from: $0)})
    print("chosen dates is (selectedDates)")

    if monthPosition == .subsequent || monthPosition == .earlier {
        calendar.setCurrentPage(date, animated: true)
    }
}

ListDataModel(Mannequin)

    import Basis
    import RealmSwift

ultimate class TodoList: Object, Identifiable {
    @Continued(primaryKey: true) var id: UUID
    @Continued var dateCreated: String
    @Continued var todocontent: String
    @Continued var isComplete: Bool
    @Continued var order: Int
}

DataBaseManager

import Basis

import RealmSwift

ultimate class DataBaseManager {

public static let shared = DataBaseManager()

non-public var notificationToken: NotificationToken?

var replace: (() -> Void)?
lazy var todoList: Outcomes<TodoList> = {
    return DataBaseManager.shared.loadAllData() 
}()

func loadAllData() -> Outcomes<TodoList> {
    do {
        let config = Realm.Configuration(
            schemaVersion: 1,
            migrationBlock: { migration, oldSchemaVersion in
                print(">>> oldSchemaVersion = (oldSchemaVersion)")
            }
        )
        
        Realm.Configuration.defaultConfiguration = config
        
        let realm = strive Realm()
        let todoList = realm.objects(TodoList.self).sorted(byKeyPath: "order", ascending: true)
        
        self.notificationToken = todoList.observe { [unowned self] adjustments in
            change adjustments {
            case .preliminary(let customers):
                print("Preliminary depend: (customers.depend)")
                self.replace?()
            case .replace(_, _, _, _):
                self.replace?()
            case .error(let error):
                fatalError("(error)")
            }
        }
        return todoList
        
        
    } catch let error {
        fatalError("save error (error)")
    }
}

@discardableResult
func saveData(id: UUID? = nil, _ dateCreated: String, _ todocontent: String, _ isComplete: Bool) -> TodoList {
    do {
        let realm = strive Realm()
        
        let merchandise: TodoList = TodoList()
        merchandise.id = id ?? UUID()
        merchandise.dateCreated = dateCreated
        merchandise.todocontent = todocontent
        merchandise.isComplete = isComplete
        merchandise.order = self.todoList.depend
        
        strive realm.write {
            if id == nil {
                realm.add(merchandise)
            } else {
                realm.add(merchandise, replace: .modified)
            }
        }
        return merchandise
    } catch let error {
        print("save error (error)")
        return TodoList()
    }
}

@discardableResult
func updateData(id: UUID, _ dateCreated: String, _ todocontent: String, _ isComplete: Bool) -> TodoList? {
    do {
        let realm = strive Realm()
        let merchandise = realm.object(ofType: TodoList.self, forPrimaryKey: id)
        
        strive realm.write {
            merchandise?.dateCreated = dateCreated
            merchandise?.todocontent = todocontent
            merchandise?.isComplete = isComplete
        }

        return merchandise
    } catch let error {
        print("replace error (error)")
        print("--------------(Realm.Configuration.defaultConfiguration.fileURL!)----------------")
        return TodoList()
    }
}

@discardableResult
func deleteData(id: UUID) -> Bool {
    do {
        let realm = strive Realm()
        guard let merchandise = realm.object(ofType: TodoList.self, forPrimaryKey: id) else { return false }
        
        strive realm.write {
            realm.delete(merchandise)
            
            for merchandise in self.todoList {
                merchandise.order -= 1
            }
        }
        return true
    } catch let error {
        print("delete error (error)")
        return false
    }
}

@discardableResult
func moveData(supply: Int, vacation spot: Int) -> Bool {
    do {
        let realm = strive Realm()
        let gadgets = realm.objects(TodoList.self).sorted { $0.order < $1.order }

        let sourceItem = gadgets[source]
        let destinationItem = gadgets[destination]
        
        let sourceOrder = sourceItem.order
        let destinationOrder = destinationItem.order
        
        strive realm.write {
            sourceItem.order = destinationOrder
            destinationItem.order = sourceOrder
        }
        return true
    } catch let error {
        print("delete error (error)")
        return false
    }
}

}

[ad_2]

Leave a Reply