Skip to content

Commit 5ea10e9

Browse files
committed
feat: fixed the memory leak issue on the pasteboard
1 parent e7c40da commit 5ea10e9

1 file changed

Lines changed: 30 additions & 26 deletions

File tree

Loadify/View/URLView.swift

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct URLView: View {
1313
@State var viewModel = URLViewModel()
1414
@State private var videoURL: String = ""
1515
@State private var isConvertButtonDisabled: Bool = false
16+
@Environment(\.scenePhase) private var scenePhase
1617

1718
var body: some View {
1819
NavigationStack(path: $viewModel.path) {
@@ -29,27 +30,8 @@ struct URLView: View {
2930
}
3031
.padding()
3132
}
32-
.onAppear(perform: {
33-
setupPasteboardObserver()
34-
})
33+
// Navigation
3534
.navigationBarHidden(true)
36-
.showLoader(LoadifyTexts.loading, isPresented: $viewModel.showLoader)
37-
.onChange(of: videoURL, {
38-
withAnimation {
39-
isConvertButtonDisabled = videoURL.isEmpty
40-
}
41-
})
42-
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
43-
Task {
44-
checkPasteboard()
45-
}
46-
}
47-
.showAlert(item: $viewModel.errorMessage, content: { errorMessage -> AlertUI in
48-
guard let errorTitle = LoadifyTexts.tryAgain.randomElement() else {
49-
return AlertUI(title: errorMessage)
50-
}
51-
return AlertUI(title: errorTitle, subtitle: errorMessage)
52-
})
5335
.navigationDestination(for: LoadifyNavigationPath.self) { path in
5436
switch path {
5537
case .downloader(let response):
@@ -63,6 +45,32 @@ struct URLView: View {
6345
await didTapContinue()
6446
}
6547
}
48+
// User feedback
49+
.showAlert(item: $viewModel.errorMessage, content: { errorMessage -> AlertUI in
50+
guard let errorTitle = LoadifyTexts.tryAgain.randomElement() else {
51+
return AlertUI(title: errorMessage)
52+
}
53+
return AlertUI(title: errorTitle, subtitle: errorMessage)
54+
})
55+
.showLoader(LoadifyTexts.loading, isPresented: $viewModel.showLoader)
56+
// Lifecycle & state
57+
.onAppear {
58+
if viewModel.path.isEmpty {
59+
Task {
60+
checkPasteboard()
61+
}
62+
}
63+
}
64+
.onChange(of: videoURL, {
65+
withAnimation {
66+
isConvertButtonDisabled = videoURL.isEmpty
67+
}
68+
})
69+
.onChange(of: scenePhase, { _, newPhase in
70+
if newPhase == .active && viewModel.path.isEmpty {
71+
checkPasteboard()
72+
}
73+
})
6674
}
6775
}
6876

@@ -95,16 +103,12 @@ struct URLView: View {
95103
}
96104

97105
private func checkPasteboard() {
106+
// Only auto-fill from pasteboard if the field is empty
107+
guard videoURL.isEmpty else { return }
98108
if let pasteboardString = UIPasteboard.general.string {
99109
videoURL = pasteboardString
100110
}
101111
}
102-
103-
private func setupPasteboardObserver() {
104-
Task {
105-
checkPasteboard()
106-
}
107-
}
108112
}
109113

110114
#Preview {

0 commit comments

Comments
 (0)