Skip to content

Commit

Permalink
feat: add widget usecase(depromeet#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
akrudal committed Jun 7, 2024
1 parent 9087a47 commit 735a744
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
// Created by geonhui Yu on 12/11/23.
//

import Foundation

struct Family: Codable {
var authorName: String
var authorProfileImageUrl: String?
var postId: String?
var postImageUrl: String?
var postContent: String?
}
//import Foundation
//
//struct Family: Codable {
// var authorName: String
// var authorProfileImageUrl: String?
// var postId: String?
// var postImageUrl: String?
// var postContent: String?
//}
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,51 @@
//
// Created by geonhui Yu on 12/11/23.
//

import Foundation
import UIKit
import Core

struct FamilyService {
func fetchInfo(completion: @escaping (Result<Family?, Error>) -> Void) {

let token = App.Repository.token.keychain.string(forKey: "accessToken")
let appKey = "7c5aaa36-570e-491f-b18a-26a1a0b72959"
#if PRD
var hostApi: String = "https://api.no5ing.kr/v1"
#else
var hostApi: String = "https://dev.api.no5ing.kr/v1"
#endif
let urlString = "\(hostApi)/widgets/single-recent-family-post"

var request = URLRequest(url: URL(string: urlString)!)
request.addValue("application/json", forHTTPHeaderField: "accept")
request.addValue(token ?? "", forHTTPHeaderField: "X-AUTH-TOKEN")
request.addValue(appKey, forHTTPHeaderField: "X-APP-KEY")
request.httpMethod = "GET"

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
completion(.failure(error))
return
}

guard let data = data else {
let noDataError = NSError(domain: "NoDataError", code: 0, userInfo: nil)
completion(.failure(noDataError))
return
}

do {
let decoder = JSONDecoder()
let family = try decoder.decode(Family.self, from: data)
completion(.success(family))
} catch {
completion(.failure(error))
}
}
task.resume()
}
}


//
//import Foundation
//import UIKit
//import Core
//
//struct FamilyService {
// func fetchInfo(completion: @escaping (Result<Family?, Error>) -> Void) {
//
// let token = App.Repository.token.keychain.string(forKey: "accessToken")
// let appKey = "7c5aaa36-570e-491f-b18a-26a1a0b72959"
//#if PRD
// var hostApi: String = "https://api.no5ing.kr/v1"
//#else
// var hostApi: String = "https://dev.api.no5ing.kr/v1"
//#endif
// let urlString = "\(hostApi)/widgets/single-recent-family-post"
//
// var request = URLRequest(url: URL(string: urlString)!)
// request.addValue("application/json", forHTTPHeaderField: "accept")
// request.addValue(token ?? "", forHTTPHeaderField: "X-AUTH-TOKEN")
// request.addValue(appKey, forHTTPHeaderField: "X-APP-KEY")
// request.httpMethod = "GET"
//
// let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
// if let error = error {
// completion(.failure(error))
// return
// }
//
// guard let data = data else {
// let noDataError = NSError(domain: "NoDataError", code: 0, userInfo: nil)
// completion(.failure(noDataError))
// return
// }
//
// do {
// let decoder = JSONDecoder()
// let family = try decoder.decode(Family.self, from: data)
// completion(.success(family))
// } catch {
// completion(.failure(error))
// }
// }
// task.resume()
// }
//}
//
//
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct FamilyWidget: Widget {
let kind: String = "FamilyWidget"

var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: FamilyWidgetTimelineProvider()) { entry in
StaticConfiguration(kind: kind, provider: FamilyWidgetDIContainer().makeProvider()) { entry in
if #available(iOSApplicationExtension 17.0, *) {
FamilyWidgetView(entry: entry)
.containerBackground(for: .widget) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// FamilyWidgetDIContainer.swift
// WidgetExtension
//
// Created by 마경미 on 05.06.24.
//

import Foundation

import Core
import Data
import Domain

final class FamilyWidgetDIContainer {

func makeRepository() -> WidgetRepositoryProtocol {
return WidgetRepository()
}

func makeUseCase() -> FetchRecentFamilyPostUseCaseProtocol {
return FetchRecentFamilyPostUseCase(widgetRepository: makeRepository())
}

func makeProvider() -> FamilyWidgetTimelineProvider {
return .init(repository: makeRepository())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
//

import WidgetKit
import Domain

struct FamilyWidgetEntry: TimelineEntry {
let date: Date
let family: Family?
let family: RecentFamilyPostData?
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
import WidgetKit
import UIKit

struct FamilyWidgetTimelineProvider: TimelineProvider {
import Domain

final class FamilyWidgetTimelineProvider: TimelineProvider {
typealias Entry = FamilyWidgetEntry

let repository: WidgetRepositoryProtocol

init(repository: WidgetRepositoryProtocol) {
self.repository = repository
}

func placeholder(in context: Context) -> FamilyWidgetEntry {
return FamilyWidgetEntry(date: Date(), family: nil)
}
Expand All @@ -21,7 +29,8 @@ struct FamilyWidgetTimelineProvider: TimelineProvider {
let currentDate = Date()
let refreshDate = Calendar.current.date(byAdding: .minute, value: 5, to: currentDate)!

FamilyService().fetchInfo { result in

repository.fetchRecentFamilyPost { result in
var entry: FamilyWidgetEntry
switch result {
case .success(let family):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI
import DesignSystem
import WidgetKit
import Core
import Domain

struct FamilyWidgetView: View {
@Environment(\.widgetFamily) var family: WidgetFamily
Expand Down Expand Up @@ -152,7 +153,7 @@ struct FamilyWidgetView: View {
}

// MARK: 가족중 일부가 사진을 올렸을 때 뷰
private func getPhotoView(info: Family) -> some View {
private func getPhotoView(info: RecentFamilyPostData) -> some View {
ZStack {
if let postImageUrl = info.postImageUrl {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// WidgetRepository.swift
// Data
//
// Created by 마경미 on 05.06.24.
//

import Foundation

import Domain

import RxSwift

public final class WidgetRepository: WidgetRepositoryProtocol {

public let disposeBag: DisposeBag = DisposeBag()
private let widgetAPIWorker: WidgetAPIWorker = WidgetAPIWorker()

public init () { }

public func fetchRecentFamilyPost(completion: @escaping (Result<Domain.RecentFamilyPostData?, Error>) -> Void) {
widgetAPIWorker.fetchRecentFamilyPost()
.subscribe(
onNext: { result in
completion(.success(result))
},
onError: { error in
completion(.failure(error))
}
)
.disposed(by: disposeBag)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// RecentFamilyPostRequestDTO.swift
// Data
//
// Created by 마경미 on 05.06.24.
//

import Foundation

struct RecentFamilyPostParameter: Codable {
let date: String = Date().toFormatString(with: "yyyy-MM-dd")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// RecentFamilyPostResponseDTO.swift
// Data
//
// Created by 마경미 on 05.06.24.
//

import Foundation

import Domain

struct RecentFamilyPostResponseDTO: Codable {
let authorName: String
let authorProfileImageUrl: String?
let postId: String?
let postImageUrl: String?
let postContent: String?

func toDomain() -> RecentFamilyPostData {
return .init(authorName: authorName,
authorProfileImageUrl: authorProfileImageUrl,
postId: postId,
postImageUrl: postImageUrl,
postContent: postContent)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// WidgetAPIWorker.swift
// Data
//
// Created by 마경미 on 05.06.24.
//

import Foundation

import Core
import Domain

import RxSwift

typealias WidgetAPIWorker = WidgetAPIs.Worker
extension WidgetAPIs {
public final class Worker: APIWorker {
static let queue = {
ConcurrentDispatchQueueScheduler(queue: DispatchQueue(label: "WidgetAPIQueue", qos: .utility))
}()

public override init() {
super.init()
self.id = "WidgetAPIWorker"
}

var headers: [APIHeader] {
guard let token = App.Repository.token.keychain.string(forKey: "accessToken") else {
return []
}

let headers = [BibbiAPI.Header.xAppKey, BibbiAPI.Header.xAuthToken(token), BibbiAPI.Header.acceptJson]
return headers
}
}
}

extension WidgetAPIWorker {
func fetchRecentFamilyPost() -> Observable<RecentFamilyPostData?> {

let spec = WidgetAPIs.fetchRecentFamilyPost.spec
let parameters = RecentFamilyPostParameter()

return request(spec: spec, headers: headers, parameters: parameters)
.subscribe(on: Self.queue)
.do {
if let str = String(data: $0.1, encoding: .utf8) {
debugPrint("Fetch Recent Family Post Result: \(str)")
}
}
.map(RecentFamilyPostResponseDTO.self)
.catchAndReturn(nil)
.map { $0?.toDomain() }
.asObservable()
}
}
22 changes: 22 additions & 0 deletions 14th-team5-iOS/Data/Sources/APIs/Widget/WidgetAPI/WidgetAPIs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// WidgetAPIs.swift
// Data
//
// Created by 마경미 on 05.06.24.
//

import Foundation

import Core

enum WidgetAPIs: API {
case fetchRecentFamilyPost

public var spec: APISpec {
switch self {
case .fetchRecentFamilyPost:
let urlString = "\(BibbiAPI.hostApi)/widgets/single-recent-family-post"
return APISpec(method: .get, url: urlString)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// RecentFamilyPostData.swift
// Domain
//
// Created by 마경미 on 05.06.24.
//

import Foundation

public struct RecentFamilyPostData: Codable {
public var authorName: String
public var authorProfileImageUrl: String?
public var postId: String?
public var postImageUrl: String?
public var postContent: String?

public init(
authorName: String,
authorProfileImageUrl: String? = nil,
postId: String? = nil,
postImageUrl: String? = nil,
postContent: String? = nil) {
self.authorName = authorName
self.authorProfileImageUrl = authorProfileImageUrl
self.postId = postId
self.postImageUrl = postImageUrl
self.postContent = postContent
}
}
Loading

0 comments on commit 735a744

Please sign in to comment.