Skip to content

Commit 9ead47a

Browse files
committed
♻️ more readable data loading
1 parent c0e5de7 commit 9ead47a

3 files changed

Lines changed: 63 additions & 15 deletions

File tree

LocalizationEditor/Providers/LocalizationProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class LocalizationProvider {
2222
// MARK: Actions
2323

2424
/**
25-
Updates given localization values in given localization file. Basially regenerates the whole localization files chagning the given value
25+
Updates given localization values in given localization file. Basially regenerates the whole localization files changing the given value
2626

2727
- Parameter localization: localization to update
2828
- Parameter string: localization string

LocalizationEditor/Providers/LocalizationsDataSource.swift

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
// Copyright © 2018 Igor Kulman. All rights reserved.
77
//
88

9+
import CleanroomLogger
910
import Cocoa
1011
import Foundation
1112

13+
typealias LocalizationsDataSourceData = ([String], String?, [LocalizationGroup])
14+
1215
/**
1316
Data source for the NSTableView with localizations
1417
*/
@@ -24,44 +27,89 @@ class LocalizationsDataSource: NSObject, NSTableViewDataSource {
2427

2528
// MARK: - Actions
2629

27-
func load(folder: URL, onCompletion: @escaping ([String], String?, [LocalizationGroup]) -> Void) {
28-
DispatchQueue.global(qos: .background).async {
29-
self.localizationGroups = self.localizationProvider.getLocalizations(url: folder)
30-
if let group = self.localizationGroups.first(where: { $0.name == "Localizable.strings" }) ?? self.localizationGroups.first {
31-
let languages = self.select(group: group)
30+
/**
31+
Loads data for directory at given path
3232

33+
- Parameter folder: directory path to start the search
34+
- Parameter onCompletion: callback with data
35+
*/
36+
func load(folder: URL, onCompletion: @escaping (LocalizationsDataSourceData) -> Void) {
37+
DispatchQueue.global(qos: .background).async {
38+
let localizationGroups = self.localizationProvider.getLocalizations(url: folder)
39+
guard localizationGroups.count > 0, let group = localizationGroups.first(where: { $0.name == "Localizable.strings" }) ?? localizationGroups.first else {
40+
Log.error?.message("No localization data found")
3341
DispatchQueue.main.async {
34-
onCompletion(languages, self.selectedLocalizationGroup?.name, self.localizationGroups)
42+
onCompletion(([], nil, []))
3543
}
44+
return
45+
}
46+
47+
self.localizationGroups = localizationGroups
48+
self.selectedLocalizationGroup = group
49+
let languages = self.getLanguages(for: group)
50+
51+
DispatchQueue.main.async {
52+
onCompletion((languages, group.name, localizationGroups))
3653
}
3754
}
3855
}
3956

40-
func select(name: String) -> [String] {
41-
let group = localizationGroups.first(where: { $0.name == name })!
42-
return select(group: group)
57+
/**
58+
Gets available languges for given group
59+
60+
- Parameter group: group name
61+
- Returns: array of languages
62+
*/
63+
func getLanguages(for group: String) -> [String] {
64+
let group = localizationGroups.first(where: { $0.name == group })!
65+
return getLanguages(for: group)
4366
}
4467

45-
func select(group: LocalizationGroup) -> [String] {
46-
selectedLocalizationGroup = group
68+
/**
69+
Gets available languges for given group
70+
71+
- Parameter group: localization group
72+
- Returns: array of languages
73+
*/
74+
private func getLanguages(for group: LocalizationGroup) -> [String] {
4775
localizations = selectedLocalizationGroup?.localizations ?? []
4876
numberOfKeys = localizations.map({ $0.translations.count }).max() ?? 0
49-
masterLocalization = localizations.first(where: { $0.translations.count == self.numberOfKeys })
77+
masterLocalization = localizations.first(where: { $0.translations.count == numberOfKeys })
5078

5179
return localizations.map({ $0.language })
5280
}
5381

82+
/**
83+
Gets key for speficied row
84+
85+
- Parameter row: row number
86+
- Returns: key if valid
87+
*/
5488
func getKey(row: Int) -> String? {
5589
return (row < masterLocalization?.translations.count ?? 0) ? masterLocalization?.translations[row].key : nil
5690
}
5791

92+
/**
93+
Gets localization for specified language and row. The language should be always valid. The localization might be missing, returning it with empty value in that case
94+
95+
- Parameter language: language to get the localization for
96+
- Parameter row: row number
97+
- Returns: localiyation string
98+
*/
5899
func getLocalization(language: String, row: Int) -> LocalizationString {
59100
guard let localization = localizations.first(where: { $0.language == language }), let masterLocalization = masterLocalization else {
60101
fatalError("Could not get localization for \(language) or master localization not present")
61102
}
62103
return localization.translations.first(where: { $0.key == masterLocalization.translations[row].key }) ?? LocalizationString(key: masterLocalization.translations[row].key, value: "")
63104
}
64105

106+
/**
107+
Updates given localization values in given language
108+
109+
- Parameter language: language to update
110+
- Parameter string: localization string
111+
- Parameter value: new value for the localization string
112+
*/
65113
func updateLocalization(language: String, string: LocalizationString, with value: String) {
66114
guard let localization = localizations.first(where: { $0.language == language }) else {
67115
return

LocalizationEditor/UI/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ class ViewController: NSViewController {
9292
// MARK: - Actions
9393

9494
@IBAction @objc private func selectAction(sender: NSMenuItem) {
95-
let title = sender.title
96-
let languages = dataSource.select(name: title)
95+
let groupName = sender.title
96+
let languages = dataSource.getLanguages(for: groupName)
9797

9898
reloadData(with: languages, title: title)
9999
}

0 commit comments

Comments
 (0)