Skip to content

Commit

Permalink
2023-09-25 - Updated package (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
markbattistella authored Sep 25, 2023
1 parent 93cb80c commit 23f239c
Show file tree
Hide file tree
Showing 34 changed files with 1,960 additions and 2,181 deletions.
39 changes: 20 additions & 19 deletions .github/supported-devices.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* Generate Supported Devices markdown file
* -----------------------
* This script reads the bezel-data-min.json file and generates a markdown file
* This script reads the bezel.min.json file and generates a markdown file
*
* Author: Mark Battistella
* Version: 1.0.0
* Version: 2.0.0
* Licence: MIT
* Contact: @markbattistella
* Website: https://markbattistella.com
Expand All @@ -20,31 +20,32 @@ const fs = require('fs');
* @return {string} The Markdown-formatted string.
*/
const jsonToMarkdown = (data) => {
// Initialize Markdown header
let markdown = '# Supported Device List\n\n';
markdown += 'Below is the current supported list of devices `BezelKit` can return data for.\n\n';

// Loop through each category (like 'iPod Touch', 'iPhone', etc.)
for (const category in data) {
// Add Markdown sub-header for each category
const devices = data.devices;

for (const category in devices) {
markdown += `## ${category}\n\n`;

// Define the Markdown table header
markdown += '| Device | Model Identifier | Bezel Size |\n';
markdown += '|-----------------------------|------------------|------------|\n';

// Populate the table with device data
for (const item of data[category]) {
// Map each identifier with backticks and join them with a comma and space
const identifiers = item.identifiers.map(id => `\`${id}\``).join(', ');
markdown += `| ${item.device} | ${identifiers} | \`${item.bezel}\` |\n`;
markdown += '| Device | Model Identifier | Bezel Size |\n';
markdown += '|-----------------------------|------------------------|------------|\n';

const categoryDevices = devices[category];
for (const modelId in categoryDevices) {
const deviceDetails = categoryDevices[modelId];
markdown += `| ${deviceDetails.name} | \`${modelId}\` | \`${deviceDetails.bezel}\` |\n`;
}

// Add a newline to separate categories
markdown += '\n';
}

// Remove trailing whitespace and add a single newline at the end
// Add _metadata information at the bottom
const meta = data._metadata;
markdown += '---\n'; // Horizontal line for separation
markdown += `**Author**: [${meta.Author}](${meta.Website})\n`;
markdown += `**Project**: ${meta.Project}\n`;

return markdown.trim() + '\n';
};

Expand All @@ -53,8 +54,8 @@ const jsonToMarkdown = (data) => {
*/
const main = () => {
// Read JSON data from file
const jsonStr = fs.readFileSync('./Sources/BezelKit/Resources/bezel-data-min.json', 'utf-8');
const jsonStr = fs.readFileSync('./Sources/BezelKit/Resources/bezel.min.json', 'utf-8');

// Parse the JSON string to an object
const jsonData = JSON.parse(jsonStr);

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*.xcodeproj
xcuserdata/
DerivedData/
/.swiftpm
.swiftpm
.netrc
logs
.markdownlint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import SwiftUI
struct MainApp: App {
var body: some Scene {
WindowGroup {
Text("Running app")
.onAppear { logMetrics() }
MainView().onAppear { logMetrics() }
}
}

Expand All @@ -19,7 +18,8 @@ struct MainApp: App {
/// This function gathers device metrics including the device model name and the screen's corner radius,
/// and saves this information in a JSON-formatted string to a file named `output.txt` in the app's document directory.
///
/// If the function encounters any errors during the JSON serialization or file writing operations, it logs those errors to the console and terminates the operation.
/// If the function encounters any errors during the JSON serialization or file writing operations, it logs those errors
/// to the console and terminates the operation.
private func logMetrics() {

// Retrieve the device model name and screen corner radius.
Expand Down Expand Up @@ -48,7 +48,7 @@ struct MainApp: App {
let path = FileManager
.default
.urls(for: .documentDirectory, in: .userDomainMask)[0]
.appendingPathComponent("output.txt")
.appendingPathComponent("output.json")

// Attempt to write the JSON string to the file at the specified path.
do {
Expand All @@ -59,31 +59,67 @@ struct MainApp: App {
}
}

/// An extension to `UIScreen` for retrieving the corner radius of the device's display.
extension UIScreen {
/// `MainView` provides a detailed view of the device's properties.
///
/// This view presents a list of device-specific details such as the device's name,
/// model name, system version, and more. It utilizes the `TableRowView` to structure
/// and present each pair of title and value.
fileprivate struct MainView: View {
var body: some View {
VStack(alignment: .leading) {
Text("FetchBezel")
.font(.largeTitle)
.fontWeight(.heavy)
.padding(.bottom)
TableRowView("Name", value: UIDevice.current.name)
TableRowView("Model Name", value: UIDevice.current.modelName)
TableRowView("Model", value: UIDevice.current.model)
TableRowView("System Version", value: UIDevice.current.systemVersion)
TableRowView("System name", value: UIDevice.current.systemName)
TableRowView("Localized Model", value: UIDevice.current.localizedModel)
TableRowView("Debug Description", value: UIDevice.current.debugDescription)
TableRowView("Description", value: UIDevice.current.description)
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.padding()
}

/// A helper view function that creates a table row with a title and its associated value.
///
/// - Parameters:
/// - title: The title or label for the row.
/// - value: The value associated with the title.
/// - Returns: A `View` representing the table row.
private func TableRowView(_ title: String, value: String) -> some View {
VStack(alignment: .leading) {
Text(title)
.fontWeight(.bold)
Text(value)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.bottom)
}
}

/// A private key used to retrieve the display corner radius value via KVC (Key-Value Coding).
/// This key is generated by reversing and joining the components ["Radius", "Corner", "display", "_"].
private static let radiusKey: String = {
let components = ["Radius", "Corner", "display", "_"]
return components.reversed().joined()
}()
/// An extension to `UIScreen` for retrieving the corner radius of the device's display.
fileprivate extension UIScreen {

/// The radius of the corners of the device's display.
/// This value is accessed through Key-Value Coding, using a private key.
///
/// - Returns: A `CGFloat` value representing the corner radius.
/// If the corner radius cannot be accessed, this property returns `0`.
public var radius: CGFloat {
guard let corner = self.value(forKey: Self.radiusKey) as? CGFloat else {
var radius: CGFloat {
let radiusKey = "_displayCornerRadius"
guard let corner = self.value(forKey: radiusKey) as? CGFloat else {
return 0
}
return corner
}
}

/// An extension to `UIDevice` for retrieving the model name identifier of the device.
extension UIDevice {
fileprivate extension UIDevice {

/// The model name identifier of the device.
///
Expand All @@ -108,7 +144,6 @@ extension UIDevice {
guard let value = element.value as? Int8, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}

#endif

return identifier
Expand Down
Loading

0 comments on commit 23f239c

Please sign in to comment.