Skip to content

Commit

Permalink
Merge pull request #68 from payan-app/story-error
Browse files Browse the repository at this point in the history
feat: show error when story couldn't be loaded
  • Loading branch information
juandahurt authored Aug 25, 2022
2 parents 145c4e9 + 263a3c8 commit fe01fab
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Payan.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@
repositoryURL = "https://github.com/juandahurt/Purace";
requirement = {
kind = exactVersion;
version = 1.13.0;
version = 1.14.0;
};
};
2FD54483287236770098468D /* XCRemoteSwiftPackageReference "swift-focuser" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@
"version": "2.30908.0"
}
},
{
"package": "PopupView",
"repositoryURL": "https://github.com/exyte/PopupView.git",
"state": {
"branch": null,
"revision": "a25e22b46d47558b1e7e049b5734522c6706600c",
"version": "1.1.5"
}
},
{
"package": "Promises",
"repositoryURL": "https://github.com/google/promises.git",
Expand All @@ -114,8 +123,8 @@
"repositoryURL": "https://github.com/juandahurt/Purace",
"state": {
"branch": null,
"revision": "cdc64206813f28d1be1522c102a45c4ad70e5fab",
"version": "1.13.0"
"revision": "14aff73d550fe8d5c03227a7db9dfb179689ab52",
"version": "1.14.0"
}
},
{
Expand Down
Binary file not shown.
21 changes: 14 additions & 7 deletions Payan/App/Domain Modules/Feed/UI/Page/PYFeedPageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,18 @@ struct PYFeedPageView: View {
var body: some View {
Group {
if viewModel.isLoading {
loader
VStack {
Spacer()

loader

Spacer()
}.snackBar(title: viewModel.errorMessage, isVisible: $viewModel.feedErrorOccurred, type: .error, buttonTitle: "REINTENTAR")
.onChange(of: viewModel.feedErrorOccurred) { value in
if !value {
viewModel.getData()
}
}
} else {
ZStack {
VStack {
Expand All @@ -202,6 +213,8 @@ struct PYFeedPageView: View {
PYSearchCoreBuilder().build(isVisible: $viewModel.isSearchVisible)
}
}.transition(.opacity.animation(.linear(duration: 0.2)))
.snackBar(title: viewModel.errorMessage, isVisible: $viewModel.storyErrorOccurred, type: .error, duration: .short, dismissOnDrag: true)
.offset(x: 0, y: -0.5)
}
}
.background(Color.white)
Expand All @@ -210,12 +223,6 @@ struct PYFeedPageView: View {
viewModel.updateSeenStories()
}
.navigationBarHidden(true)
.snackBar(title: "Parece que ha ocurrido un error", isVisible: $viewModel.errorOccurred, type: .error, buttonTitle: "REINTENTAR")
.onChange(of: viewModel.errorOccurred) { value in
if !value {
viewModel.getData()
}
}
.onChange(of: viewModel.loadingStoryIndex) { newValue in
if let data = viewModel.storyData, newValue == -1 {
let vc = PYStoryBuilder().build(data: data, onSeenStory: {
Expand Down
16 changes: 13 additions & 3 deletions Payan/App/Domain Modules/Feed/UI/View Model/PYFeedViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class PYFeedViewModel: ObservableObject {
@Published var storyData: PYStoryData?
@Published var loadingStoryIndex = -1
@Published var seenStories: [String] = []
@Published var errorMessage = ""
@Published var feedErrorOccurred = false
@Published var storyErrorOccurred = false

private var currentPercentageAddition = 0.1
private var lastScrollValue: CGFloat = .zero
Expand All @@ -26,7 +29,7 @@ class PYFeedViewModel: ObservableObject {

lazy var timer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { [weak self] _ in
guard let self = self else { return }
guard !self.errorOccurred else { return }
guard !self.feedErrorOccurred else { return }
guard self.loadedPercentage < 0.85 else { return }
self.loadedPercentage += self.currentPercentageAddition
self.currentPercentageAddition /= 1.15
Expand Down Expand Up @@ -56,14 +59,16 @@ class PYFeedViewModel: ObservableObject {
case .success(let data):
self.loadedPercentage = 1
self.feedData = data
self.feedErrorOccurred = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.isLoading = false
self.timer.invalidate()
self.onSuccess?()
}
case .failure(_):
self.loadedPercentage = 0
self.errorOccurred = true
self.feedErrorOccurred = true
self.errorMessage = "Ha ocurrido un error inesperado."
}
}
}
Expand All @@ -74,12 +79,17 @@ class PYFeedViewModel: ObservableObject {
}

func getStory(id: String, index: Int) {
errorOccurred = false
loadingStoryIndex = index
interactor.getStory(identifiedBy: id)
.receive(on: RunLoop.main)
.catch { [weak self] _ -> Empty<PYStoryData, Never> in
let empty = Empty<PYStoryData, Never>()
// TODO: show error
guard let self = self else { return empty }
self.loadingStoryIndex = -1
self.storyErrorOccurred = true
self.errorMessage = "No fue posible cargar la historia."
self.errorOccurred = true
return empty
}
.sink { [weak self] data in
Expand Down

0 comments on commit fe01fab

Please sign in to comment.