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.origin.x == 0 || bounds.origin.y == 0
|
||||
|| abs(visibleViews[id]! ) > 500 ) {
|
||||
|| abs(visibleViews[id]! ) > 500 )
|
||||
|
||||
{
|
||||
visibleViews.removeValue(forKey: id)
|
||||
}
|
||||
|
||||
|
|
Binary file not shown.
|
@ -17,6 +17,7 @@ var currentId : String?
|
|||
var currentOffset : CGFloat?
|
||||
|
||||
var gTracker: VisibilityTracker<String>?
|
||||
var printCount: Int64 = 0
|
||||
|
||||
// var curBook = "John"
|
||||
|
||||
|
@ -55,30 +56,26 @@ func goToRibbon(selectedRibbon: Ribbon,
|
|||
scrollId: Binding<String?>,
|
||||
scrollOffset: Binding<CGFloat?>,
|
||||
refresh: Binding<Bool>,
|
||||
appDatabase: AppDatabase
|
||||
) {
|
||||
appDatabase: AppDatabase,
|
||||
loading: Bool
|
||||
)
|
||||
{
|
||||
|
||||
Task {
|
||||
// @Binding var scrollId : String?
|
||||
// @Binding var scrollOffset : CGFloat?
|
||||
// @Binding var refresh : Bool
|
||||
// print("SELECTED RIBBON", selectedRibbon)
|
||||
let userDefaults = UserDefaults.standard
|
||||
var scrollOffsetToSave = currentOffset
|
||||
var scrollIdToSave = currentId
|
||||
|
||||
// Print("scrollOffsetToSave: ", scrollOffsetToSave)
|
||||
// Print("scrollIdToSave: ", scrollIdToSave)
|
||||
|
||||
var updatedRibbon = selectedRibbon
|
||||
|
||||
// scrollId = "10"
|
||||
// scrollOffset = CGFloat(100)
|
||||
// refresh.toggle()
|
||||
|
||||
if (selectedRibbon.id != destRibbon.id!) {
|
||||
if (selectedRibbon.id != destRibbon.id! || loading) {
|
||||
print("switching ribbons")
|
||||
|
||||
if (loading) {
|
||||
currentId = destRibbon.scrollId
|
||||
// currentOffset = CGFloat(destRibbon.scrollOffset)
|
||||
}
|
||||
|
||||
scrollId.wrappedValue = destRibbon.scrollId
|
||||
// print("setting scroll offset")
|
||||
scrollOffset.wrappedValue = CGFloat(destRibbon.scrollOffset)
|
||||
|
@ -86,11 +83,6 @@ func goToRibbon(selectedRibbon: Ribbon,
|
|||
// print("end setting scroll offset")
|
||||
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!)
|
||||
// print("Saving selected ribbon")
|
||||
|
@ -102,6 +94,8 @@ func goToRibbon(selectedRibbon: Ribbon,
|
|||
}
|
||||
}
|
||||
|
||||
if (!loading) {
|
||||
print("not loading")
|
||||
updatedRibbon.scrollOffset = Int(scrollOffsetToSave!)
|
||||
updatedRibbon.scrollId = scrollIdToSave!
|
||||
|
||||
|
@ -109,8 +103,11 @@ func goToRibbon(selectedRibbon: Ribbon,
|
|||
// print("saved updatedRibbon", updatedRibbon)
|
||||
|
||||
// print("UPDATED")
|
||||
scrollOffsetToSave = userDefaults.object(forKey: "currentOffset") as? CGFloat
|
||||
scrollIdToSave = userDefaults.object(forKey: "currentId") as? String
|
||||
// scrollOffsetToSave = userDefaults.object(forKey: "currentOffset") as? CGFloat
|
||||
// scrollIdToSave = userDefaults.object(forKey: "currentId") as? String
|
||||
} else {
|
||||
print("loading")
|
||||
}
|
||||
|
||||
// print("scrollOffsetToSave: ", scrollOffsetToSave)
|
||||
// print("scrollIdToSave: ", scrollIdToSave)
|
||||
|
@ -140,7 +137,8 @@ struct SwitchButton : View {
|
|||
scrollId: $scrollId,
|
||||
scrollOffset: $scrollOffset,
|
||||
refresh: $refresh,
|
||||
appDatabase: appDatabase)
|
||||
appDatabase: appDatabase,
|
||||
loading: false)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -190,6 +188,7 @@ func makeVerseView(seg: SegDenorm) -> some View {
|
|||
|
||||
private struct SegRow: View {
|
||||
var seg: SegDenorm
|
||||
var ribbonId: Int64
|
||||
|
||||
var body: some View {
|
||||
// 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)
|
||||
// Text(seg.body)
|
||||
// .contentShape(Rectangle())
|
||||
|
@ -277,6 +276,7 @@ struct ContentView: View {
|
|||
@State var setScrollOffset: CGFloat?
|
||||
|
||||
|
||||
@Environment(\.appDatabase) private var appDatabase
|
||||
@Query(SegDenormRequest(book: "bible.mark")) private var segs: [SegDenorm]
|
||||
|
||||
// @State var scrollDelegate: ScrollViewHandler
|
||||
|
@ -285,23 +285,27 @@ struct ContentView: View {
|
|||
// @State var selectedRibbonId = Int64(UserDefaults.standard.optionalInt(forKey: "selectedRibbonId") ?? 1)
|
||||
|
||||
// ribbon
|
||||
@Query(SelectedRibbonRequest()) private var selectedRibbon: [Ribbon]
|
||||
// @Query(SelectedRibbonRequest()) private var selectedRibbon: [Ribbon]
|
||||
|
||||
@State var endedDrag = true
|
||||
|
||||
@State var readOffset = CGPoint()
|
||||
|
||||
@State var refresh: Bool = false
|
||||
|
||||
@State var refresh2: Bool = false
|
||||
|
||||
|
||||
@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]
|
||||
|
||||
|
||||
init() {
|
||||
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())
|
||||
}
|
||||
|
||||
|
@ -371,7 +375,9 @@ struct ContentView: View {
|
|||
|
||||
}
|
||||
ForEach(segs) { seg in
|
||||
SegRow(seg: seg)
|
||||
SegRow(seg: seg,
|
||||
ribbonId: selectedRibbon[0].id!
|
||||
)
|
||||
.id("\(seg.id)")
|
||||
|
||||
.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))
|
||||
}
|
||||
|
||||
.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
|
||||
//if let target = target {
|
||||
//gTracker!.visibleViews["123123"] = CGFloat(100)
|
||||
|
@ -400,27 +423,68 @@ struct ContentView: View {
|
|||
Print(scrollId)
|
||||
proxy.scrollTo(scrollId! , anchor: .top)
|
||||
currentId = scrollId!
|
||||
Print("removing", gTracker!.visibleViews.removeAll())
|
||||
|
||||
Task {
|
||||
// try? await Task.sleep(nanoseconds: 1_000_000_000)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
// if (currentId != scrollId!) {
|
||||
if (true) {
|
||||
DispatchQueue.main.async {
|
||||
|
||||
// try? await Task.sleep(nanoseconds: 1_000_000_000)
|
||||
proxy.scrollTo(scrollId! , anchor: .top)
|
||||
|
||||
Print("cat MEOW MEOW MEOW")
|
||||
var newJump = String(Int(scrollId!)! + 1)
|
||||
|
||||
Print("cat new jump", newJump)
|
||||
proxy.scrollTo(newJump, anchor: .top)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
// setScrollOffset = CGFloat(scrollOffset!)
|
||||
setScrollOffset = CGFloat(Int(currentOffset!) * -1 + Int(scrollOffset!))
|
||||
|
||||
|
||||
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
|
||||
// Print("introspect")
|
||||
Print("introspect")
|
||||
// scrollView.delegate = scrollDelegate
|
||||
//Print("Scroll delegate offset", scrollDelegate.scrollOffset)
|
||||
|
||||
|
@ -435,6 +499,7 @@ struct ContentView: View {
|
|||
// thisScrollView = scrollView
|
||||
// scrollView.contentOffset.y = CGFloat(selectedRibbon[0].scrollOffset)
|
||||
// }
|
||||
Print("end instrospect")
|
||||
}
|
||||
.listStyle(PlainListStyle())
|
||||
}
|
||||
|
@ -484,16 +549,22 @@ struct ContentView: View {
|
|||
func handleVisibilityChanged(_ id: String, change: VisibilityChange, tracker: VisibilityTracker<String>) {
|
||||
|
||||
|
||||
var printRate: Int64 = 10
|
||||
gTracker = tracker
|
||||
|
||||
// @Environment(\.appDatabase) var appDatabase
|
||||
// switch change {
|
||||
// case .shown: print("\(id) shown")
|
||||
// case .hidden: print("\(id) hidden")
|
||||
// }
|
||||
// print("VISIBILITY CHANGED STARTED")
|
||||
// print(tracker.visibleViews)
|
||||
// gTracker = tracker
|
||||
// print(tracker.sortedViewIDs)
|
||||
// print("VISIBILITY CHANGED ENDED")
|
||||
if (printCount % printRate == 0) {
|
||||
print("VISIBILITY CHANGED STARTED")
|
||||
print(tracker.visibleViews)
|
||||
print(tracker.sortedViewIDs)
|
||||
print("VISIBILITY CHANGED ENDED")
|
||||
}
|
||||
printCount += 1
|
||||
|
||||
// if (currentId != nil) {
|
||||
// currentOffset = tracker.visibleViews[currentId!]!
|
||||
// }
|
||||
|
@ -506,35 +577,13 @@ struct ContentView: View {
|
|||
|
||||
currentId = tracker.sortedViewIDs[0]
|
||||
if (currentId != nil) {
|
||||
print("current ID:", currentId)
|
||||
|
||||
// Access Shared Defaults Object
|
||||
// let userDefaults = UserDefaults.standard
|
||||
|
||||
// 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!]!
|
||||
// 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")
|
||||
// }
|
||||
// }
|
||||
if (printCount % printRate == 0) {
|
||||
print(printCount)
|
||||
print("cat current ID:", currentId)
|
||||
}
|
||||
print("got here")
|
||||
currentOffset = tracker.visibleViews[currentId!]!
|
||||
}
|
||||
|
||||
// gTracker!.visibleViews.removeAll()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue