-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] #252 - 명언 api 통신 후 entry 업데이트
- URLSession을 활용한 서버통신 - 위젯에 보여질 TimeEntry 생성 - View 내의 명언 Text 크기 및 정렬 수정 - Widget_NOTTODOEntryView에서 Provider.entry 사용으로 변경
- Loading branch information
Showing
6 changed files
with
142 additions
and
50 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
iOS-NOTTODO/Widget-NOTTODO/Network/Service/WidgetService.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,64 @@ | ||
// | ||
// WidgetService.swift | ||
// iOS-NOTTODO | ||
// | ||
// Created by 강윤서 on 5/10/24. | ||
// | ||
|
||
import Foundation | ||
|
||
typealias QuoteData = GeneralResponse<QuoteResponseDTO> | ||
|
||
struct WidgetService { | ||
|
||
static let shared = WidgetService() | ||
private init() {} | ||
|
||
private let session = URLSession.shared | ||
|
||
func fetchWiseSaying() async throws -> QuoteResponseDTO { | ||
do { | ||
return try await getWiseSaying(from: generateURL()) | ||
} catch { | ||
switch error { | ||
case NetworkError.networkError: | ||
print("네트워크 에러가 발생") | ||
case NetworkError.invalidResponse: | ||
print("유효하지 않은 응답") | ||
case NetworkError.dataParsingError: | ||
print("데이터 파싱 실패") | ||
default: | ||
print("알 수 없는 에러 발생") | ||
} | ||
throw error | ||
} | ||
} | ||
|
||
private func getWiseSaying(from url: URL) async throws -> QuoteResponseDTO { | ||
let (data, response) = try await session.data(from: url) | ||
|
||
guard let httpResponse = response as? HTTPURLResponse else { | ||
throw NetworkError.networkError | ||
} | ||
|
||
guard (200..<300).contains(httpResponse.statusCode) else { | ||
throw NetworkError.invalidResponse | ||
} | ||
|
||
do { | ||
let result = try JSONDecoder().decode(QuoteData.self, from: data) | ||
guard let responseData = result.data else { | ||
throw NetworkError.dataParsingError | ||
} | ||
return responseData | ||
} catch { | ||
throw NetworkError.dataParsingError | ||
} | ||
} | ||
|
||
private func generateURL() -> URL { | ||
let baseURL = Bundle.main.baseURL | ||
let url = baseURL + URLConstant.quote | ||
return URL(string: url)! | ||
} | ||
} |
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,46 @@ | ||
// | ||
// MissionProvider.swift | ||
// iOS-NOTTODO | ||
// | ||
// Created by 강윤서 on 5/11/24. | ||
// | ||
|
||
import SwiftUI | ||
import WidgetKit | ||
|
||
struct Provider: AppIntentTimelineProvider { | ||
@AppStorage("dailyMission", store: UserDefaults.shared) var sharedData: Data = Data() | ||
|
||
func placeholder(in context: Context) -> SimpleEntry { | ||
SimpleEntry(todayMission: [], quote: "") | ||
} | ||
|
||
func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> SimpleEntry { | ||
do { | ||
let entry = try await getTimelineEntry() | ||
return entry | ||
} catch { | ||
return SimpleEntry(todayMission: [], quote: "") | ||
} | ||
} | ||
|
||
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<SimpleEntry> { | ||
do { | ||
let entry = try await getTimelineEntry() | ||
return Timeline(entries: [entry], policy: .never) | ||
} catch { | ||
return Timeline(entries: [], policy: .never) | ||
} | ||
|
||
} | ||
|
||
private func getTimelineEntry() async throws -> SimpleEntry { | ||
guard let decodedData = try? JSONDecoder().decode([DailyMissionResponseDTO].self, from: sharedData) else { | ||
return SimpleEntry(todayMission: [], quote: "") | ||
} | ||
|
||
let quoteResponse = try await WidgetService.shared.fetchWiseSaying() | ||
let quote = quoteResponse.description + " - " + quoteResponse.author | ||
return SimpleEntry(todayMission: decodedData, quote: quote) | ||
} | ||
} |
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,14 @@ | ||
// | ||
// TimeEntity.swift | ||
// iOS-NOTTODO | ||
// | ||
// Created by 강윤서 on 5/11/24. | ||
// | ||
|
||
import WidgetKit | ||
|
||
struct SimpleEntry: TimelineEntry { | ||
var date: Date = .now | ||
var todayMission: [DailyMissionResponseDTO] | ||
let quote: String | ||
} |
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