Skip to content

Commit

Permalink
Still building new dawn
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuz0u committed May 7, 2021
1 parent 46027cd commit 79d3b1e
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 31 deletions.
4 changes: 4 additions & 0 deletions EhPanda/App/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct Defaults {
static let watched = "watched"
static let mytags = "mytags"
static let api = "api.php"
static let news = "news.php"
static let index = "index.php"
static let uconfig = "uconfig.php"
static let favorites = "favorites.php"
Expand Down Expand Up @@ -236,6 +237,9 @@ extension Defaults.URL {
static func userInfo(uid: String) -> String {
merge([forum + index, showuser + uid])
}
static func greeting() -> String {
ehentai + news
}

// Misc
static func contentPage(url: String, pageNum: Int) -> String {
Expand Down
3 changes: 0 additions & 3 deletions EhPanda/App/Tools/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,6 @@ extension Parser {
break
}

guard !greeting.gainedNothing
else { throw AppError.parseFailed }

greeting.updateTime = Date()
return greeting
}
Expand Down
2 changes: 2 additions & 0 deletions EhPanda/DataFlow/AppAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ enum AppAction {
case toggleCommentViewSheetState(state: CommentViewSheetState)
case toggleCommentViewSheetNil

case fetchGreeting
case fetchGreetingDone(result: Result<Greeting, AppError>)
case fetchUserInfo(uid: String)
case fetchUserInfoDone(result: Result<User, AppError>)
case fetchFavoriteNames
Expand Down
18 changes: 18 additions & 0 deletions EhPanda/DataFlow/AppCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ protocol AppCommand {
func execute(in store: Store)
}

struct FetchGreetingCommand: AppCommand {
func execute(in store: Store) {
let token = SubscriptionToken()
GreetingRequest()
.publisher
.receive(on: DispatchQueue.main)
.sink { completion in
if case .failure(let error) = completion {
store.dispatch(.fetchGreetingDone(result: .failure(error)))
}
token.unseal()
} receiveValue: { greeting in
store.dispatch(.fetchGreetingDone(result: .success(greeting)))
}
.seal(in: token)
}
}

struct FetchUserInfoCommand: AppCommand {
let uid: String

Expand Down
16 changes: 16 additions & 0 deletions EhPanda/DataFlow/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ extension AppState {
// MARK: HomeInfo
struct HomeInfo {
var searchKeyword = ""
var greeting: Greeting?
var greetingLoading = false

var searchItems: [Manga]?
var searchLoading = false
Expand Down Expand Up @@ -134,6 +136,20 @@ extension AppState {
return tmp
}

mutating func insertGreeting(greeting: Greeting) {
guard let currDate = self.greeting?.updateTime
else { return }

if let prevGreeting = self.greeting,
let prevDate = prevGreeting.updateTime,
prevDate < currDate
{
self.greeting = greeting
} else if self.greeting == nil {
self.greeting = greeting
}
}

mutating func insertSearchItems(mangas: [Manga]) {
mangas.forEach { manga in
if searchItems?.contains(manga) == false {
Expand Down
16 changes: 16 additions & 0 deletions EhPanda/DataFlow/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ final class Store: ObservableObject {
appState.commentInfo.commentContent = ""

// MARK: Fetch Data
case .fetchGreeting:
if !didLogin && isTokenMatched { break }
if appState.homeInfo.greetingLoading { break }
appState.homeInfo.greetingLoading = true

appCommand = FetchGreetingCommand()
case .fetchGreetingDone(let result):
appState.homeInfo.greetingLoading = false

switch result {
case .success(let greeting):
appState.homeInfo.insertGreeting(greeting: greeting)
case .failure(let error):
print(error)
}

case .fetchUserInfo(let uid):
if !didLogin && isTokenMatched { break }
if appState.settings.userInfoLoading { break }
Expand Down
4 changes: 3 additions & 1 deletion EhPanda/Models/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ struct AssociatedItem {
var mangas: [Manga]
}

struct Greeting: Codable {
struct Greeting: Identifiable, Codable, Equatable {
var id = UUID()

var gainedEXP: Int?
var gainedCredits: Int?
var gainedGP: Int?
Expand Down
13 changes: 13 additions & 0 deletions EhPanda/Network/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ private func mapAppError(_ error: Error) -> AppError {
}
}

struct GreetingRequest {
var publisher: AnyPublisher<Greeting, AppError> {
URLSession.shared
.dataTaskPublisher(
for: Defaults.URL.greeting().safeURL()
)
.tryMap { try Kanna.HTML(html: $0.data, encoding: .utf8) }
.tryMap(Parser.parseGreeting)
.mapError(mapAppError)
.eraseToAnyPublisher()
}
}

struct UserInfoRequest {
let uid: String

Expand Down
48 changes: 48 additions & 0 deletions EhPanda/View/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct HomeView: View, StoreAccessor {

@State private var clipboardJumpID: String?
@State private var isJumpNavActive = false
@State private var greeting: Greeting?

@State private var hudVisible = false
@State private var hudConfig = TTProgressHUDConfig(
Expand Down Expand Up @@ -41,6 +42,10 @@ struct HomeView: View, StoreAccessor {
of: environment.favoritesIndex,
perform: onFavoritesIndexChange
)
.onChange(
of: homeInfo.greeting,
perform: onReceiveGreeting
)
.onAppear(perform: onListAppear)
.navigationBarTitle(navigationBarTitle)
.navigationBarItems(trailing:
Expand All @@ -50,6 +55,7 @@ struct HomeView: View, StoreAccessor {
}
}
.navigationViewStyle(StackNavigationViewStyle())
.fullScreenCover(item: $greeting, content: NewDawnView.init)
.sheet(item: environmentBinding.homeViewSheetState) { item in
switch item {
case .setting:
Expand Down Expand Up @@ -230,6 +236,8 @@ private extension HomeView {

func onAppear() {
detectPasteboard()
greeting = Greeting()
// fetchGreetingIfNeeded()
}
func onListAppear() {
if settings.user == nil {
Expand Down Expand Up @@ -265,6 +273,13 @@ private extension HomeView {
print(type)
}
}
func onReceiveGreeting(_ greeting: Greeting?) {
if let greeting = greeting,
!greeting.gainedNothing
{
self.greeting = greeting
}
}
func onFavoritesIndexChange(_ : Int) {
fetchFavoritesItemsIfNeeded()
}
Expand Down Expand Up @@ -337,6 +352,9 @@ private extension HomeView {
}
}

func fetchGreeting() {
store.dispatch(.fetchGreeting)
}
func fetchUserInfo() {
if let uid = settings.user?.apiuid, !uid.isEmpty {
store.dispatch(.fetchUserInfo(uid: uid))
Expand Down Expand Up @@ -381,6 +399,36 @@ private extension HomeView {
store.dispatch(.replaceMangaCommentJumpID(gid: gid))
}

func fetchGreetingIfNeeded() {
func verifyDate(with updateTime: Date?) -> Bool {
guard let updateTime = updateTime else { return false }

let currentTime = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd MMMM yyyy"
formatter.locale = Locale.current
formatter.timeZone = TimeZone(secondsFromGMT: 0)

let currentTimeString = formatter.string(from: currentTime)
if let currDay = formatter.date(from: currentTimeString) {
let prevDay = currDay.addingTimeInterval(-60 * 60 * 24)

return currentTime > currDay
&& updateTime < currDay
&& updateTime > prevDay
}

return false
}

if let greeting = homeInfo.greeting {
if verifyDate(with: greeting.updateTime) {
fetchGreeting()
}
} else {
fetchGreeting()
}
}
func fetchFrontpageItemsIfNeeded() {
if homeInfo.frontpageItems?.isEmpty != false {
fetchFrontpageItems()
Expand Down
84 changes: 57 additions & 27 deletions EhPanda/View/Tools/NewDawnView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import SwiftUI

struct NewDawnView: View {
@Environment(\.colorScheme) private var colorScheme
@State private var rotationAngle: Double = 0
@State private var greeting: Greeting?
@State private var timer = Timer
.publish(
every: 1/10,
Expand All @@ -18,11 +18,12 @@ struct NewDawnView: View {
)
.autoconnect()

private var reversePrimary: Color {
colorScheme == .light ? .white : .black
}
private let offset = screenW * 0.2

init(greeting: Greeting) {
self.greeting = greeting
}

var body: some View {
ZStack {
LinearGradient(
Expand All @@ -43,29 +44,23 @@ struct NewDawnView: View {
Spacer()
}
.ignoresSafeArea()
VStack(spacing: 10) {
HStack {
Text("It is the dawn of a new day!")
.fontWeight(.bold)
.font(.largeTitle)
.foregroundColor(reversePrimary)
Spacer()
}
HStack {
Text("Reflecting on your journey so far, you find that you are a little wiser.")
.fontWeight(.bold)
.font(.title2)
.foregroundColor(reversePrimary)
Spacer()
}
.padding(.bottom, 50)
HStack {
Text("You gain 30 EXP, 10,393 Credits, 10,000 GP and 11 Hath!")
.fontWeight(.bold)
.font(.title3)
.foregroundColor(reversePrimary)
Spacer()
VStack(spacing: 50) {
VStack(spacing: 10) {
TextView(
text: "It is the dawn of a new day!",
font: .largeTitle
)
TextView(
text: "Reflecting on your journey so far, you find that you are a little wiser.",
font: .title2
)
}
TextView(
text: "You gain 30 EXP, 10,393 Credits, 10,000 GP and 11 Hath!",
font: .title3,
fontWeight: .bold,
lineLimit: 3
)
}
.padding()
}
Expand All @@ -81,6 +76,41 @@ private extension NewDawnView {
}
}

private struct TextView: View {
@Environment(\.colorScheme) private var colorScheme
private let text: String
private let font: Font
private let fontWeight: Font.Weight
private let lineLimit: Int?

private var reversePrimary: Color {
colorScheme == .light ? .white : .black
}

init(
text: String,
font: Font,
fontWeight: Font.Weight = .bold,
lineLimit: Int? = nil
) {
self.text = text
self.font = font
self.fontWeight = fontWeight
self.lineLimit = lineLimit
}

var body: some View {
HStack {
Text(text)
.fontWeight(fontWeight)
.font(font)
.lineLimit(lineLimit)
.foregroundColor(reversePrimary)
Spacer()
}
}
}

private struct SunView: View {
private let width = screenW * 0.75
private var offset: CGFloat { width / 2 + 70 }
Expand Down Expand Up @@ -131,6 +161,6 @@ private struct SunBeamView: View {

struct NewDawnView_Previews: PreviewProvider {
static var previews: some View {
NewDawnView()
NewDawnView(greeting: Greeting())
}
}

0 comments on commit 79d3b1e

Please sign in to comment.