Skip to content

Commit

Permalink
Create snapshot variants (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-e authored Jun 17, 2024
1 parent 36a28fc commit 8b19cf6
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 204 deletions.
8 changes: 6 additions & 2 deletions ios/HackerNews.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
1DF161FA2A4346F6001A3F76 /* StoryRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DF161F92A4346F6001A3F76 /* StoryRow.swift */; };
1DF162002A4365A1001A3F76 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DF161FF2A4365A1001A3F76 /* Colors.swift */; };
1DF162032A436E7B001A3F76 /* DateUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DF162022A436E7B001A3F76 /* DateUtils.swift */; };
A40DC1FA2C20D8B60055B920 /* Previews.swift in Sources */ = {isa = PBXBuildFile; fileRef = A40DC1F92C20D8B60055B920 /* Previews.swift */; };
A413E8572A8C40E800C0F867 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A413E8562A8C40E800C0F867 /* Extensions.swift */; };
A413E8592A8D868500C0F867 /* ThemedButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = A413E8582A8D868500C0F867 /* ThemedButtonStyle.swift */; };
A423B0682BAE05FB00267DDB /* NetworkDebugger.swift in Sources */ = {isa = PBXBuildFile; fileRef = A423B0672BAE05FB00267DDB /* NetworkDebugger.swift */; };
Expand Down Expand Up @@ -60,6 +61,7 @@
1DF161F92A4346F6001A3F76 /* StoryRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoryRow.swift; sourceTree = "<group>"; };
1DF161FF2A4365A1001A3F76 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = "<group>"; };
1DF162022A436E7B001A3F76 /* DateUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateUtils.swift; sourceTree = "<group>"; };
A40DC1F92C20D8B60055B920 /* Previews.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Previews.swift; sourceTree = "<group>"; };
A413E8562A8C40E800C0F867 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
A413E8582A8D868500C0F867 /* ThemedButtonStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemedButtonStyle.swift; sourceTree = "<group>"; };
A423B0672BAE05FB00267DDB /* NetworkDebugger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkDebugger.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -166,6 +168,7 @@
1DF162022A436E7B001A3F76 /* DateUtils.swift */,
A413E8562A8C40E800C0F867 /* Extensions.swift */,
A423B0692BAE061600267DDB /* Flags.swift */,
A40DC1F92C20D8B60055B920 /* Previews.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -527,6 +530,7 @@
1DF161FA2A4346F6001A3F76 /* StoryRow.swift in Sources */,
1DF162002A4365A1001A3F76 /* Colors.swift in Sources */,
A42705AB2A4296BA0057E439 /* PostListScreen.swift in Sources */,
A40DC1FA2C20D8B60055B920 /* Previews.swift in Sources */,
A427057F2A4293B10057E439 /* ContentView.swift in Sources */,
1DF162032A436E7B001A3F76 /* DateUtils.swift in Sources */,
A413E8572A8C40E800C0F867 /* Extensions.swift in Sources */,
Expand Down Expand Up @@ -717,7 +721,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -759,7 +763,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
10 changes: 9 additions & 1 deletion ios/HackerNews/Components/CommentRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ struct CommentRow: View {
}

struct CommentView_Preview: PreviewProvider {
static var previews: some View {
PreviewVariants {
CommentRow(comment: PreviewHelpers.makeFakeComment(), level: 0)
}
}
}

struct CommentViewIndentation_Preview: PreviewProvider {
static var previews: some View {
Group {
ForEach(0..<6) { index in
CommentRow(comment: makeFakeComment(), level: index)
CommentRow(comment: PreviewHelpers.makeFakeComment(), level: index)
.previewLayout(.sizeThatFits)
.previewDisplayName("Indentation \(index)")
}
Expand Down
50 changes: 3 additions & 47 deletions ios/HackerNews/Components/StoryRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,53 +85,9 @@ struct StoryRow: View {

struct StoryRow_Preview: PreviewProvider {
static var previews: some View {
let mockStory = Story.init(
id: 0,
by: "Ryan",
time: Int64(Date().timeIntervalSinceNow),
type: ItemType.story,
title: "Mock story for preview",
text: nil,
url: "https://emergetools.com",
score: 100,
descendants: 3,
kids: [1, 2, 3]
)
let appState = AppViewModel()

Group {
StoryRow(appState: appState, story: mockStory, index: 0)
.previewDevice(PreviewDevice(rawValue: "iPhone 14 Pro"))
.previewDisplayName("iPhone 14 Pro")
.previewLayout(.sizeThatFits)

StoryRow(appState: appState, story: mockStory, index: 0)
.previewDevice(PreviewDevice(rawValue: "iPhone 14 Pro"))
.previewDisplayName("iPhone 14 Pro, dark")
.previewLayout(.sizeThatFits)
.colorScheme(.dark)

StoryRow(appState: appState, story: mockStory, index: 0)
.previewDevice(PreviewDevice(rawValue: "iPhone SE (3rd generation)"))
.previewDisplayName("iPhone SE (3rd generation)")
.previewLayout(.sizeThatFits)

StoryRow(appState: appState, story: mockStory, index: 0)
.previewDevice(PreviewDevice(rawValue: "iPhone SE (3rd generation)"))
.previewDisplayName("iPhone SE (3rd generation), dark")
.previewLayout(.sizeThatFits)
.colorScheme(.dark)

StoryRow(appState: appState, story: mockStory, index: 0)
.previewDevice(PreviewDevice(rawValue: "iPad Pro (11-inch) (4th generation)"))
.previewDisplayName("iPad Pro (11-inch) (4th generation)")
.previewLayout(.sizeThatFits)

StoryRow(appState: appState, story: mockStory, index: 0)
.previewDevice(PreviewDevice(rawValue: "iPad Pro (11-inch) (4th generation)"))
.previewDisplayName("iPad Pro (11-inch) (4th generation), dark")
.previewLayout(.sizeThatFits)
.colorScheme(.dark)
let fakeStory = PreviewHelpers.makeFakeStory(index: 0, descendants: 3, kids: [1, 2, 3])
PreviewVariants {
StoryRow(appState: AppViewModel(), story: fakeStory, index: 0)
}
}
}
51 changes: 51 additions & 0 deletions ios/HackerNews/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,54 @@ struct ContentView: View {
}

}

struct ContentView_LoggedOut_Previews: PreviewProvider {
static var previews: some View {
let appModel = AppViewModel()
appModel.authState = .loggedOut
return PreviewVariants {
PreviewHelpers.withNavigationView {
ContentView(appState: appModel)
}
}
}
}

struct ContentView_LoggedIn_Loading_Previews: PreviewProvider {
static var previews: some View {
let appModel = AppViewModel()
appModel.authState = .loggedIn
appModel.storiesState = .loading
return PreviewVariants {
PreviewHelpers.withNavigationView {
ContentView(appState: appModel)
}
}
}
}

struct ContentView_LoggedIn_WithPosts_Previews: PreviewProvider {
static var previews: some View {
let appModel = AppViewModel()
appModel.authState = .loggedIn
appModel.storiesState = .loaded(stories: PreviewHelpers.makeFakeStories())
return PreviewVariants {
PreviewHelpers.withNavigationView {
ContentView(appState: appModel)
}
}
}
}

struct ContentView_LoggedIn_EmptyPosts_Previews: PreviewProvider {
static var previews: some View {
let appModel = AppViewModel()
appModel.authState = .loggedIn
appModel.storiesState = .loaded(stories: [])
return PreviewVariants {
PreviewHelpers.withNavigationView {
ContentView(appState: appModel)
}
}
}
}
20 changes: 0 additions & 20 deletions ios/HackerNews/Screens/LoginScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,3 @@ struct LoginScreen: View {
}

}

struct LoginScreen_Previews: PreviewProvider {
static var previews: some View {
let loggedOut = AppViewModel()
loggedOut.authState = .loggedOut

return Group {
withNavigationView {
ContentView(appState: loggedOut)
}
.previewDisplayName("Logged out")

withNavigationView {
ContentView(appState: loggedOut)
}
.colorScheme(.dark)
.previewDisplayName("Logged out, dark mode")
}
}
}
66 changes: 0 additions & 66 deletions ios/HackerNews/Screens/PostListScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,69 +65,3 @@ struct PostListScreen: View {
}

}

struct PostListScreen_Previews: PreviewProvider {
static var previews: some View {
let appState = AppViewModel()
appState.storiesState = .loaded(stories: makeFakeStories())

let loading = AppViewModel()
loading.authState = .loggedIn
loading.storiesState = .loading

let loggedIn = AppViewModel()
loggedIn.authState = .loggedIn

return Group {
withNavigationView {
PostListScreen(appState: appState)
.previewDisplayName("With posts")
}

withNavigationView {
PostListScreen(appState: appState)
}
.colorScheme(.dark)
.previewDisplayName("With posts, dark mode")

withNavigationView {
PostListScreen(appState: loading)
}
.previewDisplayName("Loading")

withNavigationView {
PostListScreen(appState: loading)
}
.colorScheme(.dark)
.previewDisplayName("Loading, dark mode")

withNavigationView {
PostListScreen(appState: loggedIn)
}
.previewDisplayName("No posts")

withNavigationView {
PostListScreen(appState: loggedIn)
}
.colorScheme(.dark)
.previewDisplayName("No posts, dark mode")
}
}

static func makeFakeStories() -> [Story] {
return (0..<20).map { index in
return Story(
id: index,
by: "dang",
time: Int64(Date().timeIntervalSince1970) - Int64(index),
type: .story,
title: "Test story \(index)",
text: "Test story body \(index)",
url: "https://emergetools.com",
score: 100,
descendants: 3,
kids: nil
)
}
}
}
14 changes: 7 additions & 7 deletions ios/HackerNews/Screens/StoryScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ struct StoryScreen: View {
struct StoryScreen_Preview: PreviewProvider {
static var previews: some View {
let comments = [
makeFakeFlattenedComment(),
makeFakeFlattenedComment(),
makeFakeFlattenedComment(),
makeFakeFlattenedComment()
PreviewHelpers.makeFakeFlattenedComment(),
PreviewHelpers.makeFakeFlattenedComment(),
PreviewHelpers.makeFakeFlattenedComment(),
PreviewHelpers.makeFakeFlattenedComment()
]
let viewModel = StoryViewModel(story: makeFakeStory(kids: comments.map { $0.comment.id }))
let viewModel = StoryViewModel(story: PreviewHelpers.makeFakeStory(kids: comments.map { $0.comment.id }))
viewModel.state = .loaded(comments: comments)
return Group {
withNavigationView {
return PreviewVariants {
PreviewHelpers.withNavigationView {
StoryScreen(storyModel: viewModel)
}
}
Expand Down
61 changes: 0 additions & 61 deletions ios/HackerNews/Utils/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,8 @@
//

import Foundation
import SwiftUI
import SwiftSoup

extension PreviewProvider {
static func withNavigationView(@ViewBuilder content: () -> some View) -> some View {
NavigationStack {
ZStack {
HNColors.background
.ignoresSafeArea()

content()
}
.toolbarColorScheme(.dark, for: .navigationBar)
.toolbarBackground(HNColors.orange, for: .navigationBar)
.toolbarBackground(.visible, for: .navigationBar)
}
}

static func makeFakeStories() -> [Story] {
return (0..<20).map { index in
makeFakeStory(index: index)
}
}

static func makeFakeStory(index: Int64 = 0, descendants: Int = 0, kids: [Int64]? = nil) -> Story {
return Story(
id: index,
by: "dang",
time: Int64(Date().timeIntervalSince1970) - Int64(index),
type: .story,
title: "Test story \(index)",
text: "Test story body \(index)",
url: "https://emergetools.com",
score: 100,
descendants: descendants,
kids: kids
)
}

static func makeFakeComment() -> Comment {
Comment(
id: 1,
by: "dang",
time: Int64(Date.now.timeIntervalSince1970),
type: .comment,
text: """
Totally useless commentary:
It makes me deeply happy to hear success stories like this for a project that's moving in the correctly opposite direction to that of the rest of the world.
Engildification. Of which there should be more!
My soul was also satisfied by the Sleeping At Night post which, along with the recent "Lie Still in Bed" article, makes for very simple options to attempt to fix sleep (discipline) issues
""",
parent: nil,
kids: nil
)
}

static func makeFakeFlattenedComment(comment: Comment = makeFakeComment(), depth: Int = 0) -> FlattenedComment {
FlattenedComment(comment: comment, depth: depth)
}
}

extension String {
func strippingHTML() -> String {
guard let doc: Document = try? SwiftSoup.parse(self) else { return "" } // parse html
Expand Down
Loading

0 comments on commit 8b19cf6

Please sign in to comment.