Skip to content

Commit

Permalink
Merge pull request #38 from Infomaniak/split-coreui
Browse files Browse the repository at this point in the history
refactor: Split CoreUI into CommonUI | SwiftUI | UIKit
  • Loading branch information
PhilippeWeidmann authored Sep 19, 2024
2 parents f882565 + 92dacc5 commit e37e2b5
Show file tree
Hide file tree
Showing 63 changed files with 200 additions and 112 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:

steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1
uses: styfle/cancel-workflow-action@0.12.1
with:
access_token: ${{ github.token }}
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Build
run: xcodebuild -scheme InfomaniakCoreUI build -sdk iphonesimulator -destination "platform=iOS,name=iPhone 15 Pro,OS=latest"
run: xcodebuild -scheme InfomaniakCoreUI-Package build -sdk iphonesimulator -destination "generic/platform=iOS"
- name: Test
run: xcodebuild -scheme InfomaniakCoreUI test -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 15 Pro,OS=latest"
run: xcodebuild -scheme InfomaniakCoreUI-Package test -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 16 Pro,OS=latest"
22 changes: 18 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ let package = Package(
],
products: [
.library(
name: "InfomaniakCoreUI",
targets: ["InfomaniakCoreUI"]),
name: "InfomaniakCoreCommonUI",
targets: ["InfomaniakCoreCommonUI"]),
.library(
name: "InfomaniakCoreUIKit",
targets: ["InfomaniakCoreUIKit"]),
.library(
name: "InfomaniakCoreSwiftUI",
targets: ["InfomaniakCoreSwiftUI"]),
],
dependencies: [
.package(url: "https://github.com/Infomaniak/ios-core", .upToNextMajor(from: "12.0.0")),
Expand All @@ -21,15 +27,23 @@ let package = Package(
],
targets: [
.target(
name: "InfomaniakCoreUI",
name: "InfomaniakCoreCommonUI",
dependencies: [
"Kingfisher",
.product(name: "MatomoTracker", package: "matomo-sdk-ios"),
.product(name: "InfomaniakCore", package: "ios-core"),
.product(name: "SnackBar", package: "SnackBar.swift")
]),
.target(
name: "InfomaniakCoreUIKit",
dependencies: [
"InfomaniakCoreCommonUI",
]),
.target(
name: "InfomaniakCoreSwiftUI",
dependencies: []),
.testTarget(
name: "InfomaniakCoreUITests",
dependencies: ["InfomaniakCoreUI"])
dependencies: ["InfomaniakCoreCommonUI", "InfomaniakCoreUIKit", "InfomaniakCoreSwiftUI"])
]
)
File renamed without changes.
30 changes: 30 additions & 0 deletions Sources/InfomaniakCoreCommonUI/OrganisationColors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Infomaniak Core UI - iOS
Copyright (C) 2023 Infomaniak Network SA

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import UIKit

public extension UIColor {
static let organisationColors: [UIColor] = Array(0 ... 9)
.map { UIColor(named: "organisationColor\($0)", in: Bundle.module, compatibleWith: nil) }
.compactMap { $0 }

class func backgroundColor(from userId: Int, with colors: [UIColor] = UIColor.organisationColors) -> UIColor {
let colorIndex = abs(userId % colors.count)
return colors[colorIndex]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Infomaniak Core UI - iOS
Copyright (C) 2023 Infomaniak Network SA
Copyright (C) 2024 Infomaniak Network SA

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -24,8 +24,6 @@
import UIKit
#endif

import SwiftUI

public struct TextStyle: RawRepresentable {
public var font: UIFont
public var color: UIColor
Expand Down Expand Up @@ -123,40 +121,3 @@ public struct TextStyle: RawRepresentable {
}
}
}

@IBDesignable public class IKLabel: UILabel {
/// Set label style.
@IBInspectable public var styleName: String = TextStyle.body1.rawValue {
didSet { setUpLabel() }
}

/// Set label style.
public var style: TextStyle {
get {
return TextStyle(rawValue: styleName) ?? .body1
}
set {
styleName = newValue.rawValue
}
}

override public init(frame: CGRect) {
super.init(frame: frame)
setUpLabel()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
setUpLabel()
}

override public func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setUpLabel()
}

func setUpLabel() {
font = style.font
textColor = style.color
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import SwiftUI
import UIKit

public enum Constants {
public static let organisationColors: [UIColor] = {
return Array(0...9)
.map { UIColor(named: "organisationColor\($0)", in: Bundle.module, compatibleWith: nil) }
.compactMap { $0 }
}()
public extension UIView {
func addShadow(elevation: Double = 1) {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0.0, height: 1.23118 * elevation - 0.03933)
layer.shadowOpacity = 0.17
layer.shadowRadius = CGFloat(0.666920 * elevation - 0.001648)
layer.shouldRasterize = true
layer.rasterizationScale = UIScreen.main.scale
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public extension Image {
/// - Parameter size: The specified size.
/// - Returns: A square view containing the image scaled to fit in a frame of the specified size.
func iconSize(_ size: IKIconSize) -> some View {
self
.resizable()
resizable()
.scaledToFit()
.frame(width: size.rawValue, height: size.rawValue)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,4 @@ public extension UIColor {
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: 1)
}

class func backgroundColor(from userId: Int, with colors: [UIColor] = Constants.organisationColors) -> UIColor {
let colorIndex = abs(userId % colors.count)
return colors[colorIndex]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import UIKit

public enum ImageUtil {
internal typealias ImageOrientation = UIImage.Orientation
typealias ImageOrientation = UIImage.Orientation

public static func cgImageWithCorrectOrientation(_ image: UIImage) -> CGImage {
if image.imageOrientation == ImageOrientation.up {
Expand Down Expand Up @@ -80,7 +80,7 @@ public enum ImageUtil {
}

public static func drawImage(_ image: UIImage, in bounds: CGRect) -> UIImage? {
return self.drawImage(size: bounds.size, scale: UIScreen.main.scale) { (_: CGSize, _: CGContext) -> UIImage? in
return drawImage(size: bounds.size, scale: UIScreen.main.scale) { (_: CGSize, _: CGContext) -> UIImage? in
image.draw(in: bounds)

let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
Expand All @@ -91,7 +91,7 @@ public enum ImageUtil {
}

public static func croppedImage(_ image: UIImage, with rect: CGRect) -> UIImage? {
return self.drawImage(size: rect.size, scale: image.scale) { (_: CGSize, context: CGContext) -> UIImage? in
return drawImage(size: rect.size, scale: image.scale) { (_: CGSize, context: CGContext) -> UIImage? in
let drawRect = CGRect(x: -rect.origin.x, y: -rect.origin.y, width: image.size.width, height: image.size.height)
context.clip(to: CGRect(x: 0, y: 0, width: rect.size.width, height: rect.size.height))
image.draw(in: drawRect)
Expand All @@ -103,7 +103,11 @@ public enum ImageUtil {
}
}

public static func drawImage(size: CGSize!, scale: CGFloat, closure: @escaping (_ size: CGSize, _ context: CGContext) -> UIImage?) -> UIImage? {
public static func drawImage(
size: CGSize!,
scale: CGFloat,
closure: @escaping (_ size: CGSize, _ context: CGContext) -> UIImage?
) -> UIImage? {
guard size.width > 0.0 && size.height > 0.0 else {
print("WARNING: Invalid size requested: \(size.width) x \(size.height) - must not be 0.0 in any dimension")
return nil
Expand All @@ -129,7 +133,12 @@ public extension UIImage {

let scaleRatio = max(heightRatio, widthRatio)

let resizedImageBounds = CGRect(x: 0, y: 0, width: round(originalWidth * scaleRatio), height: round(originalHeight * scaleRatio))
let resizedImageBounds = CGRect(
x: 0,
y: 0,
width: round(originalWidth * scaleRatio),
height: round(originalHeight * scaleRatio)
)
let resizedImage = ImageUtil.drawImage(self, in: resizedImageBounds)
guard resizedImage != nil else {
return UIImage()
Expand Down Expand Up @@ -185,17 +194,29 @@ public extension UIImage {
return image!
}

class func getInitialsPlaceholder(with name: String, size: CGSize = CGSize(width: 40, height: 40), foregroundColor: UIColor = .white, backgroundColor: UIColor) -> UIImage {
class func getInitialsPlaceholder(
with name: String,
size: CGSize = CGSize(width: 40, height: 40),
foregroundColor: UIColor = .white,
backgroundColor: UIColor
) -> UIImage {
let initials = name.initials
let defaultFontRatio: CGFloat = 14.0 / 40.0
let frame = CGRect(origin: .zero, size: size)
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: frame.size.width * defaultFontRatio), .foregroundColor: foregroundColor]
let attributes: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: frame.size.width * defaultFontRatio),
.foregroundColor: foregroundColor
]
let initialsSize = initials.size(withAttributes: attributes)
let renderer = UIGraphicsImageRenderer(size: CGSize(width: frame.size.width, height: frame.size.height))
return renderer.image { context in
backgroundColor.setFill()
context.fill(renderer.format.bounds)
(initials as NSString).draw(in: CGRect(x: frame.midX - initialsSize.width / 2, y: frame.midY - initialsSize.height / 2, width: initialsSize.width, height: initialsSize.height), withAttributes: attributes)
(initials as NSString).draw(
in: CGRect(x: frame.midX - initialsSize.width / 2, y: frame.midY - initialsSize.height / 2,
width: initialsSize.width, height: initialsSize.height),
withAttributes: attributes
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,10 @@ public extension UIView {
}

func roundCorners(corners: CACornerMask, radius: CGFloat) {
self.clipsToBounds = false
self.layer.cornerRadius = radius
clipsToBounds = false
layer.cornerRadius = radius
#if !os(tvOS)
self.layer.maskedCorners = corners
layer.maskedCorners = corners
#endif
}

func addShadow(elevation: Double = 1) {
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 0.0, height: 1.23118 * elevation - 0.03933)
self.layer.shadowOpacity = 0.17
self.layer.shadowRadius = CGFloat(0.666920 * elevation - 0.001648)
self.layer.shouldRasterize = true
self.layer.rasterizationScale = UIScreen.main.scale
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public extension UIViewController {
view.endEditing(true)
}

func okAlert(title: String, message: String?, completion: (() -> ())? = nil) {
func okAlert(title: String, message: String?, completion: (() -> Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in completion?() }))
self.present(alert, animated: true)
present(alert, animated: true)
}
}
Loading

0 comments on commit e37e2b5

Please sign in to comment.