Skip to content

Commit

Permalink
[feat][#29] network 연결 확인 및 상태별 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
dayo2n committed Mar 4, 2024
1 parent 1bba44c commit 8e4a768
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
4 changes: 4 additions & 0 deletions DontForget.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
AD147F322B57FFB500561846 /* AppIntent.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD147F312B57FFB500561846 /* AppIntent.swift */; };
AD147F342B57FFB600561846 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AD147F332B57FFB600561846 /* Assets.xcassets */; };
AD147F382B57FFB600561846 /* DontForgetWidgetExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = AD147F262B57FFB500561846 /* DontForgetWidgetExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
AD1D31F92B9589A700ECCBFD /* NetworkMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD1D31F82B9589A700ECCBFD /* NetworkMonitor.swift */; };
AD36D44B2B8DD37D00250705 /* DeletionRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD36D44A2B8DD37D00250705 /* DeletionRepository.swift */; };
AD36D44D2B8DD39500250705 /* DeletionInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD36D44C2B8DD39500250705 /* DeletionInterface.swift */; };
AD3B45852B68B634009529DE /* AnniversaryDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD3B45842B68B634009529DE /* AnniversaryDetailView.swift */; };
Expand Down Expand Up @@ -152,6 +153,7 @@
AD147F312B57FFB500561846 /* AppIntent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppIntent.swift; sourceTree = "<group>"; };
AD147F332B57FFB600561846 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
AD147F352B57FFB600561846 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
AD1D31F82B9589A700ECCBFD /* NetworkMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkMonitor.swift; sourceTree = "<group>"; };
AD36D44A2B8DD37D00250705 /* DeletionRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeletionRepository.swift; sourceTree = "<group>"; };
AD36D44C2B8DD39500250705 /* DeletionInterface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeletionInterface.swift; sourceTree = "<group>"; };
AD3B45842B68B634009529DE /* AnniversaryDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnniversaryDetailView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -251,6 +253,7 @@
children = (
09642C3A2B649B260015220E /* EndPoint.swift */,
09642C3C2B649B2D0015220E /* APIConstant.swift */,
AD1D31F82B9589A700ECCBFD /* NetworkMonitor.swift */,
);
path = Network;
sourceTree = "<group>";
Expand Down Expand Up @@ -756,6 +759,7 @@
09642C462B649FD00015220E /* DTO.swift in Sources */,
AD0F99332B7C523200E2A915 /* AlarmView.swift in Sources */,
09642C582B67C8730015220E /* ViewModelType.swift in Sources */,
AD1D31F92B9589A700ECCBFD /* NetworkMonitor.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
24 changes: 24 additions & 0 deletions DontForget/Sources/Data/Network/NetworkMonitor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// NetworkMonitor.swift
// DontForget
//
// Created by 제나 on 3/4/24.
//

import Foundation
import Network

@Observable
final class NetworkMonitor: ObservableObject {
static let shared = NetworkMonitor()
private let networkMonitor = NWPathMonitor()
private let workerQueue = DispatchQueue(label: "Monitor")
var isConnected = false

init() {
networkMonitor.pathUpdateHandler = { path in
self.isConnected = path.status == .satisfied
}
networkMonitor.start(queue: workerQueue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ struct AnniversaryDetailView: View {
coordinateSpace: .local
)
.onEnded({ value in
if value.translation.width > 200 {
if value.translation.width > 100 {
withAnimation {
dismiss()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class DefaultAnniversaryDetailViewModel: ViewModelType {
// MARK: - Properties
let anniversaryId: Int
private var cancellables = Set<AnyCancellable>()
@Published var state: State
@Published var state: State = .idle
@Published var anniversaryDetail: AnniversaryDetailDTO?
private let anniversaryDetailRepository: AnniversaryDetailRepository
private let deletionRepository: DeletionRepository
Expand Down Expand Up @@ -44,7 +44,6 @@ final class DefaultAnniversaryDetailViewModel: ViewModelType {
anniversaryDetailRepository: AnniversaryDetailRepository,
deletionRepository: DeletionRepository
) {
self.state = .loading
self.anniversaryId = anniversaryId
self.anniversaryDetailRepository = anniversaryDetailRepository
self.fetchAnniversaryDetailUseCase = DefaultFetchAnniversaryDetailUseCase(
Expand Down Expand Up @@ -82,15 +81,13 @@ final class DefaultAnniversaryDetailViewModel: ViewModelType {
} catch {
print("=== DEBUG: \(error)")
promise(.failure(error))
self.state = .failed("failed fetchAnniversaryDetail()")
}
}
}
.receive(on: DispatchQueue.main)
.sink { _ in } receiveValue: { [weak self] response in
if let response = response {
self?.anniversaryDetail = response.anniversaryDetail
self?.state = .success
}
}
.store(in: &cancellables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct HomeView: View {
@State private var navigateToCreationView = false
@State private var isNavigate = false
@State private var id = -1
private var networkConnected: Bool { NetworkMonitor.shared.isConnected }

var body: some View {
NavigationView {
Expand Down Expand Up @@ -81,7 +82,8 @@ struct HomeView: View {
}
.clipShape(RoundedRectangle(cornerRadius: 32))
.onTapGesture {
if let firstAnniversaryDetail = viewModel.firstAnniversaryDetail {
if let firstAnniversaryDetail = viewModel.firstAnniversaryDetail,
networkConnected {
id = firstAnniversaryDetail.anniversaryId
isNavigate = true
}
Expand Down Expand Up @@ -109,9 +111,11 @@ struct HomeView: View {
.simultaneousGesture(
TapGesture()
.onEnded({
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
id = anniversaries[index].anniversaryId
isNavigate = true
if networkConnected {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
id = anniversaries[index].anniversaryId
isNavigate = true
}
}
})
)
Expand All @@ -129,10 +133,7 @@ struct HomeView: View {
#endif
}
.offset(y: anniversaries.isEmpty ? 0 : -140)
.onAppear {
viewModel.action(.readAnniversaries)
viewModel.action(.changePushState)
}
.onAppear(perform: actionOnAppear)
.id(Self.scrollTopView)
}
.scrollDisabled(anniversaries.isEmpty)
Expand All @@ -150,6 +151,20 @@ struct HomeView: View {
}
}
}
.onChange(of: networkConnected) { _, status in
if status {
actionOnAppear()
}
}
.toolbar {
if !networkConnected {
ToolbarItem(placement: .status) {
Text("네트워크 연결이 없어요")
.font(.pretendard(size: 15))
.foregroundStyle(.white)
}
}
}
}
}
}
Expand Down Expand Up @@ -204,5 +219,11 @@ extension HomeView {
isActive: $navigateToCreationView,
label: { AddNewAnniversaryView() }
)
.disabled(!networkConnected)
}

private func actionOnAppear() {
viewModel.action(.readAnniversaries)
viewModel.action(.changePushState)
}
}

0 comments on commit 8e4a768

Please sign in to comment.