seems to be working now
parent
d9ed0d2da9
commit
25d7d9b188
|
@ -66,7 +66,9 @@ public class VisibilityTracker<ID: Hashable>: ObservableObject {
|
||||||
|
|
||||||
bounds.width == 0 || bounds.height == 0
|
bounds.width == 0 || bounds.height == 0
|
||||||
|| bounds.origin.x == 0 || bounds.origin.y == 0
|
|| bounds.origin.x == 0 || bounds.origin.y == 0
|
||||||
|| abs(visibleViews[id]! ) > 500 ) {
|
|| abs(visibleViews[id]! ) > 500 )
|
||||||
|
|
||||||
|
{
|
||||||
visibleViews.removeValue(forKey: id)
|
visibleViews.removeValue(forKey: id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Binary file not shown.
|
@ -17,6 +17,7 @@ var currentId : String?
|
||||||
var currentOffset : CGFloat?
|
var currentOffset : CGFloat?
|
||||||
|
|
||||||
var gTracker: VisibilityTracker<String>?
|
var gTracker: VisibilityTracker<String>?
|
||||||
|
var printCount: Int64 = 0
|
||||||
|
|
||||||
// var curBook = "John"
|
// var curBook = "John"
|
||||||
|
|
||||||
|
@ -55,30 +56,26 @@ func goToRibbon(selectedRibbon: Ribbon,
|
||||||
scrollId: Binding<String?>,
|
scrollId: Binding<String?>,
|
||||||
scrollOffset: Binding<CGFloat?>,
|
scrollOffset: Binding<CGFloat?>,
|
||||||
refresh: Binding<Bool>,
|
refresh: Binding<Bool>,
|
||||||
appDatabase: AppDatabase
|
appDatabase: AppDatabase,
|
||||||
) {
|
loading: Bool
|
||||||
|
)
|
||||||
|
{
|
||||||
|
|
||||||
Task {
|
Task {
|
||||||
// @Binding var scrollId : String?
|
|
||||||
// @Binding var scrollOffset : CGFloat?
|
|
||||||
// @Binding var refresh : Bool
|
|
||||||
// print("SELECTED RIBBON", selectedRibbon)
|
// print("SELECTED RIBBON", selectedRibbon)
|
||||||
let userDefaults = UserDefaults.standard
|
|
||||||
var scrollOffsetToSave = currentOffset
|
var scrollOffsetToSave = currentOffset
|
||||||
var scrollIdToSave = currentId
|
var scrollIdToSave = currentId
|
||||||
|
|
||||||
// Print("scrollOffsetToSave: ", scrollOffsetToSave)
|
|
||||||
// Print("scrollIdToSave: ", scrollIdToSave)
|
|
||||||
|
|
||||||
var updatedRibbon = selectedRibbon
|
var updatedRibbon = selectedRibbon
|
||||||
|
|
||||||
// scrollId = "10"
|
if (selectedRibbon.id != destRibbon.id! || loading) {
|
||||||
// scrollOffset = CGFloat(100)
|
|
||||||
// refresh.toggle()
|
|
||||||
|
|
||||||
if (selectedRibbon.id != destRibbon.id!) {
|
|
||||||
print("switching ribbons")
|
print("switching ribbons")
|
||||||
|
|
||||||
|
if (loading) {
|
||||||
|
currentId = destRibbon.scrollId
|
||||||
|
// currentOffset = CGFloat(destRibbon.scrollOffset)
|
||||||
|
}
|
||||||
|
|
||||||
scrollId.wrappedValue = destRibbon.scrollId
|
scrollId.wrappedValue = destRibbon.scrollId
|
||||||
// print("setting scroll offset")
|
// print("setting scroll offset")
|
||||||
scrollOffset.wrappedValue = CGFloat(destRibbon.scrollOffset)
|
scrollOffset.wrappedValue = CGFloat(destRibbon.scrollOffset)
|
||||||
|
@ -86,11 +83,6 @@ func goToRibbon(selectedRibbon: Ribbon,
|
||||||
// print("end setting scroll offset")
|
// print("end setting scroll offset")
|
||||||
refresh.wrappedValue.toggle()
|
refresh.wrappedValue.toggle()
|
||||||
|
|
||||||
//scrollId = "10"
|
|
||||||
//scrollOffset = CGFloat(100)
|
|
||||||
|
|
||||||
userDefaults.set(scrollId.wrappedValue, forKey: "currentId")
|
|
||||||
userDefaults.set(scrollOffset.wrappedValue, forKey: "currentOffset")
|
|
||||||
|
|
||||||
var updateSelectRibbon = SelectedRibbon(id: Int64(1), ribbonId: destRibbon.id!)
|
var updateSelectRibbon = SelectedRibbon(id: Int64(1), ribbonId: destRibbon.id!)
|
||||||
// print("Saving selected ribbon")
|
// print("Saving selected ribbon")
|
||||||
|
@ -102,15 +94,20 @@ func goToRibbon(selectedRibbon: Ribbon,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updatedRibbon.scrollOffset = Int(scrollOffsetToSave!)
|
if (!loading) {
|
||||||
updatedRibbon.scrollId = scrollIdToSave!
|
print("not loading")
|
||||||
|
updatedRibbon.scrollOffset = Int(scrollOffsetToSave!)
|
||||||
|
updatedRibbon.scrollId = scrollIdToSave!
|
||||||
|
|
||||||
_ = try await appDatabase.saveRibbon(&updatedRibbon)
|
_ = try await appDatabase.saveRibbon(&updatedRibbon)
|
||||||
// print("saved updatedRibbon", updatedRibbon)
|
// print("saved updatedRibbon", updatedRibbon)
|
||||||
|
|
||||||
// print("UPDATED")
|
// print("UPDATED")
|
||||||
scrollOffsetToSave = userDefaults.object(forKey: "currentOffset") as? CGFloat
|
// scrollOffsetToSave = userDefaults.object(forKey: "currentOffset") as? CGFloat
|
||||||
scrollIdToSave = userDefaults.object(forKey: "currentId") as? String
|
// scrollIdToSave = userDefaults.object(forKey: "currentId") as? String
|
||||||
|
} else {
|
||||||
|
print("loading")
|
||||||
|
}
|
||||||
|
|
||||||
// print("scrollOffsetToSave: ", scrollOffsetToSave)
|
// print("scrollOffsetToSave: ", scrollOffsetToSave)
|
||||||
// print("scrollIdToSave: ", scrollIdToSave)
|
// print("scrollIdToSave: ", scrollIdToSave)
|
||||||
|
@ -140,7 +137,8 @@ struct SwitchButton : View {
|
||||||
scrollId: $scrollId,
|
scrollId: $scrollId,
|
||||||
scrollOffset: $scrollOffset,
|
scrollOffset: $scrollOffset,
|
||||||
refresh: $refresh,
|
refresh: $refresh,
|
||||||
appDatabase: appDatabase)
|
appDatabase: appDatabase,
|
||||||
|
loading: false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,6 +188,7 @@ func makeVerseView(seg: SegDenorm) -> some View {
|
||||||
|
|
||||||
private struct SegRow: View {
|
private struct SegRow: View {
|
||||||
var seg: SegDenorm
|
var seg: SegDenorm
|
||||||
|
var ribbonId: Int64
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
// makeVerseView(seg: seg)
|
// makeVerseView(seg: seg)
|
||||||
|
@ -214,7 +213,7 @@ private struct SegRow: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
retView = retView + Text(String(verse.verse))
|
retView = retView + Text(String(verse.verse)) + Text(String(ribbonId))
|
||||||
//retView = retView + Text(attributedString)
|
//retView = retView + Text(attributedString)
|
||||||
// Text(seg.body)
|
// Text(seg.body)
|
||||||
// .contentShape(Rectangle())
|
// .contentShape(Rectangle())
|
||||||
|
@ -277,6 +276,7 @@ struct ContentView: View {
|
||||||
@State var setScrollOffset: CGFloat?
|
@State var setScrollOffset: CGFloat?
|
||||||
|
|
||||||
|
|
||||||
|
@Environment(\.appDatabase) private var appDatabase
|
||||||
@Query(SegDenormRequest(book: "bible.mark")) private var segs: [SegDenorm]
|
@Query(SegDenormRequest(book: "bible.mark")) private var segs: [SegDenorm]
|
||||||
|
|
||||||
// @State var scrollDelegate: ScrollViewHandler
|
// @State var scrollDelegate: ScrollViewHandler
|
||||||
|
@ -285,23 +285,27 @@ struct ContentView: View {
|
||||||
// @State var selectedRibbonId = Int64(UserDefaults.standard.optionalInt(forKey: "selectedRibbonId") ?? 1)
|
// @State var selectedRibbonId = Int64(UserDefaults.standard.optionalInt(forKey: "selectedRibbonId") ?? 1)
|
||||||
|
|
||||||
// ribbon
|
// ribbon
|
||||||
@Query(SelectedRibbonRequest()) private var selectedRibbon: [Ribbon]
|
// @Query(SelectedRibbonRequest()) private var selectedRibbon: [Ribbon]
|
||||||
|
|
||||||
@State var endedDrag = true
|
@State var endedDrag = true
|
||||||
|
|
||||||
@State var readOffset = CGPoint()
|
@State var readOffset = CGPoint()
|
||||||
|
|
||||||
@State var refresh: Bool = false
|
@State var refresh: Bool = false
|
||||||
|
|
||||||
@State var refresh2: Bool = false
|
@State var refresh2: Bool = false
|
||||||
|
|
||||||
|
|
||||||
@Query(RibbonRequest()) private var ribbons: [Ribbon]
|
@Query(RibbonRequest()) private var ribbons: [Ribbon]
|
||||||
|
@Query<SelectedRibbonRequest> var selectedRibbon: [Ribbon]
|
||||||
|
|
||||||
// @Query(RibbonRequest(id: Int64(UserDefaults.standard.optionalInt(forKey: "lastRibbonId") ?? 1))) private var selectedRibbon: [Ribbon]
|
// @Query(RibbonRequest(id: Int64(UserDefaults.standard.optionalInt(forKey: "lastRibbonId") ?? 1))) private var selectedRibbon: [Ribbon]
|
||||||
|
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
UITableView.appearance().backgroundColor = UIColor(Color(red: 0.2, green: 0.2, blue: 0.2))
|
UITableView.appearance().backgroundColor = UIColor(Color(red: 0.2, green: 0.2, blue: 0.2))
|
||||||
// _selectedRibbon = Query(RibbonRequest(id: Int64(UserDefaults.standard.optionalInt(forKey: "lastRibbonId") ?? 1)))
|
_selectedRibbon = Query(SelectedRibbonRequest())
|
||||||
|
|
||||||
// self._scrollDelegate = State(initialValue: ScrollViewHandler())
|
// self._scrollDelegate = State(initialValue: ScrollViewHandler())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,7 +316,7 @@ struct ContentView: View {
|
||||||
var size2 = CGFloat(120)
|
var size2 = CGFloat(120)
|
||||||
var fontSize = CGFloat(20)
|
var fontSize = CGFloat(20)
|
||||||
GeometryReader { geometry in
|
GeometryReader { geometry in
|
||||||
ZStack{
|
ZStack {
|
||||||
VStack{
|
VStack{
|
||||||
Text("MK")
|
Text("MK")
|
||||||
.font(Font.custom("AveriaSerifLibre-Regular", size: fontSize))
|
.font(Font.custom("AveriaSerifLibre-Regular", size: fontSize))
|
||||||
|
@ -371,7 +375,9 @@ struct ContentView: View {
|
||||||
|
|
||||||
}
|
}
|
||||||
ForEach(segs) { seg in
|
ForEach(segs) { seg in
|
||||||
SegRow(seg: seg)
|
SegRow(seg: seg,
|
||||||
|
ribbonId: selectedRibbon[0].id!
|
||||||
|
)
|
||||||
.id("\(seg.id)")
|
.id("\(seg.id)")
|
||||||
|
|
||||||
.padding(EdgeInsets(top: 10, leading: 20, bottom: 40, trailing: 20))
|
.padding(EdgeInsets(top: 10, leading: 20, bottom: 40, trailing: 20))
|
||||||
|
@ -390,6 +396,23 @@ struct ContentView: View {
|
||||||
}
|
}
|
||||||
.background(Color(red: 0.2, green: 0.2, blue: 0.2))
|
.background(Color(red: 0.2, green: 0.2, blue: 0.2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.onAppear() {
|
||||||
|
Print("APPEAR")
|
||||||
|
// Print(selectedRibbon[0])
|
||||||
|
// scrollId = "3"
|
||||||
|
// scrollOffset = 103
|
||||||
|
// refresh.toggle()
|
||||||
|
|
||||||
|
goToRibbon(selectedRibbon: selectedRibbon[0],
|
||||||
|
destRibbon: selectedRibbon[0],
|
||||||
|
scrollId: $scrollId,
|
||||||
|
scrollOffset: $scrollOffset,
|
||||||
|
refresh: $refresh,
|
||||||
|
appDatabase: appDatabase,
|
||||||
|
loading: true)
|
||||||
|
|
||||||
|
}
|
||||||
.onChange(of: refresh) { target in
|
.onChange(of: refresh) { target in
|
||||||
//if let target = target {
|
//if let target = target {
|
||||||
//gTracker!.visibleViews["123123"] = CGFloat(100)
|
//gTracker!.visibleViews["123123"] = CGFloat(100)
|
||||||
|
@ -400,27 +423,68 @@ struct ContentView: View {
|
||||||
Print(scrollId)
|
Print(scrollId)
|
||||||
proxy.scrollTo(scrollId! , anchor: .top)
|
proxy.scrollTo(scrollId! , anchor: .top)
|
||||||
currentId = scrollId!
|
currentId = scrollId!
|
||||||
|
Print("removing", gTracker!.visibleViews.removeAll())
|
||||||
|
|
||||||
Task {
|
Task {
|
||||||
// try? await Task.sleep(nanoseconds: 1_000_000_000)
|
// try? await Task.sleep(nanoseconds: 1_000_000_000)
|
||||||
|
|
||||||
|
|
||||||
// try? await Task.sleep(nanoseconds: 1_000_000_000)
|
|
||||||
proxy.scrollTo(scrollId! , anchor: .top)
|
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
// setScrollOffset = CGFloat(scrollOffset!)
|
// if (currentId != scrollId!) {
|
||||||
setScrollOffset = CGFloat(Int(currentOffset!) * -1 + Int(scrollOffset!))
|
if (true) {
|
||||||
currentOffset = scrollOffset!
|
DispatchQueue.main.async {
|
||||||
|
|
||||||
refresh2.toggle()
|
|
||||||
|
|
||||||
|
Print("cat MEOW MEOW MEOW")
|
||||||
|
var newJump = String(Int(scrollId!)! + 1)
|
||||||
|
|
||||||
|
Print("cat new jump", newJump)
|
||||||
|
proxy.scrollTo(newJump, anchor: .top)
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
|
||||||
|
|
||||||
|
var newJump2 = String(Int(scrollId!)!)
|
||||||
|
Print("cat correct jump2", newJump2)
|
||||||
|
proxy.scrollTo(newJump2, anchor: .top)
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
|
||||||
|
|
||||||
|
Print(" scroll offset target", scrollOffset)
|
||||||
|
Print(" current offset ", currentOffset)
|
||||||
|
// setScrollOffset = CGFloat(Int(currentOffset!) * -1 + Int(scrollOffset!))
|
||||||
|
// setScrollOffset = CGFloat(Int(scrollOffset!) - Int(currentOffset!))
|
||||||
|
setScrollOffset = CGFloat(Int(scrollOffset!))
|
||||||
|
Print("setting scroll offset", setScrollOffset)
|
||||||
|
currentOffset = scrollOffset!
|
||||||
|
refresh2.toggle()
|
||||||
|
// currentId = scrollId!
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
currentId = scrollId!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Print(" scroll offset target", scrollOffset)
|
||||||
|
Print(" current offset ", currentOffset)
|
||||||
|
// setScrollOffset = CGFloat(Int(currentOffset!) * -1 + Int(scrollOffset!))
|
||||||
|
setScrollOffset = CGFloat(Int(scrollOffset!) - Int(currentOffset!))
|
||||||
|
Print("setting scroll offset", setScrollOffset)
|
||||||
|
currentOffset = scrollOffset!
|
||||||
|
refresh2.toggle()
|
||||||
|
// currentId = scrollId!
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
currentId = scrollId!
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
.introspectScrollView { scrollView in
|
.introspectScrollView { scrollView in
|
||||||
// Print("introspect")
|
Print("introspect")
|
||||||
// scrollView.delegate = scrollDelegate
|
// scrollView.delegate = scrollDelegate
|
||||||
//Print("Scroll delegate offset", scrollDelegate.scrollOffset)
|
//Print("Scroll delegate offset", scrollDelegate.scrollOffset)
|
||||||
|
|
||||||
|
@ -435,6 +499,7 @@ struct ContentView: View {
|
||||||
// thisScrollView = scrollView
|
// thisScrollView = scrollView
|
||||||
// scrollView.contentOffset.y = CGFloat(selectedRibbon[0].scrollOffset)
|
// scrollView.contentOffset.y = CGFloat(selectedRibbon[0].scrollOffset)
|
||||||
// }
|
// }
|
||||||
|
Print("end instrospect")
|
||||||
}
|
}
|
||||||
.listStyle(PlainListStyle())
|
.listStyle(PlainListStyle())
|
||||||
}
|
}
|
||||||
|
@ -484,16 +549,22 @@ struct ContentView: View {
|
||||||
func handleVisibilityChanged(_ id: String, change: VisibilityChange, tracker: VisibilityTracker<String>) {
|
func handleVisibilityChanged(_ id: String, change: VisibilityChange, tracker: VisibilityTracker<String>) {
|
||||||
|
|
||||||
|
|
||||||
|
var printRate: Int64 = 10
|
||||||
|
gTracker = tracker
|
||||||
|
|
||||||
// @Environment(\.appDatabase) var appDatabase
|
// @Environment(\.appDatabase) var appDatabase
|
||||||
// switch change {
|
// switch change {
|
||||||
// case .shown: print("\(id) shown")
|
// case .shown: print("\(id) shown")
|
||||||
// case .hidden: print("\(id) hidden")
|
// case .hidden: print("\(id) hidden")
|
||||||
// }
|
// }
|
||||||
// print("VISIBILITY CHANGED STARTED")
|
if (printCount % printRate == 0) {
|
||||||
// print(tracker.visibleViews)
|
print("VISIBILITY CHANGED STARTED")
|
||||||
// gTracker = tracker
|
print(tracker.visibleViews)
|
||||||
// print(tracker.sortedViewIDs)
|
print(tracker.sortedViewIDs)
|
||||||
// print("VISIBILITY CHANGED ENDED")
|
print("VISIBILITY CHANGED ENDED")
|
||||||
|
}
|
||||||
|
printCount += 1
|
||||||
|
|
||||||
// if (currentId != nil) {
|
// if (currentId != nil) {
|
||||||
// currentOffset = tracker.visibleViews[currentId!]!
|
// currentOffset = tracker.visibleViews[currentId!]!
|
||||||
// }
|
// }
|
||||||
|
@ -506,35 +577,13 @@ struct ContentView: View {
|
||||||
|
|
||||||
currentId = tracker.sortedViewIDs[0]
|
currentId = tracker.sortedViewIDs[0]
|
||||||
if (currentId != nil) {
|
if (currentId != nil) {
|
||||||
print("current ID:", currentId)
|
if (printCount % printRate == 0) {
|
||||||
|
print(printCount)
|
||||||
// Access Shared Defaults Object
|
print("cat current ID:", currentId)
|
||||||
// let userDefaults = UserDefaults.standard
|
}
|
||||||
|
print("got here")
|
||||||
// Write/Set Value
|
|
||||||
// Print(currentId!)
|
|
||||||
// Print(tracker.visibleViews[currentId?]!)
|
|
||||||
// userDefaults.set(currentId, forKey: "currentId")
|
|
||||||
// userDefaults.set(tracker.visibleViews[currentId]!, forKey: "currentOffset")
|
|
||||||
currentOffset = tracker.visibleViews[currentId!]!
|
currentOffset = tracker.visibleViews[currentId!]!
|
||||||
// userDefaults.set(tracker.visibleViews[currentId!]!, forKey: "currentOffset")
|
|
||||||
// var updateScrollState = ScrollState(id: Int64(1),
|
|
||||||
// scrollId: currentId!,
|
|
||||||
// scrollOffset: Int64(tracker.visibleViews[currentId!]!))
|
|
||||||
// Print("Savingg")
|
|
||||||
//Print(updateScrollState)
|
|
||||||
// Task(priority: .default) {
|
|
||||||
|
|
||||||
// do {
|
|
||||||
// _ = try await appDatabase.saveScrollState(&updateScrollState)
|
|
||||||
|
|
||||||
// } catch {
|
|
||||||
// Print("something wrong")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// gTracker!.visibleViews.removeAll()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue