Skip to content

Commit

Permalink
sessioncard fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pdtxie committed Mar 22, 2024
1 parent b254b98 commit 389d054
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 79 deletions.
92 changes: 54 additions & 38 deletions CubeTime/Sessions/ImportExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,48 @@ struct ImportFlow: View {

struct ExportFlowPickSessions: View {
@EnvironmentObject var exportViewModel: ExportViewModel
@Environment(\.horizontalSizeClass) var hSizeClass


let sessions: FetchedResults<Session>


var body: some View {
ForEach(sessions) { session in
let selected = exportViewModel.selectedSessions.contains(session)
HStack {
Text(session.name ?? "UNKNOWN NAME")
Spacer()
Image(systemName: selected ? "checkmark.circle.fill" : "checkmark.circle")
}
.onTapGesture {
if selected {
exportViewModel.selectedSessions.remove(session)
} else {
exportViewModel.selectedSessions.insert(session)
}
ScrollView {
ForEach(sessions) { session in
let selected = exportViewModel.selectedSessions.contains(session)

SessionCardBase(item: session,
pinned: false,
sessionType: SessionType(rawValue: session.sessionType)!,
name: session.name ?? "Unknown session name",
scrambleType: Int(session.scrambleType),
solveCount: 0,
selected: selected,
forExportUse: true)
.onTapGesture {
withAnimation(Animation.customDampedSpring) {
if selected {
exportViewModel.selectedSessions.remove(session)
} else {
exportViewModel.selectedSessions.insert(session)
}
}
}
.padding(.horizontal)
}
}
.navigationTitle("Export Sessions")

CTButton(type: exportViewModel.selectedSessions.count == 0 ? .disabled : .halfcoloured(nil), size: .large, onTapRun: { exportViewModel.exportFlowState = .pickingFormats }) {
HStack {
Text("Continue")

Image(systemName: "arrow.forward")
.overlay(alignment: .bottomTrailing) {
CTButton(type: exportViewModel.selectedSessions.count == 0 ? .disabled : .coloured(nil), size: .large, onTapRun: { exportViewModel.exportFlowState = .pickingFormats }) {
HStack {
Text("Continue")

Image(systemName: "arrow.forward")
.font(.subheadline)
}
}
.padding(.horizontal)
}
.frame(maxWidth: .infinity, alignment: .trailing)
.padding(.trailing)

}
}

Expand All @@ -58,18 +67,19 @@ struct ExportFlowPickFormats: View {
var body: some View {
let zippedArray = Array(zip(exportViewModel.allFormats.indices, exportViewModel.allFormats))
ForEach(zippedArray, id: \.0) { (_, format) in
// let selected = exportViewModel.selectedFormats.contains(format)
let indexInSelected = exportViewModel.selectedFormats.firstIndex(where: {$0 === format})
HStack {
Text(format.getName())
Spacer()
Image(systemName: indexInSelected != nil ? "checkmark.circle.fill" : "checkmark.circle")
}
.onTapGesture {
if let indexInSelected {
exportViewModel.selectedFormats.remove(at: indexInSelected)
} else {
exportViewModel.selectedFormats.append(format)
withAnimation(Animation.customDampedSpring) {
if let indexInSelected {
exportViewModel.selectedFormats.remove(at: indexInSelected)
} else {
exportViewModel.selectedFormats.append(format)
}
}
}
}
Expand Down Expand Up @@ -114,17 +124,23 @@ struct ExportFlow: View {
let sessions: FetchedResults<Session>

var body: some View {
VStack {
switch exportViewModel.exportFlowState {
case .pickingSessions:
ExportFlowPickSessions(sessions: sessions)
case .pickingFormats:
ExportFlowPickFormats()
case .finished(let result):
ExportFlowFinished(result: result)
ZStack {
BackgroundColour()
.ignoresSafeArea()

VStack {
switch exportViewModel.exportFlowState {
case .pickingSessions:
ExportFlowPickSessions(sessions: sessions)
case .pickingFormats:
ExportFlowPickFormats()
case .finished(let result):
ExportFlowFinished(result: result)
}
}
.safeAreaInset(safeArea: .tabBar)
.environmentObject(exportViewModel)

}
.safeAreaInset(safeArea: .tabBar)
.environmentObject(exportViewModel)
}
}
127 changes: 87 additions & 40 deletions CubeTime/Sessions/SessionCard.swift
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import SwiftUI
import Foundation

struct SessionCard: View {

struct SessionCardBase: View {
@Environment(\.globalGeometrySize) var globalGeometrySize
@Environment(\.managedObjectContext) var managedObjectContext

@EnvironmentObject var stopwatchManager: StopwatchManager

@State private var isShowingDeleteDialog = false
@State private var isShowingCustomizeDialog = false

@ScaledMetric private var pinnedSessionHeight: CGFloat = 110
@ScaledMetric private var regularSessionHeight: CGFloat = 65


var item: Session
var allSessions: FetchedResults<Session>

let pinned: Bool
let sessionType: SessionType
let name: String
let scrambleType: Int
let solveCount: Int

@Namespace var namespace
let forExportUse: Bool

init (item: Session, allSessions: FetchedResults<Session>) {
var selected: Bool

init(item: Session, pinned: Bool, sessionType: SessionType, name: String, scrambleType: Int, solveCount: Int, selected: Bool, forExportUse: Bool=false) {
self.item = item
self.allSessions = allSessions

// Copy out the things so that it won't change to null coalesced defaults on deletion
self.pinned = item.pinned
self.sessionType = SessionType(rawValue: item.sessionType)!
self.name = item.name ?? "Unknown session name"
self.scrambleType = Int(item.scrambleType)
self.solveCount = item.solves?.count ?? -1
self.pinned = pinned
self.sessionType = sessionType
self.name = name
self.scrambleType = scrambleType
self.solveCount = solveCount

self.selected = selected

self.forExportUse = forExportUse
}

@Namespace var namespace

var body: some View {
HStack {
VStack(alignment: .leading) {
Expand All @@ -59,37 +59,85 @@ struct SessionCard: View {
}
}
}
.padding(.leading, stopwatchManager.currentSession == item ? 24 : 10)
.padding(.leading, self.selected && !self.forExportUse ? 24 : 10)
.padding(.vertical, 10)
.offset(y: -1)

Spacer()

Image(puzzleTypes[scrambleType].name)
.resizable()
.frame(width: 45, height: 45)
.aspectRatio(contentMode: .fit)
.foregroundColor(Color("dark"))
.padding([.vertical, .trailing], 10)
.frame(maxHeight: .infinity, alignment: .topTrailing)
if (!forExportUse) {
Image(puzzleTypes[scrambleType].name)
.resizable()
.frame(width: 45, height: 45)
.aspectRatio(contentMode: .fit)
.foregroundColor(Color("dark"))
.padding([.vertical, .trailing], 10)
.frame(maxHeight: .infinity, alignment: .topTrailing)
} else if (selected) {
Image(systemName: "checkmark.circle.fill")
.font(.body.weight(.semibold))
.foregroundStyle(Color("accent"), Color("overlay0"))
.padding(.trailing, 24)
}
}


.frame(height: pinned ? pinnedSessionHeight : regularSessionHeight, alignment: .center)

.background( Group {
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(Color("indent1"))
.frame(height: pinned ? pinnedSessionHeight : regularSessionHeight)

RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(Color("overlay0"))
.frame(width: stopwatchManager.currentSession == item ? 16 : nil,
height: item.pinned ? pinnedSessionHeight : regularSessionHeight)
.frame(maxWidth: .infinity, alignment: .leading)

})
.background(
Group {
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(Color("indent1"))
.frame(height: pinned ? pinnedSessionHeight : regularSessionHeight)

if (forExportUse) {
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(Color("overlay0"))
.frame(width: nil,
height: item.pinned ? pinnedSessionHeight : regularSessionHeight)
.opacity(selected ? 0 : 1)
.frame(maxWidth: .infinity, alignment: .leading)
} else {
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(Color("overlay0"))
.frame(width: selected ? 16 : nil,
height: item.pinned ? pinnedSessionHeight : regularSessionHeight)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
)
.clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
.contentShape(.contextMenuPreview, RoundedRectangle(cornerRadius: 12, style: .continuous))
}
}


struct SessionCard: View {
@EnvironmentObject var stopwatchManager: StopwatchManager

@Environment(\.managedObjectContext) var managedObjectContext

@State private var isShowingDeleteDialog = false
@State private var isShowingCustomizeDialog = false


var item: Session
var allSessions: FetchedResults<Session>

init (item: Session, allSessions: FetchedResults<Session>) {
self.item = item
self.allSessions = allSessions
}

var body: some View {
SessionCardBase(item: item,
pinned: item.pinned,
sessionType: SessionType(rawValue: item.sessionType)!,
name: item.name ?? "Unknown session name",
scrambleType: Int(item.scrambleType),
solveCount: item.solves?.count ?? -1,
selected: item == stopwatchManager.currentSession)

.onTapGesture {
withAnimation(Animation.customDampedSpring) {
if stopwatchManager.currentSession != item {
Expand All @@ -99,7 +147,6 @@ struct SessionCard: View {
}



.contentShape(.contextMenuPreview, RoundedRectangle(cornerRadius: 12, style: .continuous))

.contextMenu(menuItems: {
Expand Down Expand Up @@ -133,7 +180,7 @@ struct SessionCard: View {
.tint(Color("accent"))
}

.confirmationDialog(String("Are you sure you want to delete \"\(name)\"? All solves will be deleted and this cannot be undone."), isPresented: $isShowingDeleteDialog, titleVisibility: .visible) {
.confirmationDialog(String("Are you sure you want to delete \"\(self.item.name)\"? All solves will be deleted and this cannot be undone."), isPresented: $isShowingDeleteDialog, titleVisibility: .visible) {
Button("Confirm", role: .destructive) {
if item == stopwatchManager.currentSession {
var next: Session? = nil
Expand Down
2 changes: 1 addition & 1 deletion CubeTime/Sessions/SessionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct SessionsView: View {
.imageScale(.small)
}
} label: {
CTButtonBase(type: .coloured(nil), size: .small, outlined: false, square: false, hasShadow: true, hasBackground: true, supportsDynamicResizing: true, expandWidth: false) {
CTBubble(type: .coloured(nil), size: .small, outlined: false, square: false, hasShadow: true, hasBackground: true, supportsDynamicResizing: true, expandWidth: false) {
Label("Import & Export", systemImage: "square.and.arrow.up.on.square")
.labelStyle(.titleAndIcon)
.imageScale(.small)
Expand Down

0 comments on commit 389d054

Please sign in to comment.