gloss-ios/gloss/ContentView.swift

314 lines
11 KiB
Swift
Raw Normal View History

//
// ContentView.swift
// gloss
//
// Created by Saint on 10/23/22.
//
import SwiftUI
import GRDB
import GRDBQuery
import Introspect
import os
let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "network")
// var curBook = "John"
2023-02-28 14:03:58 -08:00
extension UserDefaults {
public func optionalInt(forKey defaultName: String) -> Int? {
let defaults = self
if let value = defaults.value(forKey: defaultName) {
return value as? Int
}
return nil
}
public func optionalBool(forKey defaultName: String) -> Bool? {
let defaults = self
if let value = defaults.value(forKey: defaultName) {
return value as? Bool
}
return nil
}
}
struct BlueButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.font(.headline)
.frame(width: 160)
.contentShape(Rectangle())
.foregroundColor(configuration.isPressed ? Color.white.opacity(0.5) : Color.black)
.background(configuration.isPressed ? Color.purple.opacity(0.5) : Color.purple)
.listRowBackground(configuration.isPressed ? Color.blue.opacity(0.5) : Color.black)
}
}
struct SwitchButton : View {
2023-03-01 10:47:34 -08:00
var ribbon: Ribbon
// var selectedRibbon: SelectedRibbon
@Binding var book : String
2023-03-01 10:47:34 -08:00
@Binding var scrollView : UIScrollView?
2023-02-28 14:03:58 -08:00
@Environment(\.appDatabase) private var appDatabase
2023-02-28 14:03:58 -08:00
2023-03-01 10:47:34 -08:00
@Query(SelectedRibbonRequest()) private var sr: [Ribbon]
2023-02-28 14:03:58 -08:00
@State var saveOffset = CGFloat()
var body: some View {
2023-02-28 14:03:58 -08:00
// ForEach(sr) { selectedRibbon in
2023-03-01 13:19:07 -08:00
Button(String(sr[0].id!) + " " + String(ribbon.id!) + " " + String(ribbon.scrollOffset),
2023-03-01 10:47:34 -08:00
// Button("meow",
action: {
Task {
var selectedRibbon = sr[0]
Print("SELECTED RIBBON", selectedRibbon)
2023-03-01 13:19:07 -08:00
var saveScrollOffset = scrollView!.contentOffset.y
var editedRibbon = selectedRibbon
editedRibbon.scrollOffset = Int(saveScrollOffset)
_ = try await appDatabase.saveRibbon(&editedRibbon)
_ = Print("editedRibbon", editedRibbon)
_ = Print("ribbon", ribbon)
2023-03-01 10:47:34 -08:00
2023-03-01 13:19:07 -08:00
Print("ribbon offset saved")
2023-03-01 10:47:34 -08:00
if (selectedRibbon.id != ribbon.id!) {
Print("switching")
book = ribbon.book
2023-03-01 13:19:07 -08:00
scrollView!.contentOffset.y = CGFloat(ribbon.scrollOffset)
2023-03-01 10:47:34 -08:00
// scrollOffset = CGFloat(1500)
var updateSelectRibbon = SelectedRibbon(id: Int64(1), ribbonId: ribbon.id!)
Print("Savingg")
Print(updateSelectRibbon)
do {
_ = try await appDatabase.saveSelectedRibbon(&updateSelectRibbon)
} catch {
Print("something wrong")
2023-02-28 14:03:58 -08:00
}
2023-03-01 10:47:34 -08:00
}
2023-02-28 14:03:58 -08:00
2023-03-01 10:47:34 -08:00
}
}
)
.buttonStyle(BlueButtonStyle())
}
// }
}
2023-02-28 14:03:58 -08:00
2023-03-01 10:47:34 -08:00
class ScrollViewHandler: NSObject {
}
2023-02-28 14:03:58 -08:00
2023-03-01 10:47:34 -08:00
extension ScrollViewHandler: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("delegate", scrollView.contentOffset.y)
}
}
2023-03-01 10:47:34 -08:00
struct ContentView: View {
@State var viewState = CGSize.zero
@State var pulledOut = CGSize.zero
@State var taskTitle : String = "FIRST DOGGG"
@State var curBook : String = "Matthew"
// @State var scrollTo1 : Int64?
@State var selectedLine : Int64?
@State var scrollOffset = CGFloat()
2023-02-28 14:03:58 -08:00
2023-03-01 10:47:34 -08:00
@State var thisScrollView : UIScrollView?
@State var initLoad = false
var scrollDelegate = ScrollViewHandler()
2023-02-28 14:03:58 -08:00
// @State var selectedRibbon: Ribbon!,
// @State var selectedRibbon: Ribbon!
// @State var selectedRibbonId = Int64(UserDefaults.standard.optionalInt(forKey: "selectedRibbonId") ?? 1)
2023-03-01 10:47:34 -08:00
// ribbon
@Query(SelectedRibbonRequest()) private var selectedRibbon: [Ribbon]
2023-02-28 14:03:58 -08:00
@State var endedDrag = true
@State var readOffset = CGPoint()
@Query(LineRequest(ordering: .byScore, book: "bible.john")) private var lines: [Line]
2023-02-28 14:03:58 -08:00
@Query(RibbonRequest()) private var ribbons: [Ribbon]
2023-02-28 14:03:58 -08:00
// @Query*RibbonRequest> private var selectedRibbon: [Ribbon]
// @Query(RibbonRequest(id: Int64(UserDefaults.standard.optionalInt(forKey: "lastRibbonId") ?? 1))) private var selectedRibbon: [Ribbon]
init() {
UITableView.appearance().backgroundColor = UIColor(Color(red: 0.2, green: 0.2, blue: 0.2))
2023-02-28 14:03:58 -08:00
// _selectedRibbon = Query(RibbonRequest(id: Int64(UserDefaults.standard.optionalInt(forKey: "lastRibbonId") ?? 1)))
}
var body: some View {
GeometryReader { geometry in
ZStack{
VStack{
2023-03-01 10:47:34 -08:00
Text("cat" + String(selectedRibbon[0].id!))
2023-02-28 14:03:58 -08:00
ForEach(ribbons) { ribbon in
2023-03-01 10:47:34 -08:00
//ForEach(selectedRibbon) { sr in
2023-02-28 14:03:58 -08:00
SwitchButton(ribbon: ribbon,
book: $lines.book,
2023-03-01 13:19:07 -08:00
scrollView:$thisScrollView
2023-02-28 14:03:58 -08:00
)
.buttonStyle(BlueButtonStyle())
2023-03-01 10:47:34 -08:00
//}
}
}
.frame(width: geometry.size.width, height: geometry.size.height, alignment: .topLeading)
.background(Color.red)
// VStack (spacing: 0){
ScrollViewReader { proxy in
//ScrollableView($offset, animationDuration: 0) {
2023-03-01 10:47:34 -08:00
// OffsetObservingScrollView(offset: $readOffset)
ScrollView
2023-02-28 14:03:58 -08:00
{
// List {
LazyVStack {
ForEach(lines) { line in
Text(String(line.body))
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
.font(Font.custom("AveriaSerifLibre-Regular", size: 30))
.listRowBackground(Color(red: 0.2, green: 0.8, blue: 0.2))
.foregroundColor(Color.white)
.listRowInsets(EdgeInsets())
.padding(EdgeInsets(top: 10, leading: 20, bottom: 40, trailing: 20))
.listRowSeparator(.hidden)
.id(line.id!)
.onTapGesture {
selectedLine = line.id!
//Print(selectedLine)
}
// }
}
}
.background(Color(red: 0.2, green: 0.2, blue: 0.2))
//.onChange(of: scrollTo1) { scrollTo in
// // scrollTo1 = 0
// let target = scrollTo
// //Print("hellosdf")
// //Print("value:", target)
// //Print("value2:", scrollTo)
// proxy.scrollTo(target, anchor: .top)
//}
}
.introspectScrollView { scrollView in
//let width = scrollView.contentSize.width - scrollView.frame.width
//Print(scrollView.contentOffset.x)
2023-03-01 10:47:34 -08:00
Print("introspect")
scrollView.delegate = scrollDelegate
if (thisScrollView == nil) {
thisScrollView = scrollView
2023-03-01 13:19:07 -08:00
scrollView.contentOffset.y = CGFloat(selectedRibbon[0].scrollOffset)
2023-02-28 14:03:58 -08:00
}
2023-03-01 10:47:34 -08:00
// scrollView.contentOffset.y = scrollOffset
// if (scrollOffset != scrollView.contentOffset.y) {
// scrollView.contentOffset.y = scrollOffset
// }
}
.listStyle(PlainListStyle())
}
// }
.background(Color(red: 0.2, green: 0.2, blue: 0.2))
.frame(width: geometry.size.width - 50)
.offset(x:30 , y:0)
.offset(x: pulledOut.width)
.offset(x: viewState.width, y: viewState.height)
.gesture(
DragGesture()
.onChanged { gesture in
2023-02-28 14:03:58 -08:00
if (endedDrag) {
endedDrag = false
scrollOffset = readOffset.y - 20
_ = Print("meow")
}
// logger.error("hello222")
// NSLog("hellooo")
//Print(viewState.width)
viewState.width = gesture.translation.width
//offset.y = gesture.translation.width
// logger.log("hello")
}
.onEnded { _ in
2023-02-28 14:03:58 -08:00
endedDrag = true
var pulledOutWidth = CGFloat(0)
if (viewState.width < 0) {
pulledOutWidth = CGFloat(0)
}
else if abs(viewState.width + pulledOut.width ) > 30 {
pulledOutWidth = CGFloat(200)
}
withAnimation(.spring(response: 0.2)) {
pulledOut.width = pulledOutWidth
viewState = .zero
}
}
)
}
}
}
}
private struct LineRow: View {
var line: Line
var body: some View {
HStack {
//Print("Here I am", line.body)
Text(line.body)
Spacer()
}
}
}
private let itemFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
return formatter
}()
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
ContentView().environment(\.appDatabase, .random())
}
}
extension View {
func Print(_ vars: Any...) -> some View {
for v in vars { print(v) }
return EmptyView()
}
}