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

feat: Move organizationName to CrowdinProviderConfig #233

Merged
merged 8 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Sources/CrowdinSDK/CrowdinAPI/CrowdinAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CrowdinAPI: BaseAPI {
baseURL + apiPath
}

init(organizationName: String? = nil, auth: CrowdinAuth? = nil, session: URLSession = .shared) {
init(organizationName: String?, auth: CrowdinAuth? = nil, session: URLSession = .shared) {
self.organizationName = organizationName
self.auth = auth
super.init(session: session)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
class DistributionsAPI: CrowdinAPI {
let hashString: String

init(hashString: String, organizationName: String? = nil, auth: CrowdinAuth? = nil, session: URLSession = .shared) {
init(hashString: String, organizationName: String?, auth: CrowdinAuth? = nil, session: URLSession = .shared) {
self.hashString = hashString
super.init(organizationName: organizationName, auth: auth, session: session)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/CrowdinSDK/CrowdinAPI/LoginAPI/LoginAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LoginAPI: BaseAPI {
var scope: String
var redirectURI: String

init(clientId: String, clientSecret: String, scope: String, redirectURI: String, organizationName: String? = nil, session: URLSession = URLSession.shared) {
init(clientId: String, clientSecret: String, scope: String, redirectURI: String, organizationName: String?, session: URLSession = URLSession.shared) {
self.clientId = clientId
self.clientSecret = clientSecret
self.scope = scope
Expand Down
7 changes: 0 additions & 7 deletions Sources/CrowdinSDK/CrowdinSDK/CrowdinSDKConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,4 @@ import Foundation
public static func config() -> CrowdinSDKConfig {
return CrowdinSDKConfig()
}

var enterprise: Bool = false

func with(enterprise: Bool) -> Self {
self.enterprise = enterprise
return self
}
}
17 changes: 3 additions & 14 deletions Sources/CrowdinSDK/Features/LoginFeature/CrowdinLoginConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,17 @@ private let URLSchemeDocumentationLink = "https://developer.apple.com/documentat
var clientSecret: String
var scope: String
var redirectURI: String
var organizationName: String? = nil

public convenience init(clientId: String, clientSecret: String, scope: String) throws {
guard let redirectURI = Bundle.main.urlSchemes?.first else { throw NSError(domain: "Application do not support any URL Scheme. To setup it, please check - \(URLSchemeDocumentationLink)", code: defaultCrowdinErrorCode, userInfo: nil) }
try self.init(with: clientId, clientSecret: clientSecret, scope: scope, redirectURI: redirectURI, organizationName: nil)
try self.init(with: clientId, clientSecret: clientSecret, scope: scope, redirectURI: redirectURI)
}

public convenience init(clientId: String, clientSecret: String, scope: String, redirectURI: String) throws {
try self.init(with: clientId, clientSecret: clientSecret, scope: scope, redirectURI: redirectURI, organizationName: nil)
try self.init(with: clientId, clientSecret: clientSecret, scope: scope, redirectURI: redirectURI)
}

public convenience init(clientId: String, clientSecret: String, scope: String, organizationName: String) throws {
guard let redirectURI = Bundle.main.urlSchemes?.first else { throw NSError(domain: "Application do not support any URL Scheme. To setup it, please check - \(URLSchemeDocumentationLink)", code: defaultCrowdinErrorCode, userInfo: nil) }
try self.init(with: clientId, clientSecret: clientSecret, scope: scope, redirectURI: redirectURI, organizationName: organizationName)
}

public convenience init(clientId: String, clientSecret: String, scope: String, redirectURI: String, organizationName: String) throws {
try self.init(with: clientId, clientSecret: clientSecret, scope: scope, redirectURI: redirectURI, organizationName: organizationName)
}

private init(with clientId: String, clientSecret: String, scope: String, redirectURI: String, organizationName: String? = nil) throws {
private init(with clientId: String, clientSecret: String, scope: String, redirectURI: String) throws {
guard !clientId.isEmpty else { throw NSError(domain: "clientId could not be empty.", code: defaultCrowdinErrorCode, userInfo: nil) }
self.clientId = clientId
guard !clientSecret.isEmpty else { throw NSError(domain: "clientSecret could not be empty.", code: defaultCrowdinErrorCode, userInfo: nil) }
Expand All @@ -44,6 +34,5 @@ private let URLSchemeDocumentationLink = "https://developer.apple.com/documentat
guard !redirectURI.isEmpty else { throw NSError(domain: "redirectURI could not be empty.", code: defaultCrowdinErrorCode, userInfo: nil) }
guard let urlSchemes = Bundle.main.urlSchemes, urlSchemes.contains(redirectURI) else { throw NSError(domain: "Application supported url schemes should contain \(redirectURI)", code: defaultCrowdinErrorCode, userInfo: nil) }
self.redirectURI = redirectURI
self.organizationName = organizationName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension CrowdinSDK {
guard let config = CrowdinSDK.config else { return }
guard let loginConfig = config.loginConfig else { return }
guard let hash = config.crowdinProviderConfig?.hashString else { return }
LoginFeature.configureWith(with: hash, loginConfig: loginConfig)
LoginFeature.configureWith(with: hash, organizationName: config.crowdinProviderConfig?.organizationName, loginConfig: loginConfig)
}

public class func login() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ extension CrowdinSDKConfig {

public func with(loginConfig: CrowdinLoginConfig) -> Self {
self.loginConfig = loginConfig
self.enterprise = loginConfig.organizationName != nil
return self
}
}
10 changes: 5 additions & 5 deletions Sources/CrowdinSDK/Features/LoginFeature/LoginFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Foundation
protocol LoginFeatureProtocol {
static var shared: Self? { get }
static var isLogined: Bool { get }
static func configureWith(with hash: String, loginConfig: CrowdinLoginConfig)
static func configureWith(with hash: String, organizationName: String?, loginConfig: CrowdinLoginConfig)

func login(completion: @escaping () -> Void, error: @escaping (Error) -> Void)
func relogin(completion: @escaping () -> Void, error: @escaping (Error) -> Void)
Expand All @@ -34,9 +34,9 @@ final class LoginFeature: NSObject, LoginFeatureProtocol, CrowdinAuth {
fileprivate var safariVC: SFSafariViewController?
#endif

init(hashString: String, config: CrowdinLoginConfig) {
init(hashString: String, organizationName: String?, config: CrowdinLoginConfig) {
self.config = config
self.loginAPI = LoginAPI(clientId: config.clientId, clientSecret: config.clientSecret, scope: config.scope, redirectURI: config.redirectURI, organizationName: config.organizationName)
self.loginAPI = LoginAPI(clientId: config.clientId, clientSecret: config.clientSecret, scope: config.scope, redirectURI: config.redirectURI, organizationName: organizationName)
super.init()
if self.hashString != hashString {
self.logout()
Expand All @@ -45,8 +45,8 @@ final class LoginFeature: NSObject, LoginFeatureProtocol, CrowdinAuth {
NotificationCenter.default.addObserver(self, selector: #selector(receiveUnautorizedResponse), name: .CrowdinAPIUnautorizedNotification, object: nil)
}

static func configureWith(with hashString: String, loginConfig: CrowdinLoginConfig) {
LoginFeature.shared = LoginFeature(hashString: hashString, config: loginConfig)
static func configureWith(with hashString: String, organizationName: String?, loginConfig: CrowdinLoginConfig) {
LoginFeature.shared = LoginFeature(hashString: hashString, organizationName: organizationName, config: loginConfig)
}

var hashString: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension CrowdinSDK {
guard let config = CrowdinSDK.config else { return }
let crowdinProviderConfig = config.crowdinProviderConfig ?? CrowdinProviderConfig()
if config.realtimeUpdatesEnabled {
RealtimeUpdateFeature.shared = RealtimeUpdateFeature(hash: crowdinProviderConfig.hashString, sourceLanguage: crowdinProviderConfig.sourceLanguage, organizationName: config.loginConfig?.organizationName)
RealtimeUpdateFeature.shared = RealtimeUpdateFeature(hash: crowdinProviderConfig.hashString, sourceLanguage: crowdinProviderConfig.sourceLanguage, organizationName: config.crowdinProviderConfig?.organizationName)
swizzleControlMethods()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class RUFilesDownloader: CrowdinDownloaderProtocol {
let projectsAPI: ProjectsAPI
let projectId: String
let enterprise: Bool
let languageResolver: LanguageResolver
let manifestManager: ManifestManager

init(projectId: String, laguageResolver: LanguageResolver, organizationName: String? = nil) {
init(projectId: String, manifestManager: ManifestManager, organizationName: String?) {
self.projectId = projectId
self.languageResolver = laguageResolver
self.manifestManager = manifestManager
self.enterprise = organizationName != nil
self.projectsAPI = ProjectsAPI(organizationName: organizationName, auth: LoginFeature.shared)
self.operationQueue.maxConcurrentOperationCount = 1
Expand All @@ -51,7 +51,7 @@ class RUFilesDownloader: CrowdinDownloaderProtocol {
}

fileIDs.forEach { (fileId) in
let targetLanguageId = languageResolver.crowdinLanguageCode(for: localization) ?? localization
let targetLanguageId = manifestManager.crowdinLanguageCode(for: localization) ?? localization
let download = FileDataDownloadOperation(fileId: fileId, projectId: projectId, targetLanguageId: targetLanguageId, projectsAPI: projectsAPI) { [weak self] (data, error) in
guard let self = self else {
return
Expand Down Expand Up @@ -107,10 +107,9 @@ class RUFilesDownloader: CrowdinDownloaderProtocol {
}

func getFiles(for hash: String, completion: @escaping ([String]?, Error?) -> Void) {
let manifestManager = ManifestManager.manifest(for: hash)
manifestManager.download { [weak self] in
guard let self = self else { return }
guard let files = manifestManager.files else { completion(nil, nil); return; }
guard let files = self.manifestManager.files else { completion(nil, nil); return; }
let fileNames = files.compactMap({ $0.split(separator: "/").last }).map({ String($0) })
self.getAllProjectFiles { (projectFiles, error) in
guard let projectFiles = projectFiles else { completion(nil, error); return; }
Expand All @@ -126,9 +125,9 @@ class RUFilesDownloader: CrowdinDownloaderProtocol {
}

func getLangiages(for hash: String, completion: @escaping ([String]?, Error?) -> Void) {
let manifestManager = ManifestManager.manifest(for: hash)
manifestManager.download {
completion(manifestManager.languages, nil)
manifestManager.download { [weak self] in
guard let self = self else { return }
completion(self.manifestManager.languages, nil)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class RURemoteLocalizationStorage: RemoteLocalizationStorageProtocol {
init(localization: String, hash: String, projectId: String, organizationName: String?) {
self.localization = localization
self.hash = hash
manifestManager = ManifestManager.manifest(for: hash)
self.fileDownloader = RUFilesDownloader(projectId: projectId, laguageResolver: manifestManager, organizationName: organizationName)
manifestManager = ManifestManager.manifest(for: hash, organizationName: organizationName)
self.fileDownloader = RUFilesDownloader(projectId: projectId, manifestManager: manifestManager, organizationName: organizationName)
}

func prepare(with completion: @escaping (() -> Void)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ class RealtimeUpdateFeature: RealtimeUpdateFeatureProtocol {
private var socketManger: CrowdinSocketManagerProtocol?
private var mappingManager: CrowdinMappingManagerProtocol

required init(hash: String, sourceLanguage: String, organizationName: String? = nil) {
required init(hash: String, sourceLanguage: String, organizationName: String?) {
self.hashString = hash
self.organizationName = organizationName
self.mappingManager = CrowdinMappingManager(hash: hash, sourceLanguage: sourceLanguage)
self.mappingManager = CrowdinMappingManager(hash: hash, sourceLanguage: sourceLanguage, organizationName: organizationName)
}

func downloadDistribution(with successHandler: (() -> Void)? = nil, errorHandler: ((Error) -> Void)? = nil) {
Expand Down Expand Up @@ -163,7 +163,7 @@ class RealtimeUpdateFeature: RealtimeUpdateFeatureProtocol {

func setupSocketManager(with projectId: String, projectWsHash: String, userId: String, wsUrl: String) {
// Download manifest if it is not initialized.
let manifestManager = ManifestManager.manifest(for: hashString)
let manifestManager = ManifestManager.manifest(for: hashString, organizationName: organizationName)
guard manifestManager.downloaded else {
manifestManager.download { [weak self] in
guard let self = self else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension CrowdinSDK {
guard let config = CrowdinSDK.config else { return }
if config.screenshotsEnabled {
let crowdinProviderConfig = config.crowdinProviderConfig ?? CrowdinProviderConfig()
let screenshotUploader = CrowdinScreenshotUploader(organizationName: config.loginConfig?.organizationName, hash: crowdinProviderConfig.hashString, sourceLanguage: crowdinProviderConfig.sourceLanguage)
let screenshotUploader = CrowdinScreenshotUploader(organizationName: config.crowdinProviderConfig?.organizationName, hash: crowdinProviderConfig.hashString, sourceLanguage: crowdinProviderConfig.sourceLanguage)
ScreenshotFeature.shared = ScreenshotFeature(screenshotUploader: screenshotUploader, screenshotProcessor: CrowdinScreenshotProcessor())
swizzleControlMethods()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public protocol ScreenshotUploader {
}

class CrowdinScreenshotUploader: ScreenshotUploader {
var organizationName: String? = nil
var organizationName: String?
var hash: String
var sourceLanguage: String

Expand All @@ -29,11 +29,11 @@ class CrowdinScreenshotUploader: ScreenshotUploader {
case noLocalizedStringsDetected = "There are no localized strings detected on current screen."
}

init(organizationName: String? = nil, hash: String, sourceLanguage: String) {
init(organizationName: String?, hash: String, sourceLanguage: String) {
self.organizationName = organizationName
self.hash = hash
self.sourceLanguage = sourceLanguage
self.mappingManager = CrowdinMappingManager(hash: hash, sourceLanguage: sourceLanguage)
self.mappingManager = CrowdinMappingManager(hash: hash, sourceLanguage: sourceLanguage, organizationName: organizationName)
}

func loginAndGetProjectId(success: (() -> Void)? = nil, errorHandler: ((Error) -> Void)? = nil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import Foundation
@objcMembers public class CrowdinProviderConfig: NSObject {
var hashString: String
var sourceLanguage: String
var organizationName: String?

public init(hashString: String, sourceLanguage: String) {
public init(hashString: String, sourceLanguage: String, organizationName: String? = nil) {
self.hashString = hashString
self.sourceLanguage = sourceLanguage
self.organizationName = organizationName
}

@available(*, deprecated, renamed: "init(hashString:sourceLanguage:)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ import Foundation

class CrowdinRemoteLocalizationStorage: RemoteLocalizationStorageProtocol {
var localization: String
var organizationName: String?
var localizations: [String]
var hashString: String
var name: String = "Crowdin"
var manifestManager: ManifestManager

private var crowdinDownloader: CrowdinLocalizationDownloader
private var _localizations: [String]?
private let crowdinSupportedLanguages: CrowdinSupportedLanguages

init(localization: String, config: CrowdinProviderConfig) {
self.localization = localization
self.hashString = config.hashString
self.manifestManager = ManifestManager.manifest(for: config.hashString)
self.crowdinDownloader = CrowdinLocalizationDownloader(languageResolver: manifestManager)
self.organizationName = config.organizationName
self.manifestManager = ManifestManager.manifest(for: config.hashString, organizationName: config.organizationName)
self.crowdinDownloader = CrowdinLocalizationDownloader(manifestManager: manifestManager)
self.localizations = self.manifestManager.iOSLanguages
self.crowdinSupportedLanguages = CrowdinSupportedLanguages(organizationName: config.organizationName)
}

func prepare(with completion: @escaping () -> Void) {
Expand All @@ -33,8 +37,8 @@ class CrowdinRemoteLocalizationStorage: RemoteLocalizationStorageProtocol {
}

func downloadCrowdinSupportedLanguages(completion: @escaping () -> Void) {
if !CrowdinSupportedLanguages.shared.loaded {
CrowdinSupportedLanguages.shared.downloadSupportedLanguages(completion: {
if !crowdinSupportedLanguages.loaded {
crowdinSupportedLanguages.downloadSupportedLanguages(completion: {
completion()
}, error: {
LocalizationUpdateObserver.shared.notifyError(with: [$0])
Expand All @@ -54,15 +58,16 @@ class CrowdinRemoteLocalizationStorage: RemoteLocalizationStorageProtocol {
})
}

required init(localization: String, enterprise: Bool) {
required init(localization: String, enterprise: Bool, organizationName: String?) {
self.localization = localization
guard let hashString = Bundle.main.crowdinDistributionHash else {
fatalError("Please add CrowdinDistributionHash key to your Info.plist file")
}
self.hashString = hashString
self.manifestManager = ManifestManager.manifest(for: hashString)
self.crowdinDownloader = CrowdinLocalizationDownloader(languageResolver: self.manifestManager)
self.manifestManager = ManifestManager.manifest(for: hashString, organizationName: organizationName)
self.crowdinDownloader = CrowdinLocalizationDownloader(manifestManager: self.manifestManager)
self.localizations = []
self.crowdinSupportedLanguages = CrowdinSupportedLanguages(organizationName: organizationName)
}

func fetchData(completion: @escaping LocalizationStorageCompletion, errorHandler: LocalizationStorageError?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ extension CrowdinSDK {
/// - errorHandler: Error handler.
public class func localizationDictionary(for localization: String, hashString: String, completion: @escaping ([AnyHashable: Any]) -> Void, errorHandler: @escaping (Error) -> Void) {
let localLocalizationStorage = LocalLocalizationStorage(localization: localization)
let remoteLocalizationStorage = CrowdinRemoteLocalizationStorage(localization: localization, config: CrowdinProviderConfig(hashString: hashString, sourceLanguage: .empty))
let remoteLocalizationStorage = CrowdinRemoteLocalizationStorage(localization: localization, config: CrowdinProviderConfig(hashString: hashString, sourceLanguage: .empty, organizationName: nil))
remoteLocalizationStorage.prepare {
localizationProvider = LocalizationProvider(localization: localization, localStorage: localLocalizationStorage, remoteStorage: remoteLocalizationStorage)
localizationProvider?.refreshLocalization(completion: { error in
Expand Down
Loading