Add photo picker.

This commit is contained in:
Marcin Czachursk 2023-02-14 18:40:08 +01:00
parent 4ffaf9d5a9
commit 44862d2107
3 changed files with 66 additions and 2 deletions

View File

@ -939,12 +939,14 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_ASSET_PATHS = "\"Vernissage/Preview Content\"";
DEVELOPMENT_TEAM = B2U9FEKYP8;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Vernissage/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = Vernissage;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.photography";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
@ -974,12 +976,14 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_ASSET_PATHS = "\"Vernissage/Preview Content\"";
DEVELOPMENT_TEAM = B2U9FEKYP8;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Vernissage/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = Vernissage;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.photography";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;

View File

@ -14,4 +14,8 @@ extension View {
action()
}
}
func hideKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}

View File

@ -5,10 +5,12 @@
//
import SwiftUI
import PhotosUI
import MastodonKit
struct ComposeView: View {
enum FocusField: Hashable {
case unknown
case content
}
@ -18,6 +20,10 @@ struct ComposeView: View {
@State var statusViewModel: StatusModel?
@State private var text = String.empty()
@State private var photosPickerVisible = false
@State private var selectedItems: [PhotosPickerItem] = []
@State private var photosData: [Data] = []
@FocusState private var focusedField: FocusField?
@ -45,6 +51,35 @@ struct ComposeView: View {
.task {
self.focusedField = .content
}
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
HStack(alignment: .center) {
Button {
hideKeyboard()
self.focusedField = .unknown
self.photosPickerVisible = true
} label: {
Image(systemName: "photo.on.rectangle.angled")
}
Spacer()
}
}
}
HStack(alignment: .center) {
ForEach(self.photosData, id: \.self) { photoData in
if let uiImage = UIImage(data: photoData) {
Image(uiImage: uiImage)
.resizable()
.frame(width: 80, height: 80)
.clipShape(RoundedRectangle(cornerRadius: 10))
}
}
}
if let status = self.statusViewModel {
HStack (alignment: .top) {
@ -95,6 +130,27 @@ struct ComposeView: View {
}
}
}
.onChange(of: self.selectedItems) { selectedItem in
self.photosData = []
for item in self.selectedItems {
item.loadTransferable(type: Data.self) { result in
switch result {
case .success(let data):
if let data {
self.photosData.append(data)
} else {
ToastrService.shared.showError(subtitle: "Cannot show image preview.")
}
case .failure(let error):
ErrorService.shared.handle(error, message: "Cannot retreive image from library.", showToastr: true)
}
}
self.focusedField = .content
}
}
.photosPicker(isPresented: $photosPickerVisible, selection: $selectedItems, maxSelectionCount: 4, matching: .images)
.navigationBarTitle(Text("Compose"), displayMode: .inline)
}
}