Skip to content

Commit 8e8fc22

Browse files
committed
Add ResourceID
1 parent fd29ab7 commit 8e8fc22

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// ResourceID.swift
3+
// SwiftAndroid
4+
//
5+
// Created by Alsey Coleman Miller on 6/9/25.
6+
//
7+
8+
import JavaKit
9+
10+
/// Android Resource ID
11+
public struct ResourceID: RawRepresentable, Equatable, Hashable, Codable, Sendable {
12+
13+
public let rawValue: Int32
14+
15+
public init(rawValue: Int32) {
16+
assert(rawValue != 0, "Invalid Resource ID: \(rawValue)")
17+
self.rawValue = rawValue
18+
}
19+
}
20+
21+
// MARK: - Methods
22+
23+
public extension ResourceID {
24+
25+
/// Return a resource identifier for the given resource name.
26+
/// A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
27+
init?(name: String, type: String, package: String, in resources: Resources) {
28+
let rawValue = resources.getIdentifier(name, type, package)
29+
guard rawValue != 0 else {
30+
return nil
31+
}
32+
self.init(rawValue: rawValue)
33+
}
34+
35+
/// Return the full name for a given resource identifier. This name is a single string of the form "package:type/entry".
36+
func name(in resources: Resources) throws -> String {
37+
try resources.getResourceName(rawValue)
38+
}
39+
}
40+
41+
// MARK: - CustomStringConvertible
42+
43+
extension ResourceID: CustomStringConvertible {
44+
45+
public var description: String {
46+
rawValue.description
47+
}
48+
}
49+
50+
// MARK: - ExpressibleByIntegerLiteral
51+
52+
extension ResourceID: ExpressibleByIntegerLiteral {
53+
54+
public init(integerLiteral value: Int32) {
55+
self.init(rawValue: value)
56+
}
57+
}

0 commit comments

Comments
 (0)