Skip to content

Commit

Permalink
Add section filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
EgzonArifi committed Nov 28, 2023
1 parent 02806a0 commit c912b89
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Lingua-App/Lingua/Lingua.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
774A1A462A8E588500789A04 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 774A1A452A8E588500789A04 /* ContentView.swift */; };
774A1A482A8E588800789A04 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 774A1A472A8E588800789A04 /* Assets.xcassets */; };
774A1A562A8E588800789A04 /* LinguaTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 774A1A552A8E588800789A04 /* LinguaTests.swift */; };
777814B02B13D5C700A1B678 /* SectionsInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 777814AF2B13D5C700A1B678 /* SectionsInputView.swift */; };
777BC5E12A93617000C74D72 /* ValidatingTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 777BC5E02A93617000C74D72 /* ValidatingTextField.swift */; };
777BC5E42A9361E200C74D72 /* ValidationRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 777BC5E32A9361E200C74D72 /* ValidationRule.swift */; };
777BC5E62A9361F000C74D72 /* RequiredRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 777BC5E52A9361F000C74D72 /* RequiredRule.swift */; };
Expand Down Expand Up @@ -65,6 +66,7 @@
774A1A4C2A8E588800789A04 /* Lingua.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Lingua.entitlements; sourceTree = "<group>"; };
774A1A512A8E588800789A04 /* LinguaTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LinguaTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
774A1A552A8E588800789A04 /* LinguaTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinguaTests.swift; sourceTree = "<group>"; };
777814AF2B13D5C700A1B678 /* SectionsInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionsInputView.swift; sourceTree = "<group>"; };
777BC5E02A93617000C74D72 /* ValidatingTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidatingTextField.swift; sourceTree = "<group>"; };
777BC5E32A9361E200C74D72 /* ValidationRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidationRule.swift; sourceTree = "<group>"; };
777BC5E52A9361F000C74D72 /* RequiredRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequiredRule.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -205,6 +207,7 @@
777BC62E2A94B2A000C74D72 /* ProgressOverlay.swift */,
779F91F82A978AC8009C77D5 /* InformationHolderView.swift */,
77F89A472A98DF3A009EE7B1 /* CustomSearchBar.swift */,
777814AF2B13D5C700A1B678 /* SectionsInputView.swift */,
);
path = UI;
sourceTree = "<group>";
Expand Down Expand Up @@ -441,6 +444,7 @@
777BC5EF2A938B8400C74D72 /* ProjectFormView.swift in Sources */,
774A1A442A8E588500789A04 /* LinguaApp.swift in Sources */,
777BC5E82A93639E00C74D72 /* DirectoryInputField.swift in Sources */,
777814B02B13D5C700A1B678 /* SectionsInputView.swift in Sources */,
970D1F802B10EC3F00F6CD8E /* Window.swift in Sources */,
777BC62F2A94B2A000C74D72 /* ProgressOverlay.swift in Sources */,
777BC5E42A9361E200C74D72 /* ValidationRule.swift in Sources */,
Expand Down
14 changes: 12 additions & 2 deletions Lingua-App/Lingua/Lingua/Model/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ struct Project: Identifiable, Hashable, Equatable, Codable {
var directoryPath: String
var title: String
var swiftCode: SwiftCode
var swiftCodeEnabled: Bool = true
var swiftCodeEnabled: Bool
var createdAt: Date
var lastLocalizedAt: Date?
var filterSectionsEnabled: Bool
var allowedSections: [String]

init(id: UUID,
type: LocalizationPlatform,
Expand All @@ -27,17 +29,23 @@ struct Project: Identifiable, Hashable, Equatable, Codable {
directoryPath: String = "",
title: String = "",
swiftCode: SwiftCode = .init(stringsDirectory: "", outputSwiftCodeFileDirectory: ""),
swiftCodeEnabled: Bool = true,
createdAt: Date = .init(),
lastLocalizedAt: Date? = nil) {
lastLocalizedAt: Date? = nil,
filterSectionsEnabled: Bool = false,
allowedSections: [String] = []) {
self.id = id
self.type = type
self.apiKey = apiKey
self.sheetId = sheetId
self.directoryPath = directoryPath
self.title = title
self.swiftCode = swiftCode
self.swiftCodeEnabled = swiftCodeEnabled
self.createdAt = createdAt
self.lastLocalizedAt = lastLocalizedAt
self.filterSectionsEnabled = filterSectionsEnabled
self.allowedSections = allowedSections
}

/// Custom initializer for decoding [Project] from persisted storage.
Expand All @@ -57,6 +65,8 @@ struct Project: Identifiable, Hashable, Equatable, Codable {
swiftCodeEnabled = try container.decode(Bool.self, forKey: .swiftCodeEnabled)
createdAt = try container.decodeIfPresent(Date.self, forKey: .createdAt) ?? Date()
lastLocalizedAt = try container.decodeIfPresent(Date.self, forKey: .lastLocalizedAt)
filterSectionsEnabled = try container.decodeIfPresent(Bool.self, forKey: .filterSectionsEnabled) ?? false
allowedSections = try container.decodeIfPresent([String].self, forKey: .allowedSections) ?? []
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct ProjectFormView: View {
Form {
basicConfigurationFormSection()
swiftCodeFormSection()
filterSectionsFormSection()
iOSInfoFormSection()
}
.toolbar {
Expand Down Expand Up @@ -157,6 +158,19 @@ private extension ProjectFormView {
}
}

@ViewBuilder
func filterSectionsFormSection() -> some View {
Section {
Toggle(isOn: $viewModel.project.filterSectionsEnabled) {
Text("Enable sections filtering...")
.bold()
if viewModel.project.filterSectionsEnabled {
SectionsInputView(sections: $viewModel.project.allowedSections)
}
}
}
}

@ViewBuilder
func iOSInfoFormSection() -> some View {
if viewModel.project.type == .ios {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// TagInputView.swift
// Lingua
//
// Created by Egzon Arifi on 26/11/2023.
//

import SwiftUI

struct SectionsInputView: View {
@State var currentInput: String = ""
@Binding var sections: [String]

var body: some View {
VStack(alignment: .leading) {
HStack {
TextField("Enter a section", text: $currentInput, onCommit: addSection)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()

Button(action: addSection) {
Text("Add section")
}
}

Divider()

HStack {
ForEach(sections, id: \.self) { tag in
Button(action: {
removeSection(tag)
}, label: {
Text(tag)
.padding(.vertical, 4)

Image(systemName: "xmark")
})
}
}
}
}

private func addSection() {
if !currentInput.isEmpty {
sections.append(currentInput)
currentInput = ""
}
}

private func removeSection(_ tag: String) {
sections.removeAll { $0 == tag }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ struct LocalizationManager {
let config = Config.Localization(apiKey: project.apiKey,
sheetId: project.sheetId,
outputDirectory: project.directoryPath,
localizedSwiftCode: localizedSwiftCode)
localizedSwiftCode: localizedSwiftCode,
allowedSections: project.filterSectionsEnabled ? project.allowedSections : .none)
let module = LocalizationModuleFactory.make(config: config)

try await module.localize(for: project.type)
Expand Down
10 changes: 9 additions & 1 deletion Sources/LinguaLib/Domain/Entities/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ public extension Config {
let sheetId: String
let outputDirectory: String
let localizedSwiftCode: LocalizedSwiftCode?
let allowedSections: [String]?

public init(apiKey: String, sheetId: String, outputDirectory: String, localizedSwiftCode: LocalizedSwiftCode?) {
public init(
apiKey: String,
sheetId: String,
outputDirectory: String,
localizedSwiftCode: LocalizedSwiftCode?,
allowedSections: [String]? = nil
) {
self.apiKey = apiKey
self.sheetId = sheetId
self.outputDirectory = outputDirectory
self.localizedSwiftCode = localizedSwiftCode
self.allowedSections = allowedSections
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ extension LocalizedFilesGenerator: LocalizedFilesGenerating {
let sections = Dictionary(grouping: sheet.entries, by: { $0.section })

for (sectionName, sectionEntries) in sections {
if let allowedSections = config.allowedSections,
!allowedSections.contains(where: { $0.lowercased() == sectionName.lowercased() }) {
continue
}
try filesGenerator.createPlatformFiles(for: sectionEntries,
sectionName: sectionName.formatSheetSection(),
outputFolder: outputFolder,
Expand Down

0 comments on commit c912b89

Please sign in to comment.