2

I am working a project where I am modularising with Swift Packages. I have a package I called Theme where my Color and Image assets resides, under the Sources directory.

In another package called Home, I would like to have access to the Assets in Theme but I get an error message unable to find bundle named Theme_Theme.

Meanwhile this works well from the main Target.

Theme.swift

public struct PrimaryTheme {
    public init() {}
    public static var smallTextSize: CGFloat = 14.0
    public static var mediumTextSize: CGFloat = 16.0
    public static var largeTextSize: CGFloat = 24.0
    public static var navigationTextSize: CGFloat = 18.0
    public static var smallPadding: CGFloat = 14.0
    public static var mediumPadding: CGFloat = 18.0
    public static var largePadding: CGFloat = 25.0
    public enum Images: String {
        case bill, tinggSplashScreenIcon, tinggAssistImage, tinggIcon,
        addBillImage, moneyImage, home, explore, group
        public var image: Image {
            return Images.getImage(self.rawValue)
        }
        public static func getImage(_ name: String) -> Image {
            return Image(name, bundle: Bundle.module)
        }
    }
    public enum AppColors: String {
        case secondaryColor, primaryColor, cellulantPurple,
             cellulantRed, skyBlue, cellulantLightGray, textColor
        public static func getColor(_ name: String) -> Color {
            return  Color(name, bundle: Bundle.module)
        }
    }
    public static func getColor(_ name: AppColors) -> Color {
        return  AppColors.getColor(name.rawValue)
    }
    public static func getImage(image: Images) -> Image {
        return Images.getImage(image.rawValue)
    }
}

Theme Pacakage

import PackageDescription

let package = Package(
    name: "Theme",
    platforms: [.iOS(.v13)],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "Theme",
            targets: ["Theme"])
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "Theme",
            dependencies: [],
            resources: [.copy("Color.xcassets")]
        ),
        .testTarget(
            name: "ThemeTests",
            dependencies: ["Theme"])
    ]
)

Home.swift

import SwiftUI
import Theme
struct HomeUIView: View {
    var body: some View {
        VStack {
            Text("Hello")
                .foregroundColor(PrimaryTheme.getColor(.primaryColor))
        }
    }
}

struct HomeUIView_Previews: PreviewProvider {
    static var previews: some View {
        HomeUIView()
    }
}

Home Package

import PackageDescription

let package = Package(
    name: "Home",
    platforms: [.iOS(.v13)],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "Home",
            targets: ["Home"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(path: "../Theme")
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "Home",
            dependencies: ["Theme"]),
        .testTarget(
            name: "HomeTests",
            dependencies: ["Home", "Theme"])
    ]
)

What am I doing wrong?

Darotudeen
  • 1,914
  • 4
  • 21
  • 36

0 Answers0