Skip to content

Commit

Permalink
CATTY-717 Allow selection of images from files
Browse files Browse the repository at this point in the history
Implementation of feature CATTY-717 Allow selection of images from files
  • Loading branch information
lucatp authored and wallisch committed Oct 18, 2024
1 parent 6bf47e4 commit 38a82a2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Catty.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
05E68028255B5DEE00D1E295 /* EmbroideryDSTServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05E68024255B5DED00D1E295 /* EmbroideryDSTServiceTests.swift */; };
05E6802C255B5E3400D1E295 /* CGVectorExtentions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05E6802B255B5E3400D1E295 /* CGVectorExtentions.swift */; };
05E6802F255B602700D1E295 /* stitch.dst in Resources */ = {isa = PBXBuildFile; fileRef = 05E6802E255B602700D1E295 /* stitch.dst */; };
175605302CC1E46400E3B27E /* LooksTableViewController+SelectFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756052F2CC1E46400E3B27E /* LooksTableViewController+SelectFile.swift */; };
1764E6382A06CC940013102B /* SoundRecorderViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1764E6372A06CC940013102B /* SoundRecorderViewController.swift */; };
17A0218A2726F36700F0489E /* AudioKit in Frameworks */ = {isa = PBXBuildFile; productRef = 17A021892726F36700F0489E /* AudioKit */; };
17A0218D2726F3C600F0489E /* DunneAudioKit in Frameworks */ = {isa = PBXBuildFile; productRef = 17A0218C2726F3C600F0489E /* DunneAudioKit */; };
Expand Down Expand Up @@ -2204,6 +2205,7 @@
0C64E34E5347DA71ECB56DE6 /* pt */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.strings; name = pt; path = pt.lproj/Localizable.strings; sourceTree = "<group>"; };
113CDA22CF21176FFA49E694 /* sd */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.strings; name = sd; path = sd.lproj/Localizable.strings; sourceTree = "<group>"; };
13B5EB79EDA9CF73908FF75C /* no */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.strings; name = no; path = no.lproj/Localizable.strings; sourceTree = "<group>"; };
1756052F2CC1E46400E3B27E /* LooksTableViewController+SelectFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "LooksTableViewController+SelectFile.swift"; sourceTree = "<group>"; };
1764E6372A06CC940013102B /* SoundRecorderViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoundRecorderViewController.swift; sourceTree = "<group>"; };
17A8AC642644E6A900DDD480 /* ForceInit.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ForceInit.m; sourceTree = "<group>"; };
17A8AC6B2644E6B500DDD480 /* ForceInit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ForceInit.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -9243,6 +9245,7 @@
92FF311C1A24DEB300093DA7 /* LooksTableViewController.h */,
92FF311D1A24DEB300093DA7 /* LooksTableViewController.m */,
59ABDC5F2021BBFB00061403 /* LooksTableViewController+MediaLibrary.swift */,
1756052F2CC1E46400E3B27E /* LooksTableViewController+SelectFile.swift */,
);
path = MaintainLooks;
sourceTree = "<group>";
Expand Down Expand Up @@ -13364,6 +13367,7 @@
4CF0729D20D66F8D00F93AB5 /* LookNumberSensor.swift in Sources */,
05E6802C255B5E3400D1E295 /* CGVectorExtentions.swift in Sources */,
597AB56E202081E8007CD10D /* LibrarySoundCollectionViewCell.swift in Sources */,
175605302CC1E46400E3B27E /* LooksTableViewController+SelectFile.swift in Sources */,
4C724E5B1B4D3E8C00E27479 /* Script+CBXMLHandler.m in Sources */,
92EC985E1BC3ABB90003A891 /* PhiroMotorStopBrick+CBXMLHandler.m in Sources */,
4C0F9F9F204BD2B100E71B2D /* SayForBubbleBrickCell.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

import MobileCoreServices

extension LooksTableViewController {

@objc
Expand All @@ -36,7 +38,7 @@ extension LooksTableViewController {
self.navigationController?.pushViewController(viewController, animated: true)
}

private func showImportAlert(itemName: String) {
public func showImportAlert(itemName: String) {
let alertTitle = kLocalizedMediaLibraryImportFailedTitle
let alertMessage = "\(kLocalizedMediaLibraryImportFailedMessage) \(itemName)"
let buttonTitle = kLocalizedOK
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright (C) 2010-2024 The Catrobat Team
* (http://developer.catrobat.org/credits)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* (http://developer.catrobat.org/license_additional_term)
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

import MobileCoreServices

extension LooksTableViewController {

@available(iOS 14.0, *)
static var supportedFileFormats = [UTType.png, UTType.jpeg]
static let supportedFileFormatsBackwardsCompatibility = ["public.png", "public.jpeg"]

@objc
func showImagesSelectFile() {
var documentPicker: UIDocumentPickerViewController
if #available(iOS 14.0, *) {
documentPicker = UIDocumentPickerViewController.init(forOpeningContentTypes: type(of: self).supportedFileFormats, asCopy: true)
} else {
documentPicker = UIDocumentPickerViewController(documentTypes: type(of: self).supportedFileFormatsBackwardsCompatibility, in: .import)
}

documentPicker.allowsMultipleSelection = false
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .formSheet
present(documentPicker, animated: true)
}
}

extension LooksTableViewController: UIDocumentPickerDelegate, UINavigationControllerDelegate {
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
let documents = URL(fileURLWithPath:
CBFileManager.shared().documentsDirectory)

for url in urls {
let fileName = UUID().uuidString
let name = url.deletingPathExtension().lastPathComponent
let fileURL = documents
.appendingPathComponent(fileName)
.appendingPathExtension(url.pathExtension)

do {
let data = try Data.init(contentsOf: url)
try data.write(to: fileURL, options: .atomic)
if let image = UIImage(data: data) {
self.addMediaLibraryLoadedImage(image, withName: name)
}
} catch {
self.showImportAlert(itemName: name)
}
}
controller.dismiss(animated: true)
}

public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
controller.dismiss(animated: true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ - (void)showAddLookActionSheet
}
}

[[[[actionSheet
[[[[[actionSheet
addDefaultActionWithTitle:kLocalizedDrawNewImage handler:^{
dispatch_async(dispatch_get_main_queue(), ^{
PaintViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:kPaintViewControllerIdentifier];
Expand All @@ -658,7 +658,13 @@ - (void)showAddLookActionSheet
[self showLooksMediaLibrary];
}
});
}] build]
}]
addDefaultActionWithTitle:kLocalizedSelectFile handler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self showImagesSelectFile];
});
}]
build]
showWithController:self];
}

Expand Down

0 comments on commit 38a82a2

Please sign in to comment.