create helper function to get unwrapped viewer properties

This commit is contained in:
Satindar Dhillon 2022-04-18 15:30:16 -07:00
parent 1d92e64801
commit 905e628e5a
5 changed files with 16 additions and 5 deletions

View file

@ -154,8 +154,8 @@ public final class PDFViewerViewModel: ObservableObject {
let baseURL = services.dataService.appEnvironment.serverBaseURL
var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: false)
if let viewer = services.dataService.currentViewer?.username {
components?.path = "/\(viewer)/\(feedItem.slug)/highlights/\(shortId)"
if let username = services.dataService.currentViewer?.username {
components?.path = "/\(username)/\(feedItem.slug)/highlights/\(shortId)"
} else {
return nil
}

View file

@ -56,7 +56,7 @@ enum PDFProvider {
if let viewer = viewer {
createWebAppWrapperViewModel(
username: viewer.username ?? "",
username: viewer.unwrappedUsername,
dataService: dataService,
rawAuthCookie: rawAuthCookie
)

View file

@ -21,8 +21,8 @@ import Views
guard let viewer = try? await dataService.fetchViewer() else { return }
profileCardData = ProfileCardData(
name: viewer.name ?? "",
username: viewer.username ?? "",
name: viewer.unwrappedName,
username: viewer.unwrappedUsername,
imageURL: viewer.profileImageURL.flatMap { URL(string: $0) }
)
}

View file

@ -44,3 +44,13 @@ public extension UserProfile {
return nil
}
}
public extension Viewer {
var unwrappedUsername: String {
username ?? ""
}
var unwrappedName: String {
name ?? ""
}
}

View file

@ -30,6 +30,7 @@ public final class DataService: ObservableObject {
public var currentViewer: Viewer? {
let fetchRequest: NSFetchRequest<Models.Viewer> = Viewer.fetchRequest()
fetchRequest.fetchLimit = 1 // we should only have one viewer saved
return try? persistentContainer.viewContext.fetch(fetchRequest).first
}