Skip to content

Commit

Permalink
settings view wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dearlordylord committed Mar 9, 2024
1 parent 6a16cd4 commit 7335ad5
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 19 deletions.
65 changes: 63 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
"scripts": {},
"private": true,
"dependencies": {
"@effect/schema": "^0.63.2",
"@expo/metro-config": "~0.17.3",
"@expo/metro-runtime": "~3.1.1",
"@react-native-async-storage/async-storage": "1.21.0",
"@react-native-picker/picker": "^2.6.1",
"@rimbu/core": "^2.0.0",
"axios": "^1.0.0",
"expo": "~50.0.3",
"expo-asset": "~9.0.2",
"expo-av": "~13.10.5",
"expo-keep-awake": "~12.8.2",
"expo-splash-screen": "~0.26.1",
"expo-status-bar": "~1.11.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "^7.51.0",
"react-is": "18.2.0",
"react-native": "0.73.2",
"react-native-paper": "^5.12.3",
Expand All @@ -26,9 +31,7 @@
"styled-components": "5.3.6",
"terminal-kit": "^3.0.1",
"ts-pattern": "^5.0.6",
"tslib": "^2.3.0",
"expo-av": "~13.10.5",
"expo-asset": "~9.0.2"
"tslib": "^2.3.0"
},
"devDependencies": {
"@babel/core": "^7.14.5",
Expand Down
18 changes: 18 additions & 0 deletions reference-react-native/src/lib/settings/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import { ModeSettings } from '@jikan0/ui';
import * as S from "@effect/schema/Schema";
import { Effect, Option, pipe, flow } from "effect"

const KEY = 'modeSettings' as const;

export const save = (settings: ModeSettings) =>
Effect.promise((_signal/*not supported by AsyncStorage*/) => AsyncStorage.setItem(KEY, JSON.stringify(settings)));

export const load = () =>
Effect.promise(async (_signal/*not supported by AsyncStorage*/) => AsyncStorage.getItem(KEY))
.pipe(
Effect.flatMap(Effect.fromNullable),
Effect.flatMap(flow(S.decode(S.parseJson()), S.decodeUnknown(ModeSettings))),
Effect.optionFromOptional,
)

27 changes: 27 additions & 0 deletions reference-react-native/src/lib/settings/view.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ModeSettings } from '@jikan0/ui';
import React from 'react';
import { Text, View } from 'react-native';
import { useForm } from 'react-hook-form';


export type Props = {
settings: ModeSettings
}

const simpleSettings: React.FC<{settings: ModeSettings['settings']['simple']}> = ({settings}) =>
{
const {register, handleSubmit} = useForm({
defaultValues: settings
});
return (
<View>

</View>
)
}

export const view: React.FC<Props> = ({settings: {selected, settings}}) =>
<View>
<View>Settings</View>
<View><Text>Mode: {selected}</Text></View>
</View>
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"rootDir": ".",
"strict": true,
"exactOptionalPropertyTypes": true,
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
Expand Down
43 changes: 29 additions & 14 deletions ui/src/lib/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from '@jikan0/fsm';
import { lastRNEA, pipe } from '@jikan0/utils';
import { Deep } from '@rimbu/core';
import * as S from "@effect/schema/Schema";


// TODO program library
// TODO fp eslint
Expand Down Expand Up @@ -166,7 +168,7 @@ type ViewActiveValue = {
};

export type State<M extends Mode = Mode> = {
mode: ModeSelectorState<M>;
mode: ModeSelectorState & {selected: M};
} & (
| {
running: RunningStateRunning | RunningStatePaused;
Expand All @@ -177,16 +179,32 @@ export type State<M extends Mode = Mode> = {
}
);

export const SimpleModeSettings = S.struct({
exerciseTimeMs: S.bigint,
restTimeMs: S.bigint,
rounds: S.bigint,
});


export type ModeSelectorSettingsValue = {
mode: Mode;
} & {
} & ({
mode: SimpleMode;
exerciseTimeMs: bigint; // TODO positive...
restTimeMs: bigint; // TODO positive...
rounds: bigint; // TODO positive...
};

export type ModeSelectorSettings = Readonly<{
} & S.Schema.To<typeof SimpleModeSettings>);

export const ModesSettings = S.struct({
simple: SimpleModeSettings,
});

export const ModeSettings = S.struct({
selected: S.literal(...MODES),
settings: ModesSettings,
});

export type ModeSettings = S.Schema.To<typeof ModeSettings>;

export type ModesSettings = Readonly<{
[k in Mode]: Readonly<
Omit<
ModeSelectorSettingsValue & {
Expand All @@ -195,12 +213,9 @@ export type ModeSelectorSettings = Readonly<{
'mode'
>
>;
}>;
}> & S.Schema.To<typeof ModeSettings>;

export type ModeSelectorState<M extends Mode> = Readonly<{
selected: M;
settings: ModeSelectorSettings;
}>;
export type ModeSelectorState = Readonly<S.Schema.To<typeof ModeSettings>>;

export const modeSelectorState0 = Object.freeze({
selected: DEFAULT_MODE,
Expand All @@ -211,10 +226,10 @@ export const modeSelectorState0 = Object.freeze({
rounds: DEFAULT_ROUNDS,
}),
}),
}) satisfies ModeSelectorState<SimpleMode>;
}) satisfies ModeSelectorState;

const selectorToProgram = (
selector: ModeSelectorState<Mode>
selector: ModeSelectorState
): ProgramPerMode[typeof selector.selected] => {
const settings = selector.settings[selector.selected];
switch (selector.selected) {
Expand Down

0 comments on commit 7335ad5

Please sign in to comment.