Add auto converter - 1.186 🚀

Closes #193
This commit is contained in:
Brandon Presley 2023-11-15 11:00:51 +13:00
parent 1b217f1905
commit b68587f514
9 changed files with 73 additions and 9 deletions

View File

@ -30,6 +30,7 @@ import StackHeader from "./StackHeader";
import { toast } from "./toast";
import Select from "./Select";
import { PERMISSIONS, RESULTS, check, request } from "react-native-permissions";
import { convert } from "./conversions";
export default function EditSet() {
const { params } = useRoute<RouteProp<StackParams, "EditSet">>();
@ -97,12 +98,19 @@ export default function EditSet() {
const handleSubmit = async () => {
if (!name) return;
let newWeight = Number(weight);
let newUnit = unit;
if (settings.autoConvert && unit !== settings.autoConvert) {
newUnit = settings.autoConvert;
newWeight = convert(newWeight, unit, settings.autoConvert);
}
const newSet: Partial<GymSet> = {
id: set.id,
name,
reps: Number(reps || 0),
weight: Number(weight || 0),
unit,
weight: newWeight,
unit: newUnit,
minutes: Number(set.minutes ?? 3),
seconds: Number(set.seconds ?? 30),
sets: set.sets ?? 3,

View File

@ -214,6 +214,27 @@ export default function SettingsPage() {
/>
),
},
{
name: "Auto convert",
renderItem: (name: string) => (
<Select
label={name}
items={[
{ label: "Off", value: "" },
{ label: "Kilograms", value: "kg" },
{ label: "Pounds", value: "lb" },
{ label: "Stone", value: "stone" },
]}
value={settings.autoConvert}
onChange={async (value) => {
setValue("autoConvert", value);
await update("autoConvert", value);
if (value) toast(`Sets now automatically convert to ${value}`);
else toast("Stopped automatically converting sets.");
}}
/>
),
},
{
name: "Vibration duration (ms)",
renderItem: (name: string) => (

View File

@ -28,6 +28,7 @@ import StartPlanItem from "./StartPlanItem";
import { toast } from "./toast";
import { PERMISSIONS, RESULTS, check, request } from "react-native-permissions";
import Select from "./Select";
import { convert } from "./conversions";
export default function StartPlan() {
const { params } = useRoute<RouteProp<StackParams, "StartPlan">>();
@ -101,11 +102,19 @@ export default function StartPlan() {
const exercise = counts[selected];
const best = await getBestSet(exercise.name);
delete best.id;
let newWeight = Number(weight);
let newUnit = unit;
if (settings.autoConvert && unit !== settings.autoConvert) {
newUnit = settings.autoConvert;
newWeight = convert(newWeight, unit, settings.autoConvert);
}
const newSet: GymSet = {
...best,
weight: +weight,
reps: +reps,
unit,
weight: newWeight,
reps: Number(reps),
unit: newUnit,
created: now,
hidden: false,
};
@ -113,7 +122,8 @@ export default function StartPlan() {
await refresh();
if (
settings.notify &&
(+weight > best.weight || (+reps > best.reps && +weight === best.weight))
(+weight > best.weight ||
(Number(reps) > best.reps && +weight === best.weight))
) {
toast("Great work King! That's a new record.");
}

View File

@ -85,8 +85,8 @@ android {
applicationId "com.massive"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 36211
versionName "1.185"
versionCode 36212
versionName "1.186"
}
signingConfigs {
release {

11
conversions.ts Normal file
View File

@ -0,0 +1,11 @@
export function convert(weight: number, fromUnit: string, toUnit: string) {
let result = Number(weight);
if (fromUnit === "lb" && toUnit === "kg") result /= 2.2;
else if (fromUnit === "kg" && toUnit === "lb") result *= 2.2;
else if (fromUnit === "stone" && toUnit === "kg") result *= 6.35;
else if (fromUnit === "kg" && toUnit === "stone") result /= 6.35;
else if (fromUnit === "stone" && toUnit === "lb") result *= 14;
else if (fromUnit === "lb" && toUnit === "stone") result /= 14;
result = Math.round((result + Number.EPSILON) * 100) / 100;
return result;
}

View File

@ -36,6 +36,7 @@ import Weight from "./weight";
import { settingsStartup1699783784680 } from "./migrations/1699783784680-settings-startup";
import { settingsBackupDir1699839054226 } from "./migrations/1699839054226-settings-backup-dir";
import { homeHistoryStartup1699853245534 } from "./migrations/1699853245534-home-history-startup";
import { autoConvert1699948105001 } from "./migrations/1699948105001-auto-convert";
export const AppDataSource = new DataSource({
type: "react-native",
@ -78,5 +79,6 @@ export const AppDataSource = new DataSource({
settingsStartup1699783784680,
settingsBackupDir1699839054226,
homeHistoryStartup1699853245534,
autoConvert1699948105001,
],
});

View File

@ -0,0 +1,9 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class autoConvert1699948105001 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query("ALTER TABLE settings ADD COLUMN autoConvert TEXT");
}
public async down(queryRunner: QueryRunner): Promise<void> {}
}

View File

@ -1,6 +1,6 @@
{
"name": "massive",
"version": "1.185",
"version": "1.186",
"private": true,
"license": "GPL-3.0-only",
"scripts": {

View File

@ -52,4 +52,7 @@ export default class Settings {
@Column("text")
startup: string | null;
@Column("text")
autoConvert: string | null;
}