feat: cleanup appstate, move sort to tab
This commit is contained in:
@@ -3,7 +3,7 @@ pub const Tab = struct {
|
||||
currentLocation: ?*Container,
|
||||
currentItems: locked(?std.ArrayList(Arc(*Item))),
|
||||
currentLocationChanged: Observable(?*Container),
|
||||
currentItemsChanged: bool = false,
|
||||
currentItem: ?models.FullName,
|
||||
threadPool: *std.Thread.Pool,
|
||||
rootProvider: *RootProvider,
|
||||
|
||||
@@ -18,6 +18,7 @@ pub const Tab = struct {
|
||||
.currentItems = .{ .data = null },
|
||||
.currentLocationChanged = Observable(?*Container).init(allocator),
|
||||
.currentLocation = null,
|
||||
.currentItem = null,
|
||||
.threadPool = threadPool,
|
||||
.rootProvider = rootProvider,
|
||||
};
|
||||
@@ -28,26 +29,54 @@ pub const Tab = struct {
|
||||
c.item.deinit();
|
||||
}
|
||||
|
||||
errdefer {
|
||||
self.currentLocation = null;
|
||||
self.currentLocationChanged.notify(null);
|
||||
if (self.currentItem) |i| {
|
||||
self.allocator.free(i.path);
|
||||
self.currentItem = null;
|
||||
}
|
||||
|
||||
const newLocation = try self.rootProvider.getItemByFullName(newLocationFullName, &.{}, self.allocator);
|
||||
errdefer {
|
||||
newLocation.deinit();
|
||||
}
|
||||
const newLocationContainer = blk: {
|
||||
const newLocation = try self.rootProvider.getItemByFullName(newLocationFullName, &.{}, self.allocator);
|
||||
errdefer {
|
||||
newLocation.deinit();
|
||||
self.currentLocation = null;
|
||||
self.currentLocationChanged.notify(null);
|
||||
}
|
||||
|
||||
const newLocationContainer = switch (newLocation.item) {
|
||||
.container => |c| c,
|
||||
.element => return error.NotContainer,
|
||||
const newLocationContainer = switch (newLocation.item) {
|
||||
.container => |c| c,
|
||||
.element => return error.NotContainer,
|
||||
};
|
||||
|
||||
self.currentLocation = newLocationContainer;
|
||||
break :blk newLocationContainer;
|
||||
};
|
||||
|
||||
self.currentLocation = newLocationContainer;
|
||||
self.currentLocationChanged.notify(newLocationContainer);
|
||||
|
||||
//TODO: Proper error handling
|
||||
std.Thread.Pool.spawn(self.threadPool, loadItemsWrapper, .{ self, newLocationContainer }) catch unreachable;
|
||||
|
||||
self.selectItem(0) catch {
|
||||
self.currentItem = null;
|
||||
};
|
||||
}
|
||||
|
||||
pub fn selectItem(self: *Tab, index: usize) !void {
|
||||
self.currentItems.mutex.lock();
|
||||
defer self.currentItems.mutex.unlock();
|
||||
|
||||
const currentItems = self.currentItems.data orelse return error.Error;
|
||||
|
||||
if (index >= currentItems.items.len) {
|
||||
return error.OutOfRange;
|
||||
}
|
||||
|
||||
if (self.currentItem) |currentItem| {
|
||||
self.allocator.free(currentItem.path);
|
||||
}
|
||||
|
||||
self.currentItem = models.FullName{
|
||||
.path = try self.allocator.dupe(u8, currentItems.items[index].value.*.fullName.path),
|
||||
};
|
||||
}
|
||||
|
||||
fn loadItemsWrapper(self: *Tab, location: *Container) void {
|
||||
@@ -84,25 +113,25 @@ pub const Tab = struct {
|
||||
std.Thread.sleep(1 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
for (location.children.items) |item| {
|
||||
const resolvedItem = location.item.provider.getItemByFullName(item, &.{ .skipChildInit = true }, self.allocator) catch |e| {
|
||||
for (location.children.items) |item_fullname| {
|
||||
const resolvedItem = location.item.provider.getItemByFullName(item_fullname, &.{ .skipChildInit = true }, self.allocator) catch |e| {
|
||||
//TODO: save error to container errors
|
||||
std.debug.print("error {} {s}\r\n", .{ e, item.path });
|
||||
std.debug.print("error {} {s}\r\n", .{ e, item_fullname.path });
|
||||
continue;
|
||||
};
|
||||
|
||||
self.currentItems.mutex.lock();
|
||||
defer self.currentItems.mutex.unlock();
|
||||
|
||||
// const arc = try Arc(*Item).init(self.allocator, resolvedItem);
|
||||
// try self.currentItems.data.?.append(arc);
|
||||
try self.currentItems.data.?.append(try Arc(*Item).init(self.allocator, resolvedItem));
|
||||
}
|
||||
|
||||
{
|
||||
self.currentItems.mutex.lock();
|
||||
defer self.currentItems.mutex.unlock();
|
||||
self.currentItemsChanged = true;
|
||||
const index = for (self.currentItems.data.?.items, 0..) |item, i| {
|
||||
if (compareByDisplayName(resolvedItem, item.value.*)) break i;
|
||||
} else null;
|
||||
const arc = try Arc(*Item).init(self.allocator, resolvedItem);
|
||||
if (index) |i| {
|
||||
try self.currentItems.data.?.insert(i, arc);
|
||||
} else {
|
||||
try self.currentItems.data.?.append(arc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,8 +151,18 @@ pub const Tab = struct {
|
||||
currentItems.deinit();
|
||||
}
|
||||
|
||||
if (self.currentItem) |currentItem| {
|
||||
self.allocator.free(currentItem.path);
|
||||
}
|
||||
|
||||
self.allocator.destroy(self);
|
||||
}
|
||||
|
||||
fn compareByDisplayName(lhs: *models.Item, rhs: *models.Item) bool {
|
||||
if (lhs.item == .container and rhs.item == .element) return true;
|
||||
if (lhs.item == .element and rhs.item == .container) return false;
|
||||
return std.mem.order(u8, lhs.displayName, rhs.displayName) == .lt;
|
||||
}
|
||||
};
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
Reference in New Issue
Block a user