Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Refactor Directory Reading Logic for Depth Control #98

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 28 additions & 35 deletions EmpowerPlant/EmpowerPlantViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,55 +91,48 @@ class EmpowerPlantViewController: UIViewController {
}



func readCurrentDirectory() {
let path = FileManager.default.currentDirectoryPath
let maxDepth = 5 // Prevent too deep recursion

do {
let items = try FileManager.default.contentsOfDirectory(atPath: path)
let loop = fibonacciSeries(num: items.count)
for i in 1...loop {
readDirectory(path: path)
}
// Single pass through directory structure
try readDirectory(path: path, currentDepth: 0, maxDepth: maxDepth)
} catch {
// TODO: error
SentrySDK.capture(error: error)
}
}

func readDirectory(path: String) {
func readDirectory(path: String, currentDepth: Int, maxDepth: Int) throws {
// Guard against excessive recursion
guard currentDepth < maxDepth else {
return
}

let fm = FileManager.default
let span = SentrySDK.span?.startChild(operation: "file.read_directory")
span?.setData(value: path, key: "path")
defer { span?.finish() }

do {
let items = try fm.contentsOfDirectory(atPath: path)
let items = try fm.contentsOfDirectory(atPath: path)

for item in items {
let fullPath = (path as NSString).appendingPathComponent(item)
var isDirectory: ObjCBool = false

for item in items {
var isDirectory: ObjCBool = false
if fm.fileExists(atPath: item, isDirectory: &isDirectory) {
readDirectory(path: item)
} else {
return
if fm.fileExists(atPath: fullPath, isDirectory: &isDirectory) {
if isDirectory.boolValue {
// Only recurse into directories
try readDirectory(
path: fullPath,
currentDepth: currentDepth + 1,
maxDepth: maxDepth
)
}
}
} catch {
// TODO: error
}

}

func fibonacciSeries(num: Int) -> Int{
// The value of 0th and 1st number of the fibonacci series are 0 and 1
var n1 = 0
var n2 = 1

// To store the result
var nR = 0
// Adding two previous numbers to find ith number of the series
for _ in 0..<num{
nR = n1
n1 = n2
n2 = nR + n2
}

if (n1 < 500) {
return fibonacciSeries(num: n1)
}
return n1
}
Expand Down
Loading