Skip to content

Commit

Permalink
Implementent possibility to change the style of the app by chaning in…
Browse files Browse the repository at this point in the history
… in the CustomizerVIew. Added modifiers to the text in order to change the font height parapgh spacing and all the required ones by the class RCpreset.

-Bug: When opening the library and closing it, the customizer view won't open again.
  • Loading branch information
Luca committed Mar 4, 2024
1 parent f692da0 commit 0ac5447
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 27 deletions.
10 changes: 10 additions & 0 deletions Bilbary.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
46A485BA2B87392B00C6C7E3 /* EpubHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A485B72B87392B00C6C7E3 /* EpubHelper.swift */; };
46A485BB2B87392B00C6C7E3 /* EpubView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A485B82B87392B00C6C7E3 /* EpubView.swift */; };
A07E40472B8CAACE00E5E1F2 /* GenericInfoCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = A07E40462B8CAACE00E5E1F2 /* GenericInfoCard.swift */; };
A0832E802B95D636001CB444 /* FontCustomModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0832E7F2B95D636001CB444 /* FontCustomModel.swift */; };
A0832E832B95E78F001CB444 /* TextExtention.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0832E822B95E78F001CB444 /* TextExtention.swift */; };
A0832E842B95E78F001CB444 /* TextExtention.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0832E822B95E78F001CB444 /* TextExtention.swift */; };
A08CE2952B8CA0960054400D /* GeneralLibraryStructs.swift in Sources */ = {isa = PBXBuildFile; fileRef = A08CE2942B8CA0960054400D /* GeneralLibraryStructs.swift */; };
A0B3CBFD2B91D95D00DE5DA1 /* BConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0B3CBFC2B91D95D00DE5DA1 /* BConstants.swift */; };
A0B3CBFE2B91D95D00DE5DA1 /* BConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0B3CBFC2B91D95D00DE5DA1 /* BConstants.swift */; };
Expand Down Expand Up @@ -157,6 +160,8 @@
46A485B72B87392B00C6C7E3 /* EpubHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EpubHelper.swift; sourceTree = "<group>"; };
46A485B82B87392B00C6C7E3 /* EpubView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EpubView.swift; sourceTree = "<group>"; };
A07E40462B8CAACE00E5E1F2 /* GenericInfoCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenericInfoCard.swift; sourceTree = "<group>"; };
A0832E7F2B95D636001CB444 /* FontCustomModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontCustomModel.swift; sourceTree = "<group>"; };
A0832E822B95E78F001CB444 /* TextExtention.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextExtention.swift; sourceTree = "<group>"; };
A08CE2942B8CA0960054400D /* GeneralLibraryStructs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralLibraryStructs.swift; sourceTree = "<group>"; };
A0B3CBFC2B91D95D00DE5DA1 /* BConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BConstants.swift; sourceTree = "<group>"; };
A0CCC1912B8C93D90086578C /* LibraryBookCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LibraryBookCard.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -351,6 +356,7 @@
463CEDCF2B8503A8001D951C /* UIWindow.swift */,
462E0DCB2B87AC98001D2161 /* NavigationSplitViewVisibility.swift */,
462E0DFA2B8C98CF001D2161 /* Color.swift */,
A0832E822B95E78F001CB444 /* TextExtention.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand All @@ -370,6 +376,7 @@
463CEDDC2B854B77001D951C /* Models */ = {
isa = PBXGroup;
children = (
A0832E7F2B95D636001CB444 /* FontCustomModel.swift */,
463CEDD72B854579001D951C /* Book.swift */,
462E0DE02B889433001D2161 /* Content */,
462D50252B8412F0001ED44E /* Customization */,
Expand Down Expand Up @@ -646,6 +653,7 @@
A07E40472B8CAACE00E5E1F2 /* GenericInfoCard.swift in Sources */,
A0CCC1922B8C93D90086578C /* LibraryBookCard.swift in Sources */,
462739AE2B83A24A00B085FF /* GoalStreakView.swift in Sources */,
A0832E832B95E78F001CB444 /* TextExtention.swift in Sources */,
460B36272B7B7D8300D99FFF /* MainView.swift in Sources */,
462E0DF72B88B83E001D2161 /* RCPreset.swift in Sources */,
462D501D2B8409B2001ED44E /* ReadTime+String.swift in Sources */,
Expand All @@ -663,6 +671,7 @@
462D502B2B841405001ED44E /* RCustomizerModel+RCustomizerModelStorage.swift in Sources */,
462E0DC72B879EBF001D2161 /* BottomBar.swift in Sources */,
462E0DE22B88943C001D2161 /* ContentModel.swift in Sources */,
A0832E802B95D636001CB444 /* FontCustomModel.swift in Sources */,
463CEDD82B854579001D951C /* Book.swift in Sources */,
463CEDDA2B8545E0001D951C /* BookInformationView.swift in Sources */,
462D50192B8407E7001ED44E /* GoalStreakDuration.swift in Sources */,
Expand Down Expand Up @@ -703,6 +712,7 @@
buildActionMask = 2147483647;
files = (
463CEDFA2B8569BE001D951C /* MainView.swift in Sources */,
A0832E842B95E78F001CB444 /* TextExtention.swift in Sources */,
462E0DDC2B880098001D2161 /* ScreenWidthKey.swift in Sources */,
462E0DCD2B87AC98001D2161 /* NavigationSplitViewVisibility.swift in Sources */,
A0B3CBFE2B91D95D00DE5DA1 /* BConstants.swift in Sources */,
Expand Down
14 changes: 7 additions & 7 deletions Bilbary/Models/FontCustomModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import SwiftUI

class FontSizeModel: ObservableObject {
@Published var fontSize: CGFloat = 16
class FontCustomModel: ObservableObject, Equatable {
static let shared = FontCustomModel()

func increaseFontSize() {
fontSize += 2
static func == (lhs: FontCustomModel, rhs: FontCustomModel) -> Bool {
return lhs.fontStyle == rhs.fontStyle
}

func decreaseFontSize() {
fontSize -= 2
}
private init() {}

@Published var fontStyle: RCPreset = .focus
}
61 changes: 60 additions & 1 deletion Bilbary/Utilities/Extensions/TextExtention.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,64 @@
//
// Created by Luca Gargiulo on 04/03/24.
//
import SwiftUI

import Foundation
// Custom environment key for word spacing
struct WordSpacingKey: EnvironmentKey {
static var defaultValue: CGFloat = 1.0
}

// Custom environment key for line height
struct LineHeightKey: EnvironmentKey {
static var defaultValue: CGFloat = 1.0
}

// Custom environment key for paragraph spacing
struct ParagraphSpacingKey: EnvironmentKey {
static var defaultValue: CGFloat = 0.0
}

// Extension to easily access the environment values
extension EnvironmentValues {
var wordSpacing: CGFloat {
get { self[WordSpacingKey.self] }
set { self[WordSpacingKey.self] = newValue }
}

var lineHeight: CGFloat {
get { self[LineHeightKey.self] }
set { self[LineHeightKey.self] = newValue }
}

var paragraphSpacing: CGFloat {
get { self[ParagraphSpacingKey.self] }
set { self[ParagraphSpacingKey.self] = newValue }
}
}

// Custom view modifier for word spacing
struct WordSpacing: ViewModifier {
var spacing: CGFloat

func body(content: Content) -> some View {
content.environment(\.wordSpacing, spacing)
}
}

// Custom view modifier for line height
struct LineHeight: ViewModifier {
var height: CGFloat

func body(content: Content) -> some View {
content.environment(\.lineHeight, height)
}
}

// Custom view modifier for paragraph spacing
struct ParagraphSpacing: ViewModifier {
var spacing: CGFloat

func body(content: Content) -> some View {
content.environment(\.paragraphSpacing, spacing)
}
}
30 changes: 28 additions & 2 deletions Bilbary/Views/BookView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,37 @@ import SwiftUI

struct BookView: View {

@ObservedObject var fontCustomModel = FontCustomModel.shared

var book: Book?

var body: some View {
// swiftlint:disable:next line_length
Text("He was still a handsome man, with a tanned, chiseled face and long, thick, wavy white hair, but his cells had begun to reproduce in a haphazard fashion, destroying the DNA of neighboring cells and secreting toxins into his body.")
Group {
// swiftlint:disable:next line_length
Text("He was still a handsome man, with a tanned, chiseled face and long, thick, wavy white hair, but his cells had begun to reproduce in a haphazard fashion, destroying the DNA of neighboring cells and secreting toxins into his body.")
.font(.custom(fontCustomModel.fontStyle.fontFace.rawValue, size: convertFontSize(from: fontCustomModel.fontStyle.fontSize)))
.foregroundStyle(fontCustomModel.fontStyle.fontColor)
.modifier(WordSpacing(spacing: fontCustomModel.fontStyle.wordSpacing))
.modifier(LineHeight(height: fontCustomModel.fontStyle.lineHeight))
.modifier(ParagraphSpacing(spacing: fontCustomModel.fontStyle.paragraphSpacing))
}
}

func convertFontSize(from contentSizeCategory: UIContentSizeCategory) -> CGFloat {
switch contentSizeCategory {
case .extraSmall:
return 12
case .small:
return 14
case .medium:
return 16
case .large:
return 18
case .extraLarge:
return 20
default:
return 16
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion Bilbary/Views/ContentView/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ struct ContentView: View {

@State
private var view: RViewModel = .shared


@ObservedObject var fontCustomModel = FontCustomModel.shared

private let logger: Logger = .views

var body: some View {
Expand All @@ -35,6 +37,7 @@ struct ContentView: View {
.frame(minWidth: view.proxy?.size.width ?? UIWindow.current?.screen.bounds.width)
.frame(maxWidth: view.proxy?.size.width ?? UIWindow.current?.screen.bounds.width)
}
.background(fontCustomModel.fontStyle.backgroundColor)
.scrollDisabled(true)
.onTapGesture {
withAnimation {
Expand Down
1 change: 0 additions & 1 deletion Bilbary/Views/LibraryBookCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ struct LibraryBookCard: View {
onTap()
}
}

}

// PreviewProvider
Expand Down
3 changes: 3 additions & 0 deletions Bilbary/Views/LibraryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct LibraryView: View {
@State
private var view: RViewModel = .shared

@ObservedObject var fontCustomModel = FontCustomModel.shared

@State
private var viewOption = 0

Expand Down Expand Up @@ -107,6 +109,7 @@ struct LibraryView: View {
}
}
}
.background(fontCustomModel.fontStyle.backgroundColor)
.contentShape(Rectangle())
.toolbar(.hidden, for: .navigationBar)
.searchable(text: $searchText)
Expand Down
41 changes: 26 additions & 15 deletions Bilbary/Views/Popovers/CustomizerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import SwiftUI

struct CustomizerView: View {

@ObservedObject var fontCustomModel = FontCustomModel.shared

@State
private var customizer: RCustomizerModel = .shared

Expand Down Expand Up @@ -40,22 +42,26 @@ struct CustomizerView: View {
VStack {
LazyVGrid(columns: columns, spacing: 2) {
ForEach(styles) { style in
VStack {
Text(
getAttibutedString(from: style)
)
Button(action: {
hello(style: style)
}) {
VStack {
Text(
getAttibutedString(from: style)
)
}
.padding()
.frame(width: 192, height: 128)
.foregroundColor(Color(style.fontColor))
.background(style.backgroundColor)
.overlay {
RoundedRectangle(cornerRadius: 12)
.stroke(Color(white: 0, opacity: 0.5), lineWidth: 0.2)
.foregroundStyle(.clear)
}
.clipShape(RoundedRectangle(cornerRadius: 12))
.tag(style.id)
}
.padding()
.frame(width: 192, height: 128)
.foregroundColor(Color(style.fontColor))
.background(style.backgroundColor)
.overlay {
RoundedRectangle(cornerRadius: 12)
.stroke(Color(white: 0, opacity: 0.5), lineWidth: 0.2)
.foregroundStyle(.clear)
}
.clipShape(RoundedRectangle(cornerRadius: 12))
.tag(style.id)
}
}
}
Expand All @@ -66,6 +72,11 @@ struct CustomizerView: View {
self.selectedId = customizer.style.id
}
}

func hello(style: RCPreset)
{
fontCustomModel.fontStyle = style
}
}

#Preview {
Expand Down

0 comments on commit 0ac5447

Please sign in to comment.