diff --git a/VisibilityTracker.swift b/VisibilityTracker.swift index 22c129d..c2b9a9c 100644 --- a/VisibilityTracker.swift +++ b/VisibilityTracker.swift @@ -66,7 +66,9 @@ public class VisibilityTracker: 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) } diff --git a/gloss.xcodeproj/project.xcworkspace/xcuserdata/saint.xcuserdatad/UserInterfaceState.xcuserstate b/gloss.xcodeproj/project.xcworkspace/xcuserdata/saint.xcuserdatad/UserInterfaceState.xcuserstate index 5201292..395f668 100644 Binary files a/gloss.xcodeproj/project.xcworkspace/xcuserdata/saint.xcuserdatad/UserInterfaceState.xcuserstate and b/gloss.xcodeproj/project.xcworkspace/xcuserdata/saint.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/gloss/ContentView.swift b/gloss/ContentView.swift index 9568fba..302719f 100644 --- a/gloss/ContentView.swift +++ b/gloss/ContentView.swift @@ -17,6 +17,7 @@ var currentId : String? var currentOffset : CGFloat? var gTracker: VisibilityTracker? +var printCount: Int64 = 0 // var curBook = "John" @@ -55,30 +56,26 @@ func goToRibbon(selectedRibbon: Ribbon, scrollId: Binding, scrollOffset: Binding, refresh: Binding, - appDatabase: AppDatabase - ) { + appDatabase: AppDatabase, + loading: Bool + ) +{ - Task { - // @Binding var scrollId : String? - // @Binding var scrollOffset : CGFloat? - // @Binding var refresh : Bool + Task { // 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,15 +94,20 @@ func goToRibbon(selectedRibbon: Ribbon, } } - updatedRibbon.scrollOffset = Int(scrollOffsetToSave!) - updatedRibbon.scrollId = scrollIdToSave! + if (!loading) { + print("not loading") + updatedRibbon.scrollOffset = Int(scrollOffsetToSave!) + updatedRibbon.scrollId = scrollIdToSave! - _ = try await appDatabase.saveRibbon(&updatedRibbon) - // print("saved updatedRibbon", updatedRibbon) + _ = try await appDatabase.saveRibbon(&updatedRibbon) + // print("saved updatedRibbon", updatedRibbon) - // print("UPDATED") - scrollOffsetToSave = userDefaults.object(forKey: "currentOffset") as? CGFloat - scrollIdToSave = userDefaults.object(forKey: "currentId") as? String + // print("UPDATED") + // 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 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()) } @@ -312,7 +316,7 @@ struct ContentView: View { var size2 = CGFloat(120) var fontSize = CGFloat(20) GeometryReader { geometry in - ZStack{ + ZStack { VStack{ Text("MK") .font(Font.custom("AveriaSerifLibre-Regular", size: fontSize)) @@ -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) - - // try? await Task.sleep(nanoseconds: 1_000_000_000) - proxy.scrollTo(scrollId! , anchor: .top) - DispatchQueue.main.async { - // setScrollOffset = CGFloat(scrollOffset!) - setScrollOffset = CGFloat(Int(currentOffset!) * -1 + Int(scrollOffset!)) - currentOffset = scrollOffset! + // if (currentId != scrollId!) { + if (true) { + 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 - // 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) { + 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") + if (printCount % printRate == 0) { + print(printCount) + print("cat current ID:", currentId) + } + print("got here") 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() } }