Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example app #52

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Csv2ImageApp/Csv2ImageApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,12 @@
INFOPLIST_KEY_UIRequiresFullScreen = NO;
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
INFOPLIST_PREPROCESS = YES;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.0;
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.4;
PRODUCT_BUNDLE_IDENTIFIER = dev.fummicc1.Csv2ImageApp_debug;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -680,12 +680,12 @@
INFOPLIST_KEY_UIRequiresFullScreen = NO;
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
INFOPLIST_PREPROCESS = YES;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.0;
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.4;
PRODUCT_BUNDLE_IDENTIFIER = dev.fummicc1.Csv2ImageApp;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
}
},
{
"package": "SwiftSyntax",
"package": "swift-syntax",
"repositoryURL": "https://github.com/apple/swift-syntax",
"state": {
"branch": "main",
"revision": "dfb8deb846e16d98e1d5b2261ed608121e096037",
"version": null
"branch": null,
"revision": "6ad4ea24b01559dde0773e3d091f1b9e36175036",
"version": "509.0.2"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "77",
"green" : "77",
"red" : "77"
}
"platform" : "ios",
"reference" : "secondarySystemBackgroundColor"
},
"idiom" : "universal"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"colors" : [
{
"color" : {
"platform" : "universal",
"reference" : "labelColor"
"platform" : "ios",
"reference" : "tertiarySystemBackgroundColor"
},
"idiom" : "universal"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0xF0",
"green" : "0xF0",
"red" : "0xF0"
}
"platform" : "universal",
"reference" : "labelColor"
},
"idiom" : "universal"
}
Expand Down
2 changes: 2 additions & 0 deletions Csv2ImageApp/Csv2ImageApp/States/GenerateOutputState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct GenerateOutputState: Hashable, Equatable {

var encoding: String.Encoding
var exportType: Csv.ExportType
var size: PdfSize = .a3
var orientation: PdfSize.Orientation = .portrait

var cgImage: CGImage?
var pdfDocument: PDFDocument?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,40 @@ class GenerateOutputModel: ObservableObject {

@MainActor
func onAppear() async {
_state.projectedValue
.map(\.exportType)
.removeDuplicates()
.combineLatest(
_state.projectedValue
.map(\.encoding)
.removeDuplicates()
)
.receive(on: queue)
.share()
.sink { (_, _) in
Task {
await self.updateCachedCsv()
}
Publishers.CombineLatest4(
_state.projectedValue
.map(
\.exportType
).removeDuplicates(),
_state.projectedValue
.map(
\.encoding
).removeDuplicates(),
_state.projectedValue
.map(
\.size
).removeDuplicates(),
_state.projectedValue
.map(
\.orientation
)
.removeDuplicates()
)
.share()
.receive(on: queue)
.sink { (_, _, _, _) in
Task {
await self.updateCachedCsv()
}
.store(in: &cancellables)
}
.store(in: &cancellables)
}

func updateCachedCsv() async {
let exportMode = await state.exportType
let encoding = await state.encoding
let pdfSize = await state.size
let pdfOrientation = await state.orientation
let url = await state.url
let fileType = await state.fileType
let csv: Csv?
Expand Down Expand Up @@ -110,14 +123,20 @@ class GenerateOutputModel: ObservableObject {
csvTask = Task {
Task {
do {
await csv.update(
pdfMetadata: .init(
size: pdfSize,
orientation: pdfOrientation
)
)
let exportable = try await csv.generate(exportType: exportMode)
if type(of: exportable.base) == PDFDocument.self {
await self.update(keyPath: \.pdfDocument, value: (exportable.base as! PDFDocument))
} else {
await self.update(keyPath: \.cgImage, value: (exportable.base as! CGImage))
}
} catch {

print(error)
}
}
Task {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,50 @@ struct GenerateOutputView_iOS: View {
@Binding var backToPreviousPage: Bool
@State private var succeedSavingOutput: Bool = false

private let availableEncodingType: [String.Encoding] = [
.utf8,
.utf16,
.utf32,
.shiftJIS,
.ascii,
]

var body: some View {
ZStack {
Rectangle()
.background(Asset.lightAccentColor.swiftUIColor)
.ignoresSafeArea()
Group {
VStack {
HStack {
CButton.labeled("Back") {
withAnimation {
backToPreviousPage = true
}
}
Spacer()
NavigationStack {
loadedContent.id(model.state.isLoading)
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Save") {
succeedSavingOutput = model.save()
}
}
}
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button("Back") {
backToPreviousPage = true
}
}
}
.alert("Complete Saving!", isPresented: $succeedSavingOutput) {
CButton.labeled("Back") {
withAnimation {
backToPreviousPage = true
}
.padding()
}
if let savedURL = model.savedURL, Application.shared.canOpenURL(savedURL) {
CButton.labeled("Open") {
Application.shared.open(savedURL)
}
}
}
}
}

var loadedContent: some View {
VStack {
List {
Section("Export Type") {
Picker(selection: Binding(get: {
model.state.exportType
}, set: { exportType in
Expand All @@ -42,56 +70,76 @@ struct GenerateOutputView_iOS: View {
CText("PNG")
.tag(Csv.ExportType.png)
} label: {
CText("Export Type")
EmptyView()
}
.pickerStyle(.segmented)
.padding()
GeometryReader { proxy in
VStack {
GeneratePreviewView(
model: model,
size: .constant(
CGSize(
width: proxy.size.width * 0.85,
height: proxy.size.height * 0.8
)
)
)
.padding()
Spacer()
HStack {
Spacer()
CButton.labeled("Save") {
succeedSavingOutput = model.save()
}
}
Section("Encoding") {
Menu(model.state.encoding.description) {
ForEach(availableEncodingType, id: \.self) { encoding in
Button {
model.update(keyPath: \.encoding, value: encoding)
} label: {
Text(encoding.description)
}
.padding()
}
}
.fixedSize()
}
.background(Asset.lightAccentColor.swiftUIColor)
}
if model.state.isLoading {
ProgressView {
CText("Loading...", font: .largeTitle)
Section("PDF Size") {
Menu(model.state.size.rawValue) {
ForEach(PdfSize.allCases.indices, id: \.self) { index in
let size = PdfSize.allCases[index]
Button {
model.update(keyPath: \.size, value: size)
} label: {
Text(size.rawValue)
}
}
}
.fixedSize()
}
.padding()
.progressViewStyle(.linear)
}
}
.background(Asset.lightAccentColor.swiftUIColor)
.alert("Complete Saving!", isPresented: $succeedSavingOutput) {
CButton.labeled("Back") {
withAnimation {
backToPreviousPage = true
Section("PDF Orientation") {
Menu(model.state.orientation.rawValue) {
ForEach(PdfSize.Orientation.allCases.indices, id: \.self) { index in
let orientation = PdfSize.Orientation.allCases[index]
Button {
model.update(keyPath: \.orientation, value: orientation)
} label: {
Text(orientation.rawValue)
}
}
}
.fixedSize()
}
}
if let savedURL = model.savedURL, Application.shared.canOpenURL(savedURL) {
CButton.labeled("Open") {
Application.shared.open(savedURL)
.background(Asset.lightAccentColor.swiftUIColor)
.frame(maxHeight: 200)

GeometryReader { proxy in
VStack(alignment: .center) {
GeneratePreviewView(
model: model,
size: .constant(
CGSize(
width: proxy.size.width,
height: proxy.size.height
)
)
)
}

}
.background(Asset.lightAccentColor.swiftUIColor)
}
}

var loadingContent: some View {
ProgressView {
CText("Loading...", font: .largeTitle)
}
.padding()
.progressViewStyle(.linear)
}
}
#endif
Loading
Loading