scroll working more
parent
65563f84fc
commit
111c6e395d
|
@ -23,13 +23,9 @@ struct SelectedRibbonRequest: Queryable {
|
|||
|
||||
/// The ordering used by the player request.
|
||||
// var ordering: Ordering
|
||||
var id: Int64!
|
||||
static var defaultValue: [Ribbon] { [] }
|
||||
|
||||
// MARK: - Queryable Implementation
|
||||
|
||||
static var defaultValue: [SelectedRibbon] { [] }
|
||||
|
||||
func publisher(in appDatabase: AppDatabase) -> AnyPublisher<[SelectedRibbon], Error> {
|
||||
func publisher(in appDatabase: AppDatabase) -> AnyPublisher<[Ribbon], Error> {
|
||||
// Build the publisher from the general-purpose read-only access
|
||||
// granted by `appDatabase.reader`.
|
||||
// Some apps will prefer to call a dedicated method of `appDatabase`.
|
||||
|
@ -45,24 +41,14 @@ struct SelectedRibbonRequest: Queryable {
|
|||
}
|
||||
|
||||
// This method is not required by Queryable, but it makes it easier
|
||||
func fetchValue(_ db: Database) throws -> [SelectedRibbon] {
|
||||
if (id == nil) {
|
||||
return try SelectedRibbon.fetchAll(db)
|
||||
} else {
|
||||
return try SelectedRibbon.filter(idColumn == id).fetchAll(db)
|
||||
}
|
||||
func fetchValue(_ db: Database) throws -> [Ribbon] {
|
||||
|
||||
// if book == "" {
|
||||
// return try SelectedRibbon.filter(bookColumn == SelectedRibbon.randomBook()).fetchAll(db)
|
||||
// } else {
|
||||
// return try SelectedRibbon.filter(bookColumn == book).fetchAll(db)
|
||||
// }
|
||||
// switch ordering {
|
||||
// case .byScore:
|
||||
// return try SelectedRibbon.all().fetchAll(db)
|
||||
// case .byName:
|
||||
// // return try SelectedRibbon.all().orderedByName().fetchAll(db)
|
||||
// return try SelectedRibbon.all().fetchAll(db)
|
||||
// }
|
||||
print("FETCH SELECTED RIBBON")
|
||||
var ret2 = try SelectedRibbon.fetchAll(db, sql: "SELECT * FROM selectedRibbon") // [Player]
|
||||
print(ret2)
|
||||
print("FETCH JOIN RIBBON")
|
||||
var ret = try Ribbon.fetchAll(db, sql: "SELECT ribbon.* FROM selectedRibbon join ribbon on selectedRibbon.ribbonId = ribbon.rowid") // [Player]
|
||||
print(ret)
|
||||
return ret
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -101,15 +101,17 @@ extension AppDatabase {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func saveSelectedRibbon(_ selectedRibbon: inout SelectedRibbon) async throws {
|
||||
// if selectedRibbon.name.isEmpty {
|
||||
// if ribbon.name.isEmpty {
|
||||
// throw ValidationError.missingName
|
||||
// }
|
||||
selectedRibbon = try await dbWriter.write { [selectedRibbon] db in
|
||||
try selectedRibbon.saved(db)
|
||||
try await dbWriter.write { [selectedRibbon] db in
|
||||
try selectedRibbon.update(db)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Create random Lines if the database is empty.
|
||||
func createRandomLinesIfEmpty() throws {
|
||||
|
||||
|
|
|
@ -130,70 +130,82 @@ struct OffsetObservingScrollView<Content: View>: View {
|
|||
|
||||
|
||||
struct SwitchButton : View {
|
||||
@State var ribbon: Ribbon
|
||||
// @State var selectedRibbon: SelectedRibbon
|
||||
var ribbon: Ribbon
|
||||
// var selectedRibbon: SelectedRibbon
|
||||
|
||||
@Binding var book : String
|
||||
@Binding var scrollView : UIScrollView?
|
||||
@Binding var scrollOffset : CGFloat
|
||||
@Binding var readOffset: CGPoint
|
||||
|
||||
@Environment(\.appDatabase) private var appDatabase
|
||||
|
||||
|
||||
@Query(SelectedRibbonRequest()) private var sr: [SelectedRibbon]
|
||||
@Query(SelectedRibbonRequest()) private var sr: [Ribbon]
|
||||
|
||||
@State var saveOffset = CGFloat()
|
||||
var body: some View {
|
||||
// ForEach(sr) { selectedRibbon in
|
||||
Button("meow",
|
||||
action: {
|
||||
Task {
|
||||
var selectedRibbon = sr[0]
|
||||
Button(String(sr[0].id!) + " " + String(ribbon.scrollOffset),
|
||||
// Button("meow",
|
||||
action: {
|
||||
Task {
|
||||
var selectedRibbon = sr[0]
|
||||
Print("SELECTED RIBBON", selectedRibbon)
|
||||
|
||||
saveOffset = CGFloat(readOffset.y)
|
||||
Print("save/read offset", readOffset.y)
|
||||
scrollView!.contentOffset.y = CGFloat(2000)
|
||||
|
||||
Print("START")
|
||||
_ = Print(ribbon)
|
||||
_ = Print(selectedRibbon)
|
||||
Print("START2")
|
||||
saveOffset = CGFloat(readOffset.y)
|
||||
Print("selected ribbon", selectedRibbon)
|
||||
Print("view ribbon", ribbon)
|
||||
|
||||
if (selectedRibbon.ribbonId != ribbon.id!) {
|
||||
Print(ribbon.id!)
|
||||
Print("switching")
|
||||
book = ribbon.book
|
||||
scrollOffset = CGFloat(ribbon.scrollOffset)
|
||||
// scrollOffset = CGFloat(1500)
|
||||
Print("setted offset value", scrollOffset)
|
||||
selectedRibbon.ribbonId = ribbon.id!
|
||||
_ = try await appDatabase.saveSelectedRibbon(&selectedRibbon)
|
||||
_ = Print(selectedRibbon)
|
||||
if (selectedRibbon.id != ribbon.id!) {
|
||||
Print("switching")
|
||||
book = ribbon.book
|
||||
scrollOffset = CGFloat(ribbon.scrollOffset)
|
||||
// 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")
|
||||
}
|
||||
var editedRibbon = ribbon
|
||||
editedRibbon.scrollOffset = Int(saveOffset)
|
||||
Print("current ribbon offset after potential switch", editedRibbon.scrollOffset)
|
||||
|
||||
_ = try await appDatabase.saveRibbon(&editedRibbon)
|
||||
_ = Print(editedRibbon)
|
||||
|
||||
Print("ribbon offset saved")
|
||||
|
||||
Print("set offset value", scrollOffset)
|
||||
Print("read offset value", readOffset.y)
|
||||
|
||||
|
||||
Print("END")
|
||||
_ = Print(ribbon)
|
||||
_ = Print(selectedRibbon)
|
||||
Print("END2")
|
||||
}
|
||||
}
|
||||
|
||||
)
|
||||
.buttonStyle(BlueButtonStyle())
|
||||
// }
|
||||
Print("saveOffset", saveOffset)
|
||||
var editedRibbon = ribbon
|
||||
editedRibbon.scrollOffset = Int(saveOffset)
|
||||
|
||||
_ = try await appDatabase.saveRibbon(&editedRibbon)
|
||||
_ = Print("editedRibbon", editedRibbon)
|
||||
_ = Print("ribbon", ribbon)
|
||||
|
||||
Print("ribbon offset saved")
|
||||
}
|
||||
}
|
||||
|
||||
)
|
||||
.buttonStyle(BlueButtonStyle())
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
class ScrollViewHandler: NSObject {
|
||||
}
|
||||
|
||||
extension ScrollViewHandler: UIScrollViewDelegate {
|
||||
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
||||
print("delegate", scrollView.contentOffset.y)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
struct ContentView: View {
|
||||
|
||||
@State var viewState = CGSize.zero
|
||||
|
@ -204,13 +216,18 @@ struct ContentView: View {
|
|||
@State var selectedLine : Int64?
|
||||
@State var scrollOffset = CGFloat()
|
||||
|
||||
@State var thisScrollView : UIScrollView?
|
||||
@State var initLoad = false
|
||||
|
||||
var scrollDelegate = ScrollViewHandler()
|
||||
|
||||
// @State var selectedRibbon: Ribbon!,
|
||||
// @State var selectedRibbon: Ribbon!
|
||||
|
||||
// @State var selectedRibbonId = Int64(UserDefaults.standard.optionalInt(forKey: "selectedRibbonId") ?? 1)
|
||||
|
||||
|
||||
@Query(SelectedRibbonRequest()) private var selectedRibbon: [SelectedRibbon]
|
||||
// ribbon
|
||||
@Query(SelectedRibbonRequest()) private var selectedRibbon: [Ribbon]
|
||||
|
||||
@State var endedDrag = true
|
||||
|
||||
|
@ -234,16 +251,18 @@ struct ContentView: View {
|
|||
GeometryReader { geometry in
|
||||
ZStack{
|
||||
VStack{
|
||||
Text("cat" + String(selectedRibbon[0].id!))
|
||||
ForEach(ribbons) { ribbon in
|
||||
ForEach(selectedRibbon) { sr in
|
||||
//ForEach(selectedRibbon) { sr in
|
||||
SwitchButton(ribbon: ribbon,
|
||||
// selectedRibbon:sr,
|
||||
book: $lines.book,
|
||||
scrollView:$thisScrollView,
|
||||
// selectedRibbon:selectedRibbon[0],
|
||||
scrollOffset: $scrollOffset,
|
||||
readOffset: $readOffset
|
||||
)
|
||||
.buttonStyle(BlueButtonStyle())
|
||||
}
|
||||
//}
|
||||
}
|
||||
}
|
||||
.frame(width: geometry.size.width, height: geometry.size.height, alignment: .topLeading)
|
||||
|
@ -252,7 +271,8 @@ struct ContentView: View {
|
|||
// VStack (spacing: 0){
|
||||
ScrollViewReader { proxy in
|
||||
//ScrollableView($offset, animationDuration: 0) {
|
||||
OffsetObservingScrollView(offset: $readOffset)
|
||||
// OffsetObservingScrollView(offset: $readOffset)
|
||||
ScrollView
|
||||
{
|
||||
// List {
|
||||
LazyVStack {
|
||||
|
@ -287,10 +307,17 @@ struct ContentView: View {
|
|||
.introspectScrollView { scrollView in
|
||||
//let width = scrollView.contentSize.width - scrollView.frame.width
|
||||
//Print(scrollView.contentOffset.x)
|
||||
// Print("here")
|
||||
if (scrollOffset != scrollView.contentOffset.y) {
|
||||
scrollView.contentOffset.y = scrollOffset
|
||||
Print("introspect")
|
||||
scrollView.delegate = scrollDelegate
|
||||
|
||||
if (thisScrollView == nil) {
|
||||
thisScrollView = scrollView
|
||||
}
|
||||
|
||||
// scrollView.contentOffset.y = scrollOffset
|
||||
// if (scrollOffset != scrollView.contentOffset.y) {
|
||||
// scrollView.contentOffset.y = scrollOffset
|
||||
// }
|
||||
}
|
||||
.listStyle(PlainListStyle())
|
||||
|
||||
|
|
Loading…
Reference in New Issue