Skip to content

Commit

Permalink
refactor: improve checkClock() and getWeekdays()
Browse files Browse the repository at this point in the history
  • Loading branch information
diegopvlk committed Jul 24, 2024
1 parent 7f7c422 commit fc7430c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 37 deletions.
4 changes: 4 additions & 0 deletions data/styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ row:focus {
font-size: 90%;
}

.history .badge-content {
text-transform: none;
}

.badge-icon {
background-color: alpha(currentColor, 0.1);
padding: 0 4px;
Expand Down
20 changes: 8 additions & 12 deletions src/historyFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Gtk from 'gi://Gtk';
import Gdk from 'gi://Gdk';
import Pango from 'gi://Pango';

import { clockIs12 } from './utils.js';
import { amPmStr, clockIs12 } from './utils.js';

export const historyHeaderFactory = new Gtk.SignalListItemFactory();
export const historyItemFactory = new Gtk.SignalListItemFactory();
Expand Down Expand Up @@ -150,26 +150,22 @@ historyItemFactory.connect('bind', (factory, listItem) => {
let [hours, minutes] = item.time;
let period = '';
if (clockIs12) {
period = ' AM';
if (hours >= 12) period = ' PM';
period = ` ${amPmStr[0]}`;
if (hours >= 12) period = ` ${amPmStr[1]}`;
if (hours > 12) hours -= 12;
if (hours === 0) hours = 12;
}

const h = clockIs12 ? String(hours) : String(hours).padStart(2, 0);
const m = String(minutes).padStart(2, 0);

nameLabel.label = item.name;
doseLabel.label = `${item.dose} ${item.unit} • ${h}${m}` + period;

let takenTime = localDT.format('%X').replace(':', '∶');
let parts = takenTime.split(' ');
takenTime = parts[0].slice(0, -3);

if (parts.length > 1) {
// if the time has AM/PM
takenTime += ' ' + parts[1];
takenTime = takenTime[0] !== '0' ? takenTime : takenTime.substring(1);
}
let takenH = clockIs12 ? localDT.format('%l') : localDT.format('%k');
takenH = takenH.replace(' ', '');
let takenM = localDT.format('%M');
const takenTime = `${takenH}${takenM}` + period;

if (item.taken[1] === 1) {
takenLabel.label = `${takenTime}`;
Expand Down
6 changes: 3 additions & 3 deletions src/todayFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Gdk from 'gi://Gdk';
import Gtk from 'gi://Gtk';
import Pango from 'gi://Pango';

import { clockIs12 } from './utils.js';
import { amPmStr, clockIs12 } from './utils.js';

export const todayHeaderFactory = new Gtk.SignalListItemFactory();
export const todayItemFactory = new Gtk.SignalListItemFactory();
Expand All @@ -33,8 +33,8 @@ todayHeaderFactory.connect('bind', (factory, listHeaderItem) => {
let period = '';

if (clockIs12) {
period = ' AM';
if (hours >= 12) period = ' PM';
period = ` ${amPmStr[0]}`;
if (hours >= 12) period = ` ${amPmStr[1]}`;
if (hours > 12) hours -= 12;
if (hours === 0) hours = 12;
}
Expand Down
55 changes: 35 additions & 20 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';

export const firstWeekday = getWeekdays()[0];
const firstWorkDay = getWeekdays()[1];
const decoder = new TextDecoder('utf-8');

const [firstWeekday, firstWorkDay] = getWeekdays();
export { firstWeekday, firstWorkDay };

function getWeekdays() {
let firstWeekday;
let firstWorkDay;
let firstWeekday = 1;
let firstWorkDay = 2;

const [success0, output0, error0] = GLib.spawn_command_line_sync('locale first_weekday');
const [success1, output1, error1] = GLib.spawn_command_line_sync('locale first_workday');
try {
const [, weekdayOutput] = GLib.spawn_command_line_sync('locale first_weekday');
const [, workDayOutput] = GLib.spawn_command_line_sync('locale first_workday');

if (success0 && success1) {
const decoder = new TextDecoder('utf-8');
// minus 1 because `locale` days are 1 to 7
firstWeekday = +decoder.decode(output0) - 1;
firstWorkDay = +decoder.decode(output1) - 1;
} else {
log(`Error getting locale: ${(error0, error1)}`);
firstWeekday = +decoder.decode(weekdayOutput) - 1;
firstWorkDay = +decoder.decode(workDayOutput) - 1;
} catch (error) {
console.error(error);
}

return [firstWeekday, firstWorkDay];
Expand Down Expand Up @@ -57,12 +58,26 @@ export function getSpecificDaysLabel(item) {
return newLabel;
}

export const clockIs12 = checkClock();
const [clockIs12, amPmStr] = checkClock();
export { clockIs12, amPmStr };

function checkClock() {
const currentTime = GLib.DateTime.new_now_local();
const timeFormat = currentTime.format('%X').slice(-2);
return timeFormat === 'AM' || timeFormat === 'PM';
let is12 = false;
let amPmStr = '';

try {
const [, out] = GLib.spawn_command_line_sync('locale am_pm');
const output = decoder.decode(out).replace('\n', '');
const timeFormat = GLib.DateTime.new_now_local().format('%X');

amPmStr = output === ';' ? amPmStr : output;
amPmStr = amPmStr.split(';');
is12 = amPmStr.length > 1 && amPmStr.some(str => timeFormat.includes(str));
} catch (error) {
console.error(error);
}

return [is12, amPmStr];
}

export const DataDir = Gio.file_new_for_path(GLib.build_filenamev([GLib.get_user_data_dir()]));
Expand Down Expand Up @@ -230,7 +245,7 @@ export function doseRow(timeDose) {
let period = '';

if (clockIs12) {
period = hours < 12 ? 'AM' : 'PM';
period = hours < 12 ? `${amPmStr[0]}` : `${amPmStr[1]}`;
if (hours > 12) hours -= 12;
if (hours === 0) hours = 12;
}
Expand Down Expand Up @@ -325,7 +340,7 @@ export function doseRow(timeDose) {
visible: clockIs12,
});
amPmButton.connect('clicked', btn => {
btn.label = btn.label === 'AM' ? 'PM' : 'AM';
btn.label = btn.label === `${amPmStr[0]}` ? `${amPmStr[1]}` : `${amPmStr[0]}`;
});

if (clockIs12) {
Expand Down Expand Up @@ -359,8 +374,8 @@ export function getTimeBtnInput(currentDoseRow) {

// The time is stored in 24h format
if (clockIs12) {
if (period === 'AM' && hours === 12) hours = 0;
if (period === 'PM' && hours !== 12) hours += 12;
if (period === `${amPmStr[0]}` && hours === 12) hours = 0;
if (period === `${amPmStr[1]}` && hours !== 12) hours += 12;
}

return [hours, minutes, amPmButton, doseTimeButton];
Expand Down
5 changes: 3 additions & 2 deletions src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
datesPassedDiff,
clockIs12,
getWidgetByName,
amPmStr,
} from './utils.js';

export const historyLS = Gio.ListStore.new(MedicationObject);
Expand Down Expand Up @@ -515,8 +516,8 @@ export const DosageWindow = GObject.registerClass(
let period = '';

if (clockIs12) {
period = ' AM';
if (h >= 12) period = ' PM';
period = ` ${amPmStr[0]}`;
if (h >= 12) period = ` ${amPmStr[1]}`;
if (h > 12) h -= 12;
if (h === 0) h = 12;
}
Expand Down

0 comments on commit fc7430c

Please sign in to comment.