gloss-ios/gloss/ContentView.swift

232 lines
7.9 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"
extension UINavigationController {
override open func viewDidLoad() {
super.viewDidLoad()
let standard = UINavigationBarAppearance()
standard.backgroundColor = .red //When you scroll or you have title (small one)
let compact = UINavigationBarAppearance()
compact.backgroundColor = .green //compact-height
let scrollEdge = UINavigationBarAppearance()
scrollEdge.backgroundColor = .blue //When you have large title
navigationBar.standardAppearance = standard
navigationBar.compactAppearance = compact
navigationBar.scrollEdgeAppearance = scrollEdge
}
}
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 {
var ribbon: Ribbon
@Binding var selectedRibbon : Ribbon?
@Binding var book : String
@Binding var scrollOffset : CGFloat
var body: some View {
Button("meow") {
book = ribbon.book
selectedRibbon = ribbon
Print(ribbon.scrollOffset)
Print(scrollOffset)
scrollOffset = CGFloat(ribbon.scrollOffset)
//Print("hellosdf dddd")
}
.buttonStyle(BlueButtonStyle())
}
}
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()
@State var selectedRibbon: Ribbon!
@Query(LineRequest(ordering: .byScore, book: "bible.john")) private var lines: [Line]
@Query(RibbonRequest()) private var ribbons: [Ribbon]
// @Query<LineRequest> private var lines: [Line]
init() {
UITableView.appearance().backgroundColor = UIColor(Color(red: 0.2, green: 0.2, blue: 0.2))
// _lines = Query(LineRequest(ordering: .byScore, book: curBook))
}
// @Environment(\.managedObjectContext) private var viewContext
var body: some View {
GeometryReader { geometry in
ZStack{
VStack{
ForEach(ribbons) { ribbon in
SwitchButton(ribbon: ribbon,
selectedRibbon: $selectedRibbon,
book: $lines.book,
scrollOffset: $scrollOffset)
.buttonStyle(BlueButtonStyle())
}
}
.frame(width: geometry.size.width, height: geometry.size.height, alignment: .topLeading)
.background(Color.red)
// VStack (spacing: 0){
ScrollViewReader { proxy in
//ScrollableView($offset, animationDuration: 0) {
ScrollView {
// 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)
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)
.onAppear {
if (selectedRibbon != nil) {
Print(selectedRibbon)
//scrollOffset.y = scrollOffset
}
}
.gesture(
DragGesture()
.onChanged { gesture in
// logger.error("hello222")
// NSLog("hellooo")
//Print(viewState.width)
viewState.width = gesture.translation.width
//offset.y = gesture.translation.width
// logger.log("hello")
}
.onEnded { _ in
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()
}
}