generated from semicolondsm/Semicolon_Repository_Generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
296 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
import XDateUtil | ||
enum TimePeriod: Int { | ||
case morning = 0 | ||
case period1 = 1 | ||
case period2 = 2 | ||
case period3 = 3 | ||
case period4 = 4 | ||
case period5 = 5 | ||
case period6 = 6 | ||
case period7 = 7 | ||
case night = 8 | ||
init(date: Date) { | ||
let time = date.toString(format: .time) | ||
switch time { | ||
case "00:00:00"..<"08:40:00": | ||
self = .morning | ||
case "08:40:00"..<"09:40:00": | ||
self = .period1 | ||
case "09:40:00"..<"10:40:00": | ||
self = .period2 | ||
case "10:40:00"..<"11:40:00": | ||
self = .period3 | ||
case "11:40:00"..<"13:30:00": | ||
self = .period4 | ||
case "13:30:00"..<"14:30:00": | ||
self = .period5 | ||
case "14:30:00"..<"15:30:00": | ||
self = .period6 | ||
case "15:30:00"..<"16:30:00": | ||
self = .period7 | ||
default: | ||
self = .night | ||
} | ||
} | ||
} |
202 changes: 202 additions & 0 deletions
202
Application/Widget/Sources/TimeTable/XquareTimeTableWidget.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
import WidgetKit | ||
import SwiftUI | ||
|
||
import RxSwift | ||
import XDateUtil | ||
import TimeTableService | ||
import SemicolonDesign | ||
|
||
struct XquareTimeTableWidgetProvider: TimelineProvider { | ||
|
||
private let fetchTimeTableForWeekUseCase: FetchTimeTableForWeekUseCase | ||
private var disposeBag = DisposeBag() | ||
|
||
init() { | ||
self.fetchTimeTableForWeekUseCase = TimeTableServiceDependency.shared.fetchTimeTableForWeekUseCase | ||
} | ||
|
||
func placeholder(in context: Context) -> TimeTableEntry { | ||
return .init(weekDay: 1, date: Date(), dayTimeTable: []) | ||
} | ||
|
||
func getSnapshot(in context: Context, completion: @escaping (TimeTableEntry) -> Void) { | ||
getTimeTable { timeTable in | ||
completion(.init(weekDay: timeTable.weekDay, date: timeTable.date, dayTimeTable: timeTable.dayTimeTable)) | ||
} | ||
} | ||
|
||
func getTimeline(in context: Context, completion: @escaping (Timeline<TimeTableEntry>) -> Void) { | ||
let refreshDate = Calendar.current.date(byAdding: .minute, value: 5, to: Date())! | ||
|
||
getTimeTable { timeTable in | ||
completion(.init( | ||
entries: [.init( | ||
weekDay: timeTable.weekDay, | ||
date: timeTable.date, | ||
dayTimeTable: timeTable.dayTimeTable | ||
)], | ||
policy: .after(refreshDate)) | ||
) | ||
} | ||
} | ||
|
||
private func getTimeTable(completion: @escaping ((WeekTimeTableEntity) -> Void)) { | ||
self.fetchTimeTableForWeekUseCase.excute() | ||
.observe(on: MainScheduler.asyncInstance) | ||
.subscribe(onNext: { | ||
let timetable = $0.filter { | ||
$0.weekDay == getWeekDay() | ||
} | ||
completion(timetable.first!) | ||
}) | ||
.disposed(by: disposeBag) | ||
} | ||
private func getWeekDay() -> Int { | ||
return (Calendar.current.dateComponents([.weekday], from: Date()).weekday ?? 0) - 1 | ||
} | ||
} | ||
|
||
struct TimeTableEntry: TimelineEntry { | ||
var weekDay: Int | ||
var date: Date | ||
var dayTimeTable: [DayTimeTableEntity] | ||
} | ||
|
||
struct XquareTimeTableWidgetEntryView: View { | ||
@Environment(\.widgetFamily) var widgetFamily | ||
var entry: XquareTimeTableWidgetProvider.Entry | ||
|
||
var body: some View { | ||
widgetBody() | ||
} | ||
@ViewBuilder | ||
func widgetBody() -> some View { | ||
switch widgetFamily { | ||
case .systemSmall: | ||
SmallXquareTimeTableWidgetView(entry: entry, dayTimeTable: entry.dayTimeTable) | ||
case .accessoryRectangular: | ||
RectangularXquareTimeTableWidgetView(entry: entry, dayTimeTable: entry.dayTimeTable) | ||
default: | ||
EmptyView() | ||
} | ||
} | ||
} | ||
struct SmallXquareTimeTableWidgetView: View { | ||
@Environment(\.colorScheme) var colorScheme | ||
var entry: XquareTimeTableWidgetProvider.Entry | ||
var dayTimeTable: [DayTimeTableEntity] | ||
|
||
init(entry: XquareTimeTableWidgetProvider.Entry, dayTimeTable: [DayTimeTableEntity]) { | ||
self.entry = entry | ||
self.dayTimeTable = dayTimeTable.filter { | ||
$0.period == (TimePeriod(date: Date()).rawValue + 1) | ||
} | ||
} | ||
var body: some View { | ||
ZStack { | ||
if colorScheme == .light { | ||
Color.white.ignoresSafeArea() | ||
} else { | ||
Color.GrayScale.gray900.ignoresSafeArea() | ||
} | ||
HStack { | ||
VStack(alignment: .leading, spacing: 0) { | ||
Text(entry.date.toString(format: "EμμΌ")) | ||
.sdText( | ||
type: .body4, | ||
textColor: colorScheme == .light ? .GrayScale.gray900 : .GrayScale.gray100 | ||
) | ||
.padding(.trailing, 10) | ||
.padding(.top, 3) | ||
Text(entry.date.toString(format: .fullDate)) | ||
.sdText( | ||
type: .caption2, | ||
textColor: colorScheme == .light ? .GrayScale.gray600 : .GrayScale.gray500 | ||
) | ||
.padding(.bottom, 10) | ||
if dayTimeTable != [] { | ||
VStack(alignment: .leading, spacing: 0) { | ||
Text("\(dayTimeTable.first!.period)κ΅μ") | ||
.sdText( | ||
type: .body1, | ||
textColor: colorScheme == .light ? .GrayScale.gray900 : .GrayScale.gray100 | ||
) | ||
Text("\(dayTimeTable.first!.subjectName)") | ||
.fontWeight(.light) | ||
.foregroundColor(colorScheme == .light ? .GrayScale.gray900 : .GrayScale.gray100) | ||
.lineLimit(nil) | ||
} | ||
} else { | ||
Text("λ±λ‘λ μ λ³΄κ° μμ΅λλ€.") | ||
.sdText( | ||
type: .caption2, | ||
textColor: colorScheme == .light ? .GrayScale.gray900 : .GrayScale.gray100 | ||
) | ||
} | ||
Spacer() | ||
} | ||
.padding([.horizontal, .top], 10) | ||
Spacer() | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct RectangularXquareTimeTableWidgetView: View { | ||
@Environment(\.colorScheme) var colorScheme | ||
var entry: XquareTimeTableWidgetProvider.Entry | ||
var dayTimeTable: [DayTimeTableEntity] | ||
|
||
init(entry: XquareTimeTableWidgetProvider.Entry, dayTimeTable: [DayTimeTableEntity]) { | ||
self.entry = entry | ||
self.dayTimeTable = dayTimeTable.filter { | ||
$0.period == (TimePeriod(date: Date()).rawValue + 1) | ||
} | ||
} | ||
var body: some View { | ||
if dayTimeTable != [] { | ||
HStack { | ||
VStack(alignment: .leading, spacing: 0) { | ||
Text("\(dayTimeTable.first!.period)κ΅μ") | ||
.sdText( | ||
type: .body1, | ||
textColor: colorScheme == .light ? .GrayScale.gray900 : .GrayScale.gray100 | ||
) | ||
Text("\(dayTimeTable.first!.subjectName)") | ||
.fontWeight(.light) | ||
.foregroundColor(colorScheme == .light ? .GrayScale.gray900 : .GrayScale.gray100) | ||
} | ||
Spacer() | ||
} | ||
} else { | ||
Text("λ±λ‘λ μ λ³΄κ° μμ΅λλ€.") | ||
.sdText( | ||
type: .caption2, | ||
textColor: colorScheme == .light ? .GrayScale.gray900 : .GrayScale.gray100 | ||
) | ||
} | ||
} | ||
} | ||
|
||
struct XquareTimeTableWidget: Widget { | ||
let kind: String = "XquareTimeTableWidget" | ||
|
||
var body: some WidgetConfiguration { | ||
let configuration = StaticConfiguration( | ||
kind: kind, | ||
provider: XquareTimeTableWidgetProvider() | ||
) { entry in | ||
XquareTimeTableWidgetEntryView(entry: entry) | ||
} | ||
.configurationDisplayName("XQuare timeTable Widget") | ||
.description("Xquare μμ ―μ ν΅ν΄ μ€λ μκ°νλ₯Ό νμΈνμΈμ.") | ||
|
||
if #available(iOSApplicationExtension 16.0, *) { | ||
return configuration | ||
.supportedFamilies([.systemSmall, .accessoryRectangular]) | ||
} else { | ||
return configuration | ||
.supportedFamilies([.systemSmall]) | ||
} | ||
} | ||
} |