SwiftUI: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Line 66: Line 66:


public struct MyKDCalendar: UIViewRepresentable {
public struct MyKDCalendar: UIViewRepresentable {
     var date: Date = .init()
     @State var date:Date = .init()
 
   
     public func makeUIView(context: Context) -> CalendarView {
     public func makeUIView(context: Context) -> CalendarView {
         let calendar: CalendarView = .init()
         let calendar: CalendarView = .init()
         calendar.setDisplayDate(self.date, animated: false)
        calendar.direction = .horizontal
         calendar.selectDate(self.date)
        calendar.style = self.getStyle()
        calendar.marksWeekends = true
       
         calendar.setDisplayDate(date, animated: false)
         calendar.selectDate(date)
         calendar.dataSource = self
         calendar.dataSource = self
         calendar.delegate = self
         calendar.delegate = self
         return calendar;
//      calendar.addEvent(<#T##title: String##String#>, date: <#T##Date#>)
//      calendar.addEvent(<#T##title: String##String#>, date: <#T##Date#>, duration: <#T##NSInteger#>)
         return calendar
     }
     }
 
   
     public func updateUIView(_ calendar: CalendarView, context: Context) {
     public func updateUIView(_ calendar: CalendarView, context: Context) {
         calendar.selectDate(self.date)
         calendar.selectDate(date)
     }
     }
}
}
Line 86: Line 92:
         return Date.init()
         return Date.init()
     }
     }
 
   
     public func endDate() -> Date {
     public func endDate() -> Date {
         var month:DateComponents = .init()
         var byAdding = DateComponents()
         month.month = 2
         byAdding.month = 3
         return Calendar.current.date(byAdding: month, to: Date.init())!
         return Calendar.current.date(byAdding: byAdding, to: Date.init())!
     }
     }
 
   
     public func headerString(_ date: Date) -> String? {
     public func headerString(_ date: Date) -> String? {
         return nil
         return nil
    }
   
    private func getStyle(_ primary: UIColor = ThemeColor.midnightBlue.uicolor, _ secondary: UIColor = ThemeColor.sharpOrange.uicolor, _ tersiary: UIColor = ThemeColor.whitishBlue.uicolor) -> CalendarView.Style {
        let style: CalendarView.Style = .init()
       
        style.cellSelectedBorderColor = secondary
        style.cellSelectedTextColor = tersiary
        style.cellSelectedColor = secondary
        style.firstWeekday = .sunday
       
        style.weekdaysBackgroundColor = primary
        style.weekdaysTextColor = tersiary
       
        style.headerBackgroundColor = primary
        style.headerTextColor = tersiary
       
        style.cellTextColorWeekend = secondary
        style.cellTextColorDefault = primary
        style.cellTextColorToday = tersiary
       
        style.cellColorOutOfRange = .systemGray
        style.cellColorDefault = tersiary
        style.cellColorToday = primary
       
        return style
     }
     }
}
}
Line 103: Line 134:
     public func calendar(_ calendar: CalendarView, didSelectDate date: Date, withEvents events: [CalendarEvent]) {}
     public func calendar(_ calendar: CalendarView, didSelectDate date: Date, withEvents events: [CalendarEvent]) {}
     public func calendar(_ calendar: CalendarView, didLongPressDate date: Date, withEvents events: [CalendarEvent]?) {}
     public func calendar(_ calendar: CalendarView, didLongPressDate date: Date, withEvents events: [CalendarEvent]?) {}
     public func calendar(_ calendar: CalendarView, canSelectDate date: Date) -> Bool {
     public func calendar(_ calendar: CalendarView, canSelectDate date: Date) -> Bool {
         return true
         return true

Revision as of 23:33, 16 May 2020

ContentView

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack(alignment: .leading) {
            Text("Turtle Rock")
                .font(.title)
            HStack {
                Text("Joshua Tree National Park")
                    .font(.subheadline)
                Spacer()
                Text("California")
                    .font(.subheadline)
            }
        }
        .padding()
    }
}

struct ContentView_Preview: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

MyPDFView

// MARK: - MyPDFView
fileprivate struct MyPDFView: UIViewRepresentable {
    @Binding var data: Data

    func makeUIView(context: UIViewRepresentableContext<MyPDFView>) -> UIView {
        let empty = UIView()
      //guard let provider = CGDataProvider(data: self.data as CFData) else {return empty}
      //guard let document = CGPDFDocument(provider) else {return empty}
        guard let url = URL(string: "https://api.chorke.org/academia/api/v1.0/health/card/01890600.pdf") else {return empty}
        guard let document = CGPDFDocument(url as CFURL) else {return empty}
        guard let page     = document.page(at: 1) else {return empty}
        let pageRect = page.getBoxRect(.mediaBox)
        let renderer = UIGraphicsImageRenderer(size: pageRect.size)
        let uiImage  = renderer.image { ctx in
            UIColor.white.set()
            ctx.fill(pageRect)
            ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
            ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
            ctx.cgContext.drawPDFPage(page)
        }
        let view = UIImageView(image: uiImage)
        view.contentMode = .scaleAspectFill
        return view
    }

    func updateUIView(_ view: UIView, context: UIViewRepresentableContext<MyPDFView>) {
        // MARK: - TODO
    }
}

MyKDCalendar

import SwiftUI
import KDCalendar

public struct MyKDCalendar: UIViewRepresentable {
    @State var date:Date = .init()
    
    public func makeUIView(context: Context) -> CalendarView {
        let calendar: CalendarView = .init()
        calendar.direction = .horizontal
        calendar.style = self.getStyle()
        calendar.marksWeekends = true
        
        calendar.setDisplayDate(date, animated: false)
        calendar.selectDate(date)
        calendar.dataSource = self
        calendar.delegate = self
//      calendar.addEvent(<#T##title: String##String#>, date: <#T##Date#>)
//      calendar.addEvent(<#T##title: String##String#>, date: <#T##Date#>, duration: <#T##NSInteger#>)
        return calendar
    }
    
    public func updateUIView(_ calendar: CalendarView, context: Context) {
        calendar.selectDate(date)
    }
}

extension MyKDCalendar: CalendarViewDataSource {
    public func startDate() -> Date {
        return Date.init()
    }
    
    public func endDate() -> Date {
        var byAdding = DateComponents()
        byAdding.month = 3
        return Calendar.current.date(byAdding: byAdding, to: Date.init())!
    }
    
    public func headerString(_ date: Date) -> String? {
        return nil
    }
    
    private func getStyle(_ primary: UIColor = ThemeColor.midnightBlue.uicolor, _ secondary: UIColor = ThemeColor.sharpOrange.uicolor, _ tersiary: UIColor = ThemeColor.whitishBlue.uicolor) -> CalendarView.Style {
        let style: CalendarView.Style = .init()
        
        style.cellSelectedBorderColor = secondary
        style.cellSelectedTextColor = tersiary
        style.cellSelectedColor = secondary
        style.firstWeekday = .sunday
        
        style.weekdaysBackgroundColor = primary
        style.weekdaysTextColor = tersiary
        
        style.headerBackgroundColor = primary
        style.headerTextColor = tersiary
        
        style.cellTextColorWeekend = secondary
        style.cellTextColorDefault = primary
        style.cellTextColorToday = tersiary
        
        style.cellColorOutOfRange = .systemGray
        style.cellColorDefault = tersiary
        style.cellColorToday = primary
        
        return style
    }
}

extension MyKDCalendar: CalendarViewDelegate {
    public func calendar(_ calendar: CalendarView, didDeselectDate date: Date) {}
    public func calendar(_ calendar: CalendarView, didScrollToMonth date: Date) {}
    public func calendar(_ calendar: CalendarView, didSelectDate date: Date, withEvents events: [CalendarEvent]) {}
    public func calendar(_ calendar: CalendarView, didLongPressDate date: Date, withEvents events: [CalendarEvent]?) {}
    public func calendar(_ calendar: CalendarView, canSelectDate date: Date) -> Bool {
        return true
    }
}

MyFSCalendar

import SwiftUI
import FSCalendar

struct MyFSCalendar: UIViewControllerRepresentable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<MyFSCalendar>) -> MyFSCalendarController {
        let calendar: MyFSCalendarController = .init()
        return calendar
    }

    func updateUIViewController(_ calendar: MyFSCalendarController, context: UIViewControllerRepresentableContext<MyFSCalendar>) {
        // MARK: - TODO
    }
}
import UIKit
import FSCalendar

class MyFSCalendarController: UIViewController, FSCalendarDelegateAppearance {
    let secondary: UIColor = ThemeColor.sharpOrange.uicolor
    let primary:  UIColor = ThemeColor.midnightBlue.uicolor
    let tersiary: UIColor = ThemeColor.whitishBlue.uicolor
    
    fileprivate let formatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()
    
    fileprivate weak var calendar: FSCalendar!
    
    override func loadView() {
        let width: CGFloat = ThemeSettings.width - 40
        let frame: CGRect  = .init(x: 0, y: 0, width: width, height: 300)
        let view:  UIView  = .init(frame: frame)
        self.view = view
        
        let calendar: FSCalendar = .init(frame: frame)
        calendar.allowsMultipleSelection = false
        calendar.dataSource = self
        calendar.delegate = self
        
        view.addSubview(calendar)
        self.calendar = calendar
        
        calendar.calendarHeaderView.backgroundColor = self.primary
        calendar.calendarWeekdayView.backgroundColor = self.primary
        calendar.appearance.headerTitleColor = self.tersiary
        calendar.appearance.weekdayTextColor = self.tersiary
        
        calendar.appearance.eventSelectionColor = self.tersiary
        calendar.appearance.eventDefaultColor = self.primary
        calendar.appearance.eventOffset = CGPoint(x: 0, y: -7)
        
        calendar.appearance.todaySelectionColor = self.primary
        calendar.appearance.selectionColor = self.secondary
        calendar.appearance.todayColor = self.primary
        
        calendar.appearance.titleWeekendColor = self.secondary
        calendar.appearance.titleDefaultColor = self.primary

        calendar.swipeToChooseGesture.isEnabled = true
        let scopeGesture = UIPanGestureRecognizer(target: calendar, action: #selector(calendar.handleScopeGesture(_:)));
        scopeGesture.delegate = self
        calendar.addGestureRecognizer(scopeGesture)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.calendar.scope = .month
        self.calendar.select(Date.init())
        self.calendar.accessibilityIdentifier = "calendar"
    }
}

extension MyFSCalendarController: FSCalendarDataSource {
    func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
        debugPrint("did select date \(self.formatter.string(from: date))")
        let selectedDates = calendar.selectedDates.map({self.formatter.string(from: $0)})
        debugPrint("selected dates is \(selectedDates)")
        if monthPosition == .next || monthPosition == .previous {
            calendar.setCurrentPage(date, animated: true)
        }
    }
}

extension MyFSCalendarController: FSCalendarDelegate {
    func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
        self.calendar.frame.size.height = bounds.height
        self.view.layoutIfNeeded()
    }
}

extension MyFSCalendarController: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        debugPrint("UIGestureRecognizer")
        return true
    }
}

References