Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Fix for Issue 567 #592

Merged
merged 7 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Emitron/Emitron.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
2213193C23E47B6A00F15816 /* SessionController+States.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2213193B23E47B6A00F15816 /* SessionController+States.swift */; };
2213193F23E4E85500F15816 /* NoResultsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2213193E23E4E85500F15816 /* NoResultsView.swift */; };
2213194123E4E85F00F15816 /* OfflineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2213194023E4E85F00F15816 /* OfflineView.swift */; };
2213194323E4E86900F15816 /* ReloadView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2213194223E4E86900F15816 /* ReloadView.swift */; };
2213194323E4E86900F15816 /* ErrorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2213194223E4E86900F15816 /* ErrorView.swift */; };
2213194523E864D800F15816 /* PersistenceStoreChildContentsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2213194423E864D800F15816 /* PersistenceStoreChildContentsViewModel.swift */; };
2213194723EAFBD700F15816 /* VerticalFadeImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2213194623EAFBD700F15816 /* VerticalFadeImageView.swift */; };
2213194923EB04D300F15816 /* sampleCardImage.png in Resources */ = {isa = PBXBuildFile; fileRef = 2213194823EB04D300F15816 /* sampleCardImage.png */; };
Expand Down Expand Up @@ -359,7 +359,7 @@
2213193B23E47B6A00F15816 /* SessionController+States.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SessionController+States.swift"; sourceTree = "<group>"; };
2213193E23E4E85500F15816 /* NoResultsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoResultsView.swift; sourceTree = "<group>"; };
2213194023E4E85F00F15816 /* OfflineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OfflineView.swift; sourceTree = "<group>"; };
2213194223E4E86900F15816 /* ReloadView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReloadView.swift; sourceTree = "<group>"; };
2213194223E4E86900F15816 /* ErrorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorView.swift; sourceTree = "<group>"; };
2213194423E864D800F15816 /* PersistenceStoreChildContentsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistenceStoreChildContentsViewModel.swift; sourceTree = "<group>"; };
2213194623EAFBD700F15816 /* VerticalFadeImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VerticalFadeImageView.swift; sourceTree = "<group>"; };
2213194823EB04D300F15816 /* sampleCardImage.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = sampleCardImage.png; sourceTree = "<group>"; };
Expand Down Expand Up @@ -738,7 +738,7 @@
children = (
2213193E23E4E85500F15816 /* NoResultsView.swift */,
2213194023E4E85F00F15816 /* OfflineView.swift */,
2213194223E4E86900F15816 /* ReloadView.swift */,
2213194223E4E86900F15816 /* ErrorView.swift */,
22D39FC723EC0AEA0058599D /* LoadingView.swift */,
);
path = "Empty States";
Expand Down Expand Up @@ -2193,7 +2193,7 @@
B6D4529C22CAB67900BFB812 /* Date+Extensions.swift in Sources */,
B6C4F3BD22E668FC0087ED10 /* FiltersView.swift in Sources */,
8BFCEF1F2341184A003FF72F /* PermissionsRequest.swift in Sources */,
2213194323E4E86900F15816 /* ReloadView.swift in Sources */,
2213194323E4E86900F15816 /* ErrorView.swift in Sources */,
B6FC15C322CB55CC0078CEDB /* JSONAPIRelationship.swift in Sources */,
229F0AB723BACC320004DD4F /* ContentSummaryState+Fetchable.swift in Sources */,
B6C4F3D722E6EE5B0087ED10 /* Filter.swift in Sources */,
Expand Down
27 changes: 17 additions & 10 deletions Emitron/Emitron/Sessions/SessionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ protocol UserModelController {
}

// Conforming to NSObject, so that we can conform to ASWebAuthenticationPresentationContextProviding
class SessionController: NSObject, UserModelController, ObservablePrePostFactoObject, Refreshable {
// MARK: Refreshable
var refreshableCheckTimeSpan: RefreshableTimeSpan = .short

final class SessionController: NSObject, UserModelController, ObservablePrePostFactoObject {
private var subscriptions = Set<AnyCancellable>()

// Managing the state of the current session
private(set) var sessionState: SessionState = .unknown
@Published private(set) var sessionState: SessionState = .unknown
@Published private(set) var userState: UserState = .notLoggedIn
@Published private(set) var permissionState: PermissionState = .notLoaded

// MARK: - ObservablePrePostFactoObject, UserModelController
let objectDidChange = ObservableObjectPublisher()

// MARK: - ObservablePrePostFactoObject
@PublishedPrePostFacto var user: User? {
didSet {
if let user = user {
Expand All @@ -69,11 +70,13 @@ class SessionController: NSObject, UserModelController, ObservablePrePostFactoOb
}
}
}
let objectDidChange = ObservableObjectPublisher()


private(set) var client: RWAPI

// MARK: -

private let guardpost: Guardpost
private let connectionMonitor = NWPathMonitor()
private(set) var client: RWAPI
private(set) var permissionsService: PermissionsService

var isLoggedIn: Bool {
Expand Down Expand Up @@ -137,8 +140,7 @@ class SessionController: NSObject, UserModelController, ObservablePrePostFactoOb
case .failure(let error):
self.userState = .notLoggedIn
self.permissionState = .notLoaded
// Have to manually do this since we're not allowed @Published with enums
self.objectWillChange.send()

Failure
.login(from: "SessionController", reason: error.localizedDescription)
.log()
Expand Down Expand Up @@ -233,6 +235,11 @@ class SessionController: NSObject, UserModelController, ObservablePrePostFactoOb
}
}

// MARK: - Refreshable
extension SessionController: Refreshable {
var refreshableCheckTimeSpan: RefreshableTimeSpan { .short }
}

// MARK: - ASWebAuthenticationPresentationContextProviding
extension SessionController: ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
Expand Down
30 changes: 19 additions & 11 deletions Emitron/Emitron/UI/App Root/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import SwiftUI
import StoreKit

struct MainView: View {
@EnvironmentObject var sessionController: SessionController
@EnvironmentObject var dataManager: DataManager
@EnvironmentObject var messageBus: MessageBus
@EnvironmentObject var settingsManager: SettingsManager
@EnvironmentObject private var sessionController: SessionController
@EnvironmentObject private var dataManager: DataManager
@EnvironmentObject private var messageBus: MessageBus
@EnvironmentObject private var settingsManager: SettingsManager

private let tabViewModel = TabViewModel()
private let notification = NotificationCenter.default.publisher(for: .requestReview)
Expand Down Expand Up @@ -63,14 +63,22 @@ private extension MainView {
@ViewBuilder var contentView: some View {
if !sessionController.isLoggedIn {
LoginView()
} else if case .loaded = sessionController.permissionState {
if sessionController.hasPermissionToUseApp {
tabBarView
} else {
LogoutView()
}
} else {
PermissionsLoadingView()
switch sessionController.permissionState {
case .loaded:
if sessionController.hasPermissionToUseApp {
tabBarView
} else {
LogoutView()
}
case .notLoaded, .loading:
PermissionsLoadingView()
case .error:
ErrorView(
buttonTitle: "Back to login screen",
buttonAction: sessionController.logout
)
}
}
}

Expand Down
8 changes: 3 additions & 5 deletions Emitron/Emitron/UI/App Root/PermissionsLoadingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import SwiftUI

struct PermissionsLoadingView: View {
@EnvironmentObject var sessionController: SessionController
@State var showLogoutAlert = false
@EnvironmentObject private var sessionController: SessionController
@State private var showLogoutAlert = false

var body: some View {
LoadingView()
Expand All @@ -40,9 +40,7 @@ struct PermissionsLoadingView: View {
.alert(isPresented: $showLogoutAlert) {
Alert(
title: Text("Force Logout?"),
primaryButton: .destructive(Text("Logout"), action: {
sessionController.logout()
}),
primaryButton: .destructive(Text("Logout"), action: sessionController.logout),
secondaryButton: .cancel()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,43 @@

import SwiftUI

struct ReloadView<Header: View> {
struct ErrorView<Header: View> {
private let header: Header
private let titleText = "Something went wrong."
private let bodyText = "Please try again."
private let buttonTitle: String
private let buttonAction: () -> Void
}

// MARK: - internal
extension ErrorView {
init(
header: Header,
reloadHandler: @escaping () -> Void
buttonAction: @escaping () -> Void
) {
self.header = header
self.reloadHandler = reloadHandler
self.init(
header: header,
buttonTitle: "Reload",
buttonAction: buttonAction
)
}
}

private let header: Header
private let reloadHandler: () -> Void
extension ErrorView where Header == EmptyView {
init(
buttonTitle: String,
buttonAction: @escaping () -> Void
) {
self.init(
header: .init(),
buttonTitle: buttonTitle,
buttonAction: buttonAction
)
}
}

// MARK: - View {
extension ReloadView: View {
extension ErrorView: View {
var body: some View {
ZStack {
Rectangle()
Expand All @@ -55,13 +77,13 @@ extension ReloadView: View {
Image("emojiCrying")
.padding(.bottom, 30)

Text("Something went wrong.")
Text(titleText)
.font(.uiTitle2)
.foregroundColor(.titleText)
.multilineTextAlignment(.center)
.padding([.leading, .trailing, .bottom], 20)

Text("Please try again.")
Text(bodyText)
.lineSpacing(8)
.font(.uiLabel)
.foregroundColor(.contentText)
Expand All @@ -71,9 +93,9 @@ extension ReloadView: View {
Spacer()

MainButtonView(
title: "Reload",
title: buttonTitle,
type: .primary(withArrow: false),
callback: reloadHandler)
callback: buttonAction)
.padding([.horizontal, .bottom], 20)
}
}
Expand All @@ -82,9 +104,13 @@ extension ReloadView: View {

struct ErrorView_Previews: PreviewProvider {
static var previews: some View {
SwiftUI.Group {
ReloadView(header: EmptyView(), reloadHandler: {}).colorScheme(.light)
ReloadView(header: EmptyView(), reloadHandler: {}).colorScheme(.dark)
}
ErrorView().colorScheme(.light)
ErrorView().colorScheme(.dark)
}
}

private extension ErrorView where Header == EmptyView {
init() {
self.init(header: .init()) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private extension ContentListView {
var reloadView: some View {
ZStack {
Color.backgroundColor.edgesIgnoringSafeArea(.all)
ReloadView(header: header, reloadHandler: contentRepository.reload)
ErrorView(header: header, buttonAction: contentRepository.reload)
}
}

Expand Down