Skip to content

Commit

Permalink
NewDawn feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuz0u committed May 13, 2021
1 parent de2cbd7 commit c9d0fda
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 67 deletions.
13 changes: 13 additions & 0 deletions EhPanda/App/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,16 @@ extension Bundle {
return nil
}
}

extension Int {
var withComma: String? {
let decimalFormatter = NumberFormatter()
decimalFormatter.numberStyle = .decimal
decimalFormatter.locale = Locale.current

let string = decimalFormatter.string(
from: self as NSNumber
)
return string
}
}
26 changes: 18 additions & 8 deletions EhPanda/App/Tools/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,18 @@ struct Parser {
extension Parser {
// MARK: Greeting
static func parseGreeting(_ doc: HTMLDocument) throws -> Greeting {
func trimString(_ string: String) -> String {
string
.replacingOccurrences(of: ",", with: "")
.replacingOccurrences(of: "!", with: "")
.replacingOccurrences(of: "and", with: "")
.trimmingCharacters(in: .whitespacesAndNewlines)
func trimString(_ string: String) -> String? {
if string.contains("EXP") {
return "EXP"
} else if string.contains("Credits") {
return "Credits"
} else if string.contains("GP") {
return "GP"
} else if string.contains("Hath") {
return "Hath"
} else {
return nil
}
}

func trimInt(_ value: String) -> Int? {
Expand Down Expand Up @@ -633,13 +639,17 @@ extension Parser {
let removeText = String(text.prefix(upTo: range.upperBound))

if value != gainedValues.first {
gainedTypes.append(trimString(removeText))
if let text = trimString(removeText) {
gainedTypes.append(text)
}
}

text = text.replacingOccurrences(of: removeText, with: "")

if value == gainedValues.last {
gainedTypes.append(trimString(text))
if let text = trimString(text) {
gainedTypes.append(text)
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions EhPanda/App/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@
// MARK: User
"favoriteNameByDev" = "Favorite";
"all_appendedByDev" = "All";

// MARK: NewDawnView
"GAINCONTENT_START" = "You gain ";
"GAINCONTENT_SEPARATOR" = ", ";
"GAINCONTENT_AND" = " and ";
"GAINCONTENT_END" = "!";
9 changes: 9 additions & 0 deletions EhPanda/App/ja.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@
"Disable uploader filter" = "アップローダフィルターを無効化";
"Disable tags filter" = "タグフィルターを無効化";

// MARK: NewDawnView
"Show new dawn greeting" = "夜明けの挨拶を表示";
"It is the dawn of a new day!" = "新しい一日の夜明けです!";
"Reflecting on your journey so far, you find that you are a little wiser." = "今までの歩みを振り返り、少し賢くなった気がする。";
"GAINCONTENT_START" = "";
"GAINCONTENT_SEPARATOR" = "、";
"GAINCONTENT_AND" = "と";
"GAINCONTENT_END" = "を手に入れた!";

// MARK: HomeListType
"Search" = "検索";
"Frontpage" = "ホーム";
Expand Down
9 changes: 9 additions & 0 deletions EhPanda/App/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@
"Disable uploader filter" = "禁用上传者筛选";
"Disable tags filter" = "禁用标签筛选";

// MARK: NewDawnView
"Show new dawn greeting" = "显示黎明问候";
"It is the dawn of a new day!" = "又是新一天的黎明!";
"Reflecting on your journey so far, you find that you are a little wiser." = "回顾至今的历程,发觉自己更睿智了一些。";
"GAINCONTENT_START" = "你获得了";
"GAINCONTENT_SEPARATOR" = "、";
"GAINCONTENT_AND" = "和";
"GAINCONTENT_END" = "!";

// MARK: HomeListType
"Search" = "搜索";
"Frontpage" = "主页";
Expand Down
31 changes: 15 additions & 16 deletions EhPanda/DataFlow/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extension AppState {
struct Settings {
var userInfoLoading = false
var favoriteNamesLoading = false
var greetingLoading = false

@FileStorage(directory: .cachesDirectory, fileName: "user.json")
var user: User?
Expand All @@ -66,15 +67,27 @@ extension AppState {
self.user?.currentCredits = currentCredits
}
}

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

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

extension AppState {
// MARK: HomeInfo
struct HomeInfo {
var searchKeyword = ""
var greeting: Greeting?
var greetingLoading = false

var searchItems: [Manga]?
var searchLoading = false
Expand Down Expand Up @@ -136,20 +149,6 @@ 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
8 changes: 4 additions & 4 deletions EhPanda/DataFlow/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ final class Store: ObservableObject {
// MARK: Fetch Data
case .fetchGreeting:
if !didLogin && isTokenMatched { break }
if appState.homeInfo.greetingLoading { break }
appState.homeInfo.greetingLoading = true
if appState.settings.greetingLoading { break }
appState.settings.greetingLoading = true

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

switch result {
case .success(let greeting):
appState.homeInfo.insertGreeting(greeting: greeting)
appState.settings.insertGreeting(greeting: greeting)
case .failure(let error):
print(error)
}
Expand Down
47 changes: 47 additions & 0 deletions EhPanda/Models/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,53 @@ struct Greeting: Identifiable, Codable, Equatable {
var gainedHath: Int?
var updateTime: Date?

var strings: [String] {
var strings = [String]()

if let exp = gainedEXP?.withComma {
strings.append(exp + " EXP")
}
if let credits = gainedCredits?.withComma {
strings.append(credits + " Credits")
}
if let galleryPoint = gainedGP?.withComma {
strings.append(galleryPoint + " GP")
}
if let hath = gainedHath?.withComma {
strings.append(hath + " Hath")
}

return strings
}

var gainContent: String? {
guard !strings.isEmpty else { return nil }

var base = "GAINCONTENT_START".localized()

if strings.count == 1 {
base += strings[0]
} else {
let stringsToJoin = strings.count > 2
? strings.dropLast() : strings

base += stringsToJoin
.joined(
separator:
"GAINCONTENT_SEPARATOR"
.localized()
)
if strings.count > 2 {
base += "GAINCONTENT_AND".localized()
base += strings[strings.count - 1]
}
}

base += "GAINCONTENT_END".localized()

return base
}

var gainedNothing: Bool {
[
gainedEXP,
Expand Down
1 change: 1 addition & 0 deletions EhPanda/Models/Setting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct Setting: Codable {
setGalleryType(with: galleryType)
}
}
var showNewDawnGreeting = false

// General
var detectGalleryFromPasteboard = false
Expand Down
2 changes: 2 additions & 0 deletions EhPanda/Models/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct User: Codable {
var currentGP: String?
var currentCredits: String?

var greeting: Greeting?

var apiuid: String {
getCookieValue(
url: Defaults.URL.host.safeURL(),
Expand Down
32 changes: 23 additions & 9 deletions EhPanda/View/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct HomeView: View, StoreAccessor {
perform: onFavoritesIndexChange
)
.onChange(
of: homeInfo.greeting,
of: user?.greeting,
perform: onReceiveGreeting
)
.onAppear(perform: onListAppear)
Expand All @@ -55,7 +55,6 @@ struct HomeView: View, StoreAccessor {
}
}
.navigationViewStyle(StackNavigationViewStyle())
.fullScreenCover(item: $greeting, content: NewDawnView.init)
.sheet(item: environmentBinding.homeViewSheetState) { item in
switch item {
case .setting:
Expand All @@ -72,6 +71,11 @@ struct HomeView: View, StoreAccessor {
.preferredColorScheme(colorScheme)
.blur(radius: environment.blurRadius)
.allowsHitTesting(environment.isAppUnlocked)
case .newDawn:
NewDawnView(greeting: greeting)
.preferredColorScheme(colorScheme)
.blur(radius: environment.blurRadius)
.allowsHitTesting(environment.isAppUnlocked)
}
}
.onAppear(perform: onAppear)
Expand Down Expand Up @@ -236,8 +240,7 @@ private extension HomeView {

func onAppear() {
detectPasteboard()
// greeting = Greeting()
// fetchGreetingIfNeeded()
fetchGreetingIfNeeded()
}
func onListAppear() {
if settings.user == nil {
Expand All @@ -253,7 +256,10 @@ private extension HomeView {
fetchFrontpageItemsIfNeeded()
}
func onBecomeActive() {
detectPasteboard()
if vcsCount == 1 {
detectPasteboard()
fetchGreetingIfNeeded()
}
}
func onHomeListTypeChange(_ type: HomeListType) {
switch type {
Expand All @@ -278,6 +284,7 @@ private extension HomeView {
!greeting.gainedNothing
{
self.greeting = greeting
toggleNewDawn()
}
}
func onFavoritesIndexChange(_ : Int) {
Expand Down Expand Up @@ -421,12 +428,14 @@ private extension HomeView {
return false
}

if let greeting = homeInfo.greeting {
if verifyDate(with: greeting.updateTime) {
if setting?.showNewDawnGreeting == true {
if let greeting = user?.greeting {
if verifyDate(with: greeting.updateTime) {
fetchGreeting()
}
} else {
fetchGreeting()
}
} else {
fetchGreeting()
}
}
func fetchFrontpageItemsIfNeeded() {
Expand All @@ -449,6 +458,10 @@ private extension HomeView {
fetchFavoritesItems()
}
}

func toggleNewDawn() {
store.dispatch(.toggleHomeViewSheetState(state: .newDawn))
}
}

// MARK: GenericList
Expand Down Expand Up @@ -672,4 +685,5 @@ enum HomeViewSheetState: Identifiable {

case setting
case filter
case newDawn
}
4 changes: 4 additions & 0 deletions EhPanda/View/Setting/AccountSettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ struct AccountSettingView: View {
.withArrow()
Button("Manage tags subscription", action: onMyTagsTap)
.withArrow()
Toggle(
"Show new dawn greeting",
isOn: settingBinding.showNewDawnGreeting
)
}
.foregroundColor(.primary)
}
Expand Down
Loading

0 comments on commit c9d0fda

Please sign in to comment.