Skip to content

Commit

Permalink
Merge pull request #469 from hyun99999/feature/#463
Browse files Browse the repository at this point in the history
feat: 위젯에 명함 조회 API 연결 및 구현 (#463)
  • Loading branch information
hyun99999 authored May 2, 2023
2 parents 7518430 + b6e93a9 commit c76b593
Show file tree
Hide file tree
Showing 23 changed files with 165 additions and 128 deletions.
97 changes: 86 additions & 11 deletions IntentsExtension/IntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,107 @@ import Intents

class IntentHandler: INExtension {

// MARK: - Properties

var cardItems: [Card]?

override func handler(for intent: INIntent) -> Any {
// This is the default implementation. If you want different objects to handle different intents,
// you can override this and return the handler you want for that particular intent.

cardListFetchWithAPI { [weak self] result in
switch result {
case .success(let result):
if let result {
self?.cardItems = result.data
print(self?.cardItems)
}
case .failure(let err):
print(err)
}
}

return self
}
}

extension IntentHandler: MyCardIntentHandling {
// 내 명함 목록 선택할 때 호출.
func provideMyCardOptionsCollection(for intent: MyCardIntent, with completion: @escaping (INObjectCollection<MyCard>?, Error?) -> Void) {
// TODO: - 서버 통신 혹은 DB 에서 선택 목록 가져온다.
let myCards: [MyCard] = Card.mockData.map { card in
let myCard = MyCard(identifier: card.cardUUID, display: card.cardName)

return myCard
cardListFetchWithAPI { [weak self] result in
switch result {
case .success(let result):
if let result {
self?.cardItems = result.data

if let cardItems = self?.cardItems {
let myCards = cardItems.map { card in
let myCard = MyCard(identifier: card.cardUUID, display: card.cardName)
myCard.userName = card.userName
myCard.cardImage = card.cardImage

return myCard
}
let collection = INObjectCollection(items: myCards)
completion(collection, nil)
}
}
case .failure(let err):
print(err)
}
}
let collection = INObjectCollection(items: myCards)

completion(collection, nil)
}

// 위젯 편집할때 호출. 기본값 설정.
func defaultMyCard(for intent: MyCardIntent) -> MyCard? {
// TODO: - 내 명함이 존재하면 첫 번째 명함을 기본값으로 설정. 존재하지 않다면 nil 반환.
let myCard = MyCard(identifier: Card.mockData[0].cardUUID,
display: Card.mockData[0].cardName)
var myCard: MyCard?

if let cardItems {
myCard = MyCard(identifier: cardItems[0].cardUUID, display: cardItems[0].cardName)
}

return myCard
}
}

// MARK: - Newtwork

enum WidgetError: Error {
case networkFail(status: Int, code: String, message: String)
case decodeFail(status: Int)
case error(status: Int, error: Error)
}

extension IntentHandler {
func cardListFetchWithAPI(completion: @escaping (Result<GenericResponse<[Card]>?, Error>) -> Void) {
guard let url = URL(string: "http://3.35.107.3:8080/api/v1/card") else { return }
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "GET"
urlRequest.addValue("Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjEwfQ.rq__Rzvunpxq3paeo2fK4i_oeupyDlHp3q1RW6uSHSQ", forHTTPHeaderField: "Authorization")
print("😀", UserDefaults.appGroup.string(forKey: "accessToken") ?? "")

URLSession.shared.dataTask(with: urlRequest) { data, response, error in
guard let status = (response as? HTTPURLResponse)?.statusCode else { return }

if let error = error {
completion(.failure(WidgetError.error(status: status, error: error)))
} else {
if let data {
let result = try? JSONDecoder().decode(GenericResponse<[Card]>.self, from: data)

if status != 200 {
completion(.failure(WidgetError.networkFail(status: status,
code: result?.code ?? "none code",
message: result?.message ?? "none message")))
} else {
if let result {
completion(.success(result))
} else {
completion(.failure(WidgetError.decodeFail(status: status)))
}
}
}
}
}.resume()
}
}
9 changes: 9 additions & 0 deletions NADA-iOS-forRelease.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
F8257750299546920067E145 /* Card.swift in Sources */ = {isa = PBXBuildFile; fileRef = F87122752733046300A24E74 /* Card.swift */; };
F8257751299546930067E145 /* Card.swift in Sources */ = {isa = PBXBuildFile; fileRef = F87122752733046300A24E74 /* Card.swift */; };
F8268DB927730B0100BF114B /* FirstCardAlertBottomSheetViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8268DB827730B0100BF114B /* FirstCardAlertBottomSheetViewController.swift */; };
F82AF69F29FBB3000051545B /* GenericResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8C83FBC272F9F430009DF0D /* GenericResponse.swift */; };
F82AF6A229FBBEE50051545B /* UserDefaults+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82AF6A129FBBEE50051545B /* UserDefaults+Extension.swift */; };
F82AF6A329FBC1710051545B /* UserDefaults+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82AF6A129FBBEE50051545B /* UserDefaults+Extension.swift */; };
F82FEB4C27639F3100DA7847 /* MainCardCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82FEB4A27639F3100DA7847 /* MainCardCell.swift */; };
F82FEB4D27639F3100DA7847 /* MainCardCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = F82FEB4B27639F3100DA7847 /* MainCardCell.xib */; };
F82FF81D2701EBCE00E57F8B /* CardCreation.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F82FF81C2701EBCE00E57F8B /* CardCreation.storyboard */; };
Expand Down Expand Up @@ -376,6 +379,8 @@
F81171FF27383097002742CF /* ChangeGroupRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChangeGroupRequest.swift; sourceTree = "<group>"; };
F822E7A82709CEB60020452C /* Notification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notification.swift; sourceTree = "<group>"; };
F8268DB827730B0100BF114B /* FirstCardAlertBottomSheetViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstCardAlertBottomSheetViewController.swift; sourceTree = "<group>"; };
F82AF6A029FBBAF50051545B /* IntentsExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = IntentsExtension.entitlements; sourceTree = "<group>"; };
F82AF6A129FBBEE50051545B /* UserDefaults+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UserDefaults+Extension.swift"; sourceTree = "<group>"; };
F82FEB4A27639F3100DA7847 /* MainCardCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainCardCell.swift; sourceTree = "<group>"; };
F82FEB4B27639F3100DA7847 /* MainCardCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MainCardCell.xib; sourceTree = "<group>"; };
F82FF81C2701EBCE00E57F8B /* CardCreation.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = CardCreation.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1449,6 +1454,7 @@
7734D5B72777A8E8004360E4 /* String+Extension.swift */,
F835EF6E29800F51001ABAAF /* UIApplication+Extension.swift */,
77A4D5F529BC304C00367B7C /* UIStackView+Extension.swift */,
F82AF6A129FBBEE50051545B /* UserDefaults+Extension.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -1741,8 +1747,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F82AF69F29FBB3000051545B /* GenericResponse.swift in Sources */,
F80975E92990A27400075B93 /* Widgets.intentdefinition in Sources */,
F8257751299546930067E145 /* Card.swift in Sources */,
F82AF6A329FBC1710051545B /* UserDefaults+Extension.swift in Sources */,
F87D222D298ECAFB001A882B /* IntentHandler.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -1796,6 +1804,7 @@
3979709626FA0BE5003FB4F5 /* LoginViewController.swift in Sources */,
39488E352706E17200BDC917 /* UIAlertController+Extension.swift in Sources */,
3903CC202769F4F40094C458 /* EmptyCardCell.swift in Sources */,
F82AF6A229FBBEE50051545B /* UserDefaults+Extension.swift in Sources */,
F8C310C0273A7360008EC5BA /* FrontCardCreationDelegate.swift in Sources */,
F8C83FBF272FA0670009DF0D /* UserSevice.swift in Sources */,
39F5A3D0271461EA00191F94 /* BackCardCell.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions NADA-iOS-forRelease/Resouces/Constants/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ extension Const {

static func multipartFormHeader() -> [String: String] {
["Content-Type": "application/json",
"Authorization": "Bearer \(UserDefaults.standard.string(forKey: Const.UserDefaultsKey.accessToken) ?? "")"]
"Authorization": "Bearer \(UserDefaults.appGroup.string(forKey: Const.UserDefaultsKey.accessToken) ?? "")"]
}

static func bearerHeader() -> [String: String] {
["Authorization": "Bearer \(UserDefaults.standard.string(forKey: Const.UserDefaultsKey.accessToken) ?? "")"]
["Authorization": "Bearer \(UserDefaults.appGroup.string(forKey: Const.UserDefaultsKey.accessToken) ?? "")"]
}

static func basicHeader() -> [String: String] {
["Content-Type": "application/json",
"Authorization": "Bearer \(UserDefaults.standard.string(forKey: Const.UserDefaultsKey.accessToken) ?? "")"]
"Authorization": "Bearer \(UserDefaults.appGroup.string(forKey: Const.UserDefaultsKey.accessToken) ?? "")"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// UserDefaults+Extension.swift
// NADA-iOS-forRelease
//
// Created by kimhyungyu on 2023/04/28.
//

import Foundation

extension UserDefaults {
static var appGroup = UserDefaults(suiteName: "group.NADA-iOS-forRelease")!
}
2 changes: 1 addition & 1 deletion NADA-iOS-forRelease/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

KakaoSDK.initSDK(appKey: "5b8dd8cc878344bb7532eeca4365a4aa")

let acToken = UserDefaults.standard.string(forKey: Const.UserDefaultsKey.accessToken)
let acToken = UserDefaults.appGroup.string(forKey: Const.UserDefaultsKey.accessToken)

if acToken != nil {
if UserDefaults.standard.bool(forKey: Const.UserDefaultsKey.isAppleLogin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class GroupAPI {
public init() { }

func cardDeleteInGroup(cardUUID: String, cardGroupName: String, completion: @escaping (NetworkResult<Any>) -> Void) {
groupProvider.request(.cardDeleteInGroup(cardUUID: cardUUID, cardGroupName: cardGroupName)) { (result) in
groupProvider.request(.cardDeleteInGroup(cardUuid: cardUUID, cardGroupName: cardGroupName)) { (result) in
switch result {
case .success(let response):
let statusCode = response.statusCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ extension LoginViewController {
print("postUserSignUpWithAPI - success")
if let userData = loginData as? AccessToken {
UserDefaults.standard.set(socialID, forKey: Const.UserDefaultsKey.userID)
UserDefaults.standard.set(userData.accessToken, forKey: Const.UserDefaultsKey.accessToken)
UserDefaults.appGroup.set(userData.accessToken, forKey: Const.UserDefaultsKey.accessToken)
// UserDefaults.standard.set(userData.user.token.refreshToken, forKey: Const.UserDefaultsKey.refreshToken)
self.presentToMain()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ extension MoreViewController {
func setLogoutClicked() {
makeOKCancelAlert(title: "", message: "로그아웃 하시겠습니까?", okAction: { _ in
self.makeOKAlert(title: "", message: "로그아웃이 완료 되었습니다.") { _ in
if let acToken = UserDefaults.standard.string(forKey: Const.UserDefaultsKey.accessToken) {
self.defaults.removeObject(forKey: Const.UserDefaultsKey.accessToken)
if let acToken = UserDefaults.appGroup.string(forKey: Const.UserDefaultsKey.accessToken) {
UserDefaults.appGroup.removeObject(forKey: Const.UserDefaultsKey.accessToken)
// self.defaults.removeObject(forKey: Const.UserDefaultsKey.refreshToken)
self.defaults.removeObject(forKey: Const.UserDefaultsKey.darkModeState)

Expand All @@ -137,7 +137,7 @@ extension MoreViewController {
makeOKCancelAlert(title: "", message: "정말 탈퇴하시겠습니까?\n앱 내 정보가 모두 삭제되며, 이후 복구는 불가합니다.", okAction: { [weak self ]_ in
self?.deleteUserWithAPI {
self?.makeOKAlert(title: "", message: "나다를 이용해주셔서 감사합니다.\n다음에 또 뵈어요! 🥹") { _ in
self?.defaults.removeObject(forKey: Const.UserDefaultsKey.accessToken)
UserDefaults.appGroup.removeObject(forKey: Const.UserDefaultsKey.accessToken)
// self.defaults.removeObject(forKey: Const.UserDefaultsKey.refreshToken)
self?.defaults.removeObject(forKey: Const.UserDefaultsKey.darkModeState)

Expand Down

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
36 changes: 32 additions & 4 deletions Widgets/Resource/Widgets.intentdefinition
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
<key>INIntentDefinitionNamespace</key>
<string>88xZPY</string>
<key>INIntentDefinitionSystemVersion</key>
<string>22C65</string>
<string>22D68</string>
<key>INIntentDefinitionToolsBuildVersion</key>
<string>14C18</string>
<string>14E222b</string>
<key>INIntentDefinitionToolsVersion</key>
<string>14.2</string>
<string>14.3</string>
<key>INIntents</key>
<array>
<dict>
Expand Down Expand Up @@ -103,7 +103,7 @@
<key>INTypeDisplayNameID</key>
<string>J1deG2</string>
<key>INTypeLastPropertyTag</key>
<integer>101</integer>
<integer>103</integer>
<key>INTypeName</key>
<string>MyCard</string>
<key>INTypeProperties</key>
Expand Down Expand Up @@ -158,6 +158,34 @@
<key>INTypePropertyType</key>
<string>SpeakableString</string>
</dict>
<dict>
<key>INTypePropertyDisplayName</key>
<string>User Name</string>
<key>INTypePropertyDisplayNameID</key>
<string>oPOynL</string>
<key>INTypePropertyDisplayPriority</key>
<integer>5</integer>
<key>INTypePropertyName</key>
<string>userName</string>
<key>INTypePropertyTag</key>
<integer>102</integer>
<key>INTypePropertyType</key>
<string>String</string>
</dict>
<dict>
<key>INTypePropertyDisplayName</key>
<string>Card Image</string>
<key>INTypePropertyDisplayNameID</key>
<string>QzTUbw</string>
<key>INTypePropertyDisplayPriority</key>
<integer>6</integer>
<key>INTypePropertyName</key>
<string>cardImage</string>
<key>INTypePropertyTag</key>
<integer>103</integer>
<key>INTypePropertyType</key>
<string>String</string>
</dict>
</array>
</dict>
</array>
Expand Down
Loading

0 comments on commit c76b593

Please sign in to comment.