Skip to content

Commit

Permalink
[Android] Support RTL (#182)
Browse files Browse the repository at this point in the history
* Update FileContents

* Update FileDownloader

* Update FileWriter

* Update ExportIcons

* Update README

* Fix name

* Update FileWriter

* Update ExportIcons
  • Loading branch information
alexey1312 authored Jul 20, 2022
1 parent 116629e commit 7184f92
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Table of Contents:
* Supports High contrast colors for Xcode
* Supports SwiftUI and UIKit
* Supports Objective-C
* Supports RTL (only iOS)
* Supports RTL

> Exporting icons and images works only for Professional/Organisation Figma plan because FigmaExport use *Shareable team libraries*.
Expand Down Expand Up @@ -452,7 +452,7 @@ Custom Stencil templates must have the following names:

If a color, icon or image is unique for iOS or Android platform, it should contains "ios" or "android" word in the description field in the properties. If a color, icon or image is used only by the designer, and it should not be exported, the word "none" should be specified in the description field.

If an icon supports RTL (iOS only), it should contains "rtl" word in the description field in the properties.
If an icon supports RTL, it should contains "rtl" word in the description field in the properties.

**Styles and Components must be published to a Team Library.**

Expand Down
3 changes: 2 additions & 1 deletion Sources/FigmaExport/Output/FileDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ final class FileDownloader {
destination: file.destination,
dataFile: fileURL,
scale: file.scale,
dark: file.dark
dark: file.dark,
isRTL: file.isRTL
)
newFiles.append(newFile)
downloaded += 1
Expand Down
6 changes: 6 additions & 0 deletions Sources/FigmaExport/Output/FileWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ final class FileWriter {
}
}
}

func write(xmlFile: XMLDocument, directory: URL) throws {
let fileURL = URL(fileURLWithPath: directory.path)
let options: XMLNode.Options = [.nodePrettyPrint, .nodeCompactEmptyElement]
try xmlFile.xmlData(options: options).write(to: fileURL, options: .atomic)
}
}
31 changes: 27 additions & 4 deletions Sources/FigmaExport/Subcommands/ExportIcons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ extension FigmaExportCommand {
let lightFiles = asset.light.images.map { image -> FileContents in
let fileURL = URL(string: "\(image.name).svg")!
let dest = Destination(directory: tempDirectoryLightURL, file: fileURL)
return FileContents(destination: dest, sourceURL: image.url)
return FileContents(destination: dest, sourceURL: image.url, isRTL: image.isRTL)
}
let darkFiles = asset.dark?.images.map { image -> FileContents in
let fileURL = URL(string: "\(image.name).svg")!
let dest = Destination(directory: tempDirectoryDarkURL, file: fileURL)
return FileContents(destination: dest, sourceURL: image.url, dark: true)
return FileContents(destination: dest, sourceURL: image.url, dark: true, isRTL: image.isRTL)
} ?? []
return lightFiles + darkFiles
}
Expand Down Expand Up @@ -182,6 +182,7 @@ extension FigmaExportCommand {

// 6. Move XML files to main/res/drawable/
localFiles = localFiles.map { fileContents -> FileContents in
let directory = fileContents.dark ? darkDirectory : lightDirectory

let source = fileContents.destination.url
.deletingPathExtension()
Expand All @@ -190,8 +191,9 @@ extension FigmaExportCommand {
let fileURL = fileContents.destination.file
.deletingPathExtension()
.appendingPathExtension("xml")

let directory = fileContents.dark ? darkDirectory : lightDirectory

rewriteXMLFile(from: source, fileContents: fileContents)

return FileContents(
destination: Destination(directory: directory, file: fileURL),
dataFile: source
Expand Down Expand Up @@ -227,3 +229,24 @@ extension FigmaExportCommand {
}
}
}

private extension FigmaExportCommand.ExportIcons {
private func rewriteXMLFile(from source: URL, fileContents: FileContents) {
guard fileContents.isRTL,
let attribute = XMLNode.attribute(withName: "android:autoMirrored", stringValue: "\(fileContents.isRTL)") as? XMLNode
else { return }
if let sourceXML = try? XMLDocument(contentsOf: source, options: .documentTidyXML) {
try? sourceXML.nodes(forXPath: "//vector").forEach { node in
guard let element = node as? XMLElement else { return }
element.addAttribute(attribute)
}

do {
FigmaExportCommand.logger.info("Add autoMirrored property for xml file...")
try FigmaExportCommand.fileWriter.write(xmlFile: sourceXML, directory: source)
} catch {
FigmaExportCommand.logger.info("Rewrite XMLFile - \(sourceXML) is faild")
}
}
}
}
10 changes: 7 additions & 3 deletions Sources/FigmaExportCore/FileContents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,39 @@ public struct FileContents: Equatable {

public var dark: Bool = false
public var scale: Double = 1.0
public var isRTL: Bool = false

/// In-memory file
public init(destination: Destination, data: Data, scale: Double = 1.0, dark: Bool = false) {
public init(destination: Destination, data: Data, scale: Double = 1.0, dark: Bool = false, isRTL: Bool = false) {
self.destination = destination
self.data = data
self.dataFile = nil
self.sourceURL = nil
self.scale = scale
self.dark = dark
self.isRTL = isRTL
}

/// Remote file
public init(destination: Destination, sourceURL: URL, scale: Double = 1.0, dark: Bool = false) {
public init(destination: Destination, sourceURL: URL, scale: Double = 1.0, dark: Bool = false, isRTL: Bool = false) {
self.destination = destination
self.data = nil
self.dataFile = nil
self.sourceURL = sourceURL
self.scale = scale
self.dark = dark
self.isRTL = isRTL
}

/// On-disk file
public init(destination: Destination, dataFile: URL, scale: Double = 1.0, dark: Bool = false) {
public init(destination: Destination, dataFile: URL, scale: Double = 1.0, dark: Bool = false, isRTL: Bool = false) {
self.destination = destination
self.data = nil
self.dataFile = dataFile
self.sourceURL = nil
self.scale = scale
self.dark = dark
self.isRTL = isRTL
}

/// Make a copy of the FileContents with different file extension
Expand Down

0 comments on commit 7184f92

Please sign in to comment.