-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadingView.swift
72 lines (59 loc) · 2.17 KB
/
ReadingView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// ReadingView.swift
// manga_vault
//
// Created by Benjie Tigley on 9/17/24.
//
import SwiftUI
import WebKit
struct ReadingView: View {
var mangaTitle: String
var mangaURL: String // URL of the current chapter
@State private var currentChapterURL: URL
init(mangaTitle: String, mangaURL: String) {
self.mangaTitle = mangaTitle
self.mangaURL = mangaURL
_currentChapterURL = State(initialValue: URL(string: mangaURL)!) // Initialize the current chapter URL
}
var body: some View {
VStack {
WebView(url: currentChapterURL) // Load the current chapter URL
.edgesIgnoringSafeArea(.all)
}
.navigationTitle(mangaTitle)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Next Chapter") {
loadNextChapter()
}
}
}
}
private func loadNextChapter() {
saveChapterURL(currentURL: currentChapterURL)
}
private func saveChapterURL(currentURL: URL) {
// Convert the URL to a string for manipulation
let currentURLString = currentURL.absoluteString
guard let range = currentURLString.range(of: "/chapter/"),
let chapterNumber = Int(currentURLString[range.upperBound...]) else {
print("Invalid URL format")
return
}
let nextChapterNumber = chapterNumber + 1
let newURLString = currentURLString.replacingOccurrences(of: "/chapter/\(chapterNumber)", with: "/chapter/\(nextChapterNumber)")
// Convert the new URL string back to a URL object
guard let newURL = URL(string: newURLString) else {
print("Invalid new URL")
return
}
// Save the new URL to the database
saveToDatabase(newURL.absoluteString)
// Update the current chapter URL
currentChapterURL = newURL
}
private func saveToDatabase(_ url: String) {
// Database saving logic here (use CoreData, SQLite, or your preferred database solution)
print("URL saved to the database: \(url)")
}
}