Browse Source

Optimise imports

master
Kenneth Bruen 9 months ago
parent
commit
226121ac16
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 4
      src/departure.zig
  2. 26
      src/home.zig
  3. 4
      src/main.zig
  4. 2
      src/state.zig

4
src/departure.zig

@ -1,8 +1,8 @@
const raylib = @import("raylib.zig"); const raylib = @import("raylib.zig");
const rl = raylib.rl; const rl = raylib.rl;
const stateMod = @import("state.zig"); const AppState = @import("state.zig");
pub fn render(state: *stateMod.AppState) !void { pub fn render(state: *AppState) !void {
while (raylib.GetKeyPressed()) |key| { while (raylib.GetKeyPressed()) |key| {
switch (key) { switch (key) {
rl.KEY_LEFT => { rl.KEY_LEFT => {

26
src/home.zig

@ -1,8 +1,8 @@
const std = @import("std"); const std = @import("std");
const raylib = @import("raylib.zig"); const raylib = @import("raylib.zig");
const rl = raylib.rl; const rl = raylib.rl;
const state_mod = @import("state.zig"); const AppState = @import("state.zig");
const curl_mod = @import("curl.zig"); const Curl = @import("curl.zig");
fn curlWriteHandler(ptr: [*]u8, size: usize, nmemb: usize, userdata: *std.ArrayList(u8)) callconv(.C) usize { fn curlWriteHandler(ptr: [*]u8, size: usize, nmemb: usize, userdata: *std.ArrayList(u8)) callconv(.C) usize {
_ = size; _ = size;
@ -10,13 +10,13 @@ fn curlWriteHandler(ptr: [*]u8, size: usize, nmemb: usize, userdata: *std.ArrayL
return nmemb; return nmemb;
} }
fn fetchThread(state: *state_mod.AppState) !void { fn fetchThread(state: *AppState) !void {
std.debug.print("Started fetchThread\n", .{}); std.debug.print("[home/fetchThread] Started\n", .{});
defer std.debug.print("Ended fetchThread\n", .{}); defer std.debug.print("[home/fetchThread] Ended\n", .{});
defer state.home_screen_state.fetch_thread = null; defer state.home_screen_state.fetch_thread = null;
const allocator = state.allocator; const allocator = state.allocator;
var station_name_buf = std.BoundedArray(u8, 200){}; var station_name_buf = std.BoundedArray(u8, 200){};
var curl = curl_mod.init() orelse return; var curl = Curl.init() orelse return;
defer curl.deinit(); defer curl.deinit();
const locations_base = "https://v6.db.transport.rest/locations"; const locations_base = "https://v6.db.transport.rest/locations";
var locations_uri = std.Uri.parse(locations_base) catch unreachable; var locations_uri = std.Uri.parse(locations_base) catch unreachable;
@ -30,7 +30,7 @@ fn fetchThread(state: *state_mod.AppState) !void {
station_name_buf.resize(state.home_screen_state.station_name.items.len) catch continue; station_name_buf.resize(state.home_screen_state.station_name.items.len) catch continue;
std.mem.copyForwards(u8, station_name_buf.slice(), state.home_screen_state.station_name.items); std.mem.copyForwards(u8, station_name_buf.slice(), state.home_screen_state.station_name.items);
std.debug.print("[fetchThread] Detected update: {s}\n", .{station_name_buf.slice()}); std.debug.print("[home/fetchThread] Detected update: {s}\n", .{station_name_buf.slice()});
curl.reset(); curl.reset();
@ -38,7 +38,7 @@ fn fetchThread(state: *state_mod.AppState) !void {
defer allocator.free(query); defer allocator.free(query);
locations_uri.query = query; locations_uri.query = query;
defer locations_uri.query = null; defer locations_uri.query = null;
std.debug.print("[fetchThread] Making request to: {}\n", .{locations_uri}); std.debug.print("[home/fetchThread] Making request to: {}\n", .{locations_uri});
const url = try std.fmt.allocPrintZ(allocator, "{}", .{locations_uri}); const url = try std.fmt.allocPrintZ(allocator, "{}", .{locations_uri});
defer allocator.free(url); defer allocator.free(url);
@ -50,17 +50,17 @@ fn fetchThread(state: *state_mod.AppState) !void {
_ = curl.setopt(.write_data, .{&result}); _ = curl.setopt(.write_data, .{&result});
const code = curl.perform(); const code = curl.perform();
std.debug.print("[fetchThread] cURL Code: {}\n", .{code}); std.debug.print("[home/fetchThread] cURL Code: {}\n", .{code});
if (code != 0) continue; if (code != 0) continue;
std.debug.print("[fetchThread] Fetched data: <redacted>(len: {})\n", .{result.items.len}); std.debug.print("[home/fetchThread] Fetched data: <redacted>(len: {})\n", .{result.items.len});
const parsed = std.json.parseFromSlice([]const std.json.Value, allocator, result.items, .{}) catch |err| { const parsed = std.json.parseFromSlice([]const std.json.Value, allocator, result.items, .{}) catch |err| {
std.debug.print("[fetchThread] JSON parse error: {}\n", .{err}); std.debug.print("[home/fetchThread] JSON parse error: {}\n", .{err});
continue; continue;
}; };
defer parsed.deinit(); defer parsed.deinit();
var results = std.ArrayList(state_mod.HSSuggestion).init(allocator); var results = std.ArrayList(AppState.HSSuggestion).init(allocator);
for (parsed.value) |station| { for (parsed.value) |station| {
if (station.object.get("name")) |nameValue| { if (station.object.get("name")) |nameValue| {
const name = nameValue.string; const name = nameValue.string;
@ -85,7 +85,7 @@ fn fetchThread(state: *state_mod.AppState) !void {
} }
} }
pub fn render(state: *state_mod.AppState) !void { pub fn render(state: *AppState) !void {
var hs = &state.home_screen_state; var hs = &state.home_screen_state;
if (hs.fetch_thread == null) { if (hs.fetch_thread == null) {

4
src/main.zig

@ -1,9 +1,9 @@
const std = @import("std"); const std = @import("std");
const raylib = @import("raylib.zig"); const raylib = @import("raylib.zig");
const rl = raylib.rl; const rl = raylib.rl;
const stateMod = @import("state.zig");
const home = @import("home.zig"); const home = @import("home.zig");
const departure = @import("departure.zig"); const departure = @import("departure.zig");
const AppState = @import("state.zig");
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -25,7 +25,7 @@ pub fn main() !void {
var station_name_buffer: [100]u8 = .{0} ** 100; var station_name_buffer: [100]u8 = .{0} ** 100;
var platform_buffer: [20]u8 = .{0} ** 20; var platform_buffer: [20]u8 = .{0} ** 20;
var station_id_buffer: [10]u8 = .{0} ** 10; var station_id_buffer: [10]u8 = .{0} ** 10;
var appState = stateMod.AppState{ var appState = AppState{
.allocator = allocator, .allocator = allocator,
// .db_font = font, // .db_font = font,
.home_screen_state = .{ .home_screen_state = .{

2
src/state.zig

@ -26,11 +26,9 @@ pub const DepartureScreenState = struct {
loading: bool = false, loading: bool = false,
}; };
pub const AppState = struct {
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
close_app: bool = false, close_app: bool = false,
db_font: ?rl.Font = null, db_font: ?rl.Font = null,
screen: Screen = .home, screen: Screen = .home,
home_screen_state: HomeScreenState, home_screen_state: HomeScreenState,
departure_screen_state: DepartureScreenState, departure_screen_state: DepartureScreenState,
};

Loading…
Cancel
Save