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

Fix/font select view #272

Merged
merged 10 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
10 commits
Select commit Hold shift + click to select a range
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
204 changes: 143 additions & 61 deletions Kabinett/Presentation/View/WriteLetter/ContentWriteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct ContentWriteView: View {
@StateObject var viewModel = ContentWriteViewModel()
@ObservedObject var imageViewModel: ImagePickerViewModel
@ObservedObject var customTabViewModel: CustomTabViewModel
@StateObject var fontViewModel = FontSelectionViewModel()

@State var isPopup: Bool = false

init(
letterContent: Binding<LetterWriteModel>,
Expand All @@ -36,57 +39,16 @@ struct ContentWriteView: View {
ZStack(alignment: .top) {
VStack {
ScrollableLetterView(letterContent: $letterContent, viewModel: viewModel, currentIndex: $viewModel.currentIndex)
.font(FontUtility.selectedFont(font: letterContent.fontString ?? "", size: 14))

Text("\(viewModel.currentIndex+1) / \(viewModel.texts.count)")
}
HStack(alignment: .center) {
Button {
if viewModel.texts.count > 1 {
viewModel.isDeleteAlertPresented = true
}
} label: {
Image("PageMinus")
.font(.system(size: 15))
.frame(width: UIScreen.main.bounds.width * 0.3/3)
}
.alert(isPresented: $viewModel.isDeleteAlertPresented) {
Alert(
title: Text("Delete Page"),
message: Text("ν˜„μž¬ νŽ˜μ΄μ§€λ₯Ό μ§€μš°μ‹œκ² μ–΄μš”?"),
primaryButton: .destructive(Text("μ‚­μ œ")) {
viewModel.deleteLetter(idx: viewModel.currentIndex)
},
secondaryButton: .cancel(Text("μ·¨μ†Œ")) {
viewModel.isDeleteAlertPresented = false
}
)
}
Button {
viewModel.createNewLetter(idx: viewModel.currentIndex)
} label: {
Image(systemName: "doc.badge.plus")
.font(.system(size: 15))
.frame(width: UIScreen.main.bounds.width * 0.3/3)
}
Button {
customTabViewModel.showPhotoLibrary = true
customTabViewModel.isLetterWrite = true
} label: {
Image(systemName: "photo.on.rectangle.angled")
.font(.system(size: 15))
.frame(width: UIScreen.main.bounds.width * 0.3/3, height: 30)
.background(letterContent.photoContents.isEmpty ? Color.clear : Color.white)
.foregroundStyle(letterContent.photoContents.isEmpty ? Color("ToolBarIcon") : Color(.primary900))
.clipShape(Capsule())
.shadow(color: letterContent.photoContents.isEmpty ? Color.clear : Color(.primary300), radius: 7, x: 3, y: 3)
}
}
.frame(maxWidth: UIScreen.main.bounds.width * 0.45, maxHeight: 40)
.foregroundStyle(Color("ToolBarIcon"))
.background(Color(.primary100))
.clipShape(Capsule())
.shadow(color: Color(.primary300), radius: 5, x: 3, y: 3)
.padding(.top, -10)
MiniTabBar(letterContent: $letterContent, viewModel: viewModel, customTabViewModel: customTabViewModel, isPopup: $isPopup)
}
}
.overlay {
if isPopup {
CustomFontMenu(letterContent: $letterContent, isPopup: $isPopup, fontViewModel: fontViewModel)
}
}
.toolbar {
Expand Down Expand Up @@ -114,6 +76,132 @@ struct ContentWriteView: View {
}
}

struct CustomFontMenu: View {
@Binding var letterContent: LetterWriteModel
@Binding var isPopup: Bool
@ObservedObject var fontViewModel: FontSelectionViewModel

var body: some View {
ZStack {
Color.black.opacity(0.1)
.edgesIgnoringSafeArea(.all)
.onTapGesture {
isPopup = false
}

VStack(spacing: 0) {
ForEach(0..<fontViewModel.dummyFonts.count, id: \.self) { i in
Button(action: {
fontViewModel.selectedIndex = i
letterContent.fontString = fontViewModel.dummyFonts[i].font
isPopup = false
}) {
HStack {
Text(fontViewModel.dummyFonts[i].fontName)
.font(FontUtility.selectedFont(font: fontViewModel.dummyFonts[i].font, size: 15))

Spacer()

if fontViewModel.selectedIndex == i {
Image("checked")
.resizable()
.frame(width: 25, height: 25)
}
}
.padding(13)
}
.buttonStyle(.plain)
}
}
.padding(7)
.frame(width: 250)
.background(Color.white)
.cornerRadius(10)
.padding(.top, -(UIScreen.main.bounds.height/2.7))
.shadow(color: Color(.primary300), radius: 5, x: 3, y: 3)
}
}
}


// MARK: - MiniTabBar
struct MiniTabBar: View {
@Binding var letterContent: LetterWriteModel
@ObservedObject var viewModel: ContentWriteViewModel
@ObservedObject var customTabViewModel: CustomTabViewModel

@Binding var isPopup: Bool
@State var isFontEdit: Bool = true

var body: some View {
HStack(alignment: .center) {
Button {
isPopup.toggle()
} label: {
Text("F")
.bold()
.frame(width: UIScreen.main.bounds.width * 0.4/4, height: 30)
.background(isFontEdit ? Color.clear : Color(.primary300))
.clipShape(Capsule())
}
.disabled(isFontEdit ? false : true)
.onChange(of: viewModel.texts) {
if viewModel.texts[0].isEmpty && viewModel.texts.count == 1 {
isFontEdit = true
} else {
isFontEdit = false
}
}

Button {
if viewModel.texts.count > 1 {
viewModel.isDeleteAlertPresented = true
}
} label: {
Image("PageMinus")
.font(.system(size: 15))
.frame(width: UIScreen.main.bounds.width * 0.4/4)
}
.alert(isPresented: $viewModel.isDeleteAlertPresented) {
Alert(
title: Text("Delete Page"),
message: Text("ν˜„μž¬ νŽ˜μ΄μ§€λ₯Ό μ§€μš°μ‹œκ² μ–΄μš”?"),
primaryButton: .destructive(Text("μ‚­μ œ")) {
viewModel.deleteLetter(idx: viewModel.currentIndex)
},
secondaryButton: .cancel(Text("μ·¨μ†Œ")) {
viewModel.isDeleteAlertPresented = false
}
)
}
Button {
viewModel.createNewLetter(idx: viewModel.currentIndex)
} label: {
Image(systemName: "doc.badge.plus")
.font(.system(size: 15))
.frame(width: UIScreen.main.bounds.width * 0.4/4)
}
Button {
customTabViewModel.showPhotoLibrary = true
customTabViewModel.isLetterWrite = true
} label: {
Image(systemName: "photo.on.rectangle.angled")
.font(.system(size: 15))
.frame(width: UIScreen.main.bounds.width * 0.4/4, height: 30)
.background(letterContent.photoContents.isEmpty ? Color.clear : Color.white)
.foregroundStyle(letterContent.photoContents.isEmpty ? Color("ToolBarIcon") : Color(.primary900))
.clipShape(Capsule())
.shadow(color: letterContent.photoContents.isEmpty ? Color.clear : Color(.primary300), radius: 7, x: 3, y: 3)
}
}
.frame(maxWidth: UIScreen.main.bounds.width * 0.5, maxHeight: 40)
.foregroundStyle(Color("ToolBarIcon"))
.background(Color(.primary100))
.clipShape(Capsule())
.shadow(color: Color(.primary300), radius: 5, x: 3, y: 3)
.padding(.top, -10)
}
}

// MARK: - ScrollableLetterView
struct ScrollableLetterView: View {
Expand Down Expand Up @@ -145,9 +233,6 @@ struct ScrollableLetterView: View {
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.top, screenHeight * 0.05)
.padding(.bottom, screenHeight * 0.01)
.onAppear {
print(screenHeight)
}
.onTapGesture {
UIApplication.shared.endEditing()
}
Expand All @@ -157,9 +242,9 @@ struct ScrollableLetterView: View {
text: $viewModel.texts[i],
maxWidth: geo.size.width,
maxHeight: geo.size.height,
font: FontUtility.selectedUIFont(font: letterContent.fontString ?? "", size: FontUtility.fontSize(font: letterContent.fontString ?? "")),
lineSpacing: FontUtility.lineSpacing(font: letterContent.fontString ?? ""),
kerning: FontUtility.kerning(font: letterContent.fontString ?? "")
font: FontUtility.selectedUIFont(font: letterContent.fontString ?? "", size: FontUtility.fontSize(font: letterContent.fontString ?? ""))
// lineSpacing: FontUtility.lineSpacing(font: letterContent.fontString ?? ""),
// kerning: FontUtility.kerning(font: letterContent.fontString ?? "")
)
}
.onChange(of: viewModel.texts[i]) {
Expand Down Expand Up @@ -193,7 +278,6 @@ struct ScrollableLetterView: View {
.scrollTargetLayout()
}
.scrollTargetBehavior(.viewAligned)
.font(FontUtility.selectedFont(font: letterContent.fontString ?? "", size: 14))
.onChange(of: viewModel.texts.count) {
withAnimation {
scrollViewProxy.scrollTo((currentIndex+1), anchor: .center)
Expand Down Expand Up @@ -231,8 +315,8 @@ struct CustomTextEditor: UIViewRepresentable {
var maxWidth: CGFloat
var maxHeight: CGFloat
var font: UIFont
var lineSpacing: CGFloat
var kerning: CGFloat
// var lineSpacing: CGFloat
// var kerning: CGFloat

let maxCharacterLimit: Int = 397

Expand Down Expand Up @@ -285,16 +369,14 @@ struct CustomTextEditor: UIViewRepresentable {
return
}

uiView.attributedText = createAttributedString(text: text, font: font, lineSpacing: lineSpacing, kerning: kerning)
uiView.attributedText = createAttributedString(text: text, font: font)
}

private func createAttributedString(text: String, font: UIFont, lineSpacing: CGFloat, kerning: CGFloat) -> NSAttributedString {
private func createAttributedString(text: String, font: UIFont) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing

let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.kern: kerning,
.paragraphStyle: paragraphStyle
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ struct StationerySelectionView: View {
.navigationTitle("νŽΈμ§€μ§€ κ³ λ₯΄κΈ°")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
NavigationLink(destination: FontSelectionView(
NavigationLink(destination: ContentWriteView(
letterContent: $letterContent,
customViewModel: customViewModel,
imageViewModel: imageViewModel
imageViewModel: imageViewModel,
customTabViewModel: customViewModel
)) {
Text("λ‹€μŒ")
.fontWeight(.medium)
Expand Down