Skip to content

Commit

Permalink
Implement Save On Reset
Browse files Browse the repository at this point in the history
This adds a save on reset setting to the general settings. When enabled,
the splits are automatically saved when the timer is reset.

Changelog: You can now enable **Save On Reset** in the settings, which
will automatically save the splits when you reset the timer.
  • Loading branch information
CryZe committed Jun 3, 2024
1 parent ec06801 commit bbdce95
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/storage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export async function loadGeneralSettings(): Promise<GeneralSettings> {
frameRate: generalSettings.frameRate ?? FRAME_RATE_AUTOMATIC,
showControlButtons: generalSettings.showControlButtons ?? true,
showManualGameTime: generalSettings.showManualGameTime ?? false,
saveOnReset: generalSettings.saveOnReset ?? false,
speedrunComIntegration: generalSettings.speedrunComIntegration ?? true,
splitsIoIntegration: generalSettings.splitsIoIntegration ?? true,
serverUrl: generalSettings.serverUrl,
Expand Down
2 changes: 2 additions & 0 deletions src/ui/LSOEventSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class LSOEventSink {
private currentTimingMethodChanged: () => void,
private currentPhaseChanged: () => void,
private currentSplitChanged: () => void,
private onReset: () => void,
) {
this.eventSink = new EventSink(new WebEventSink(this).intoGeneric());
}
Expand Down Expand Up @@ -54,6 +55,7 @@ export class LSOEventSink {

this.currentPhaseChanged();
this.currentSplitChanged();
this.onReset();
}

public undoSplit(): void {
Expand Down
26 changes: 26 additions & 0 deletions src/ui/LiveSplit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export class LiveSplit extends React.Component<Props, State> {
() => this.currentTimingMethodChanged(),
() => this.currentPhaseChanged(),
() => this.currentSplitChanged(),
() => this.onReset(),
);

const hotkeys = props.hotkeys;
Expand Down Expand Up @@ -443,6 +444,7 @@ export class LiveSplit extends React.Component<Props, State> {
try {
const layout = this.state.layout.settingsAsJson();
await Storage.storeLayout(layout);
toast.info("Layout saved successfully.");
} catch (_) {
toast.error("Failed to save the layout.");
}
Expand Down Expand Up @@ -715,6 +717,24 @@ export class LiveSplit extends React.Component<Props, State> {
e.preventDefault();
}

async saveSplits() {
try {
const openedSplitsKey = await Storage.storeSplits(
(callback) => {
callback(this.state.eventSink.getRun(), this.state.eventSink.saveAsLssBytes());
this.state.eventSink.markAsUnmodified();
},
this.state.openedSplitsKey,
);
if (this.state.openedSplitsKey !== openedSplitsKey) {
this.setSplitsKey(openedSplitsKey);
}
toast.info("Splits saved successfully.");
} catch (_) {
toast.error("Failed to save the splits.");
}
}

onServerConnectionOpened(serverConnection: LiveSplitServer): void {
this.setState({ serverConnection });
}
Expand Down Expand Up @@ -754,4 +774,10 @@ export class LiveSplit extends React.Component<Props, State> {
});
}
}

onReset(): void {
if (this.state.generalSettings.saveOnReset) {
this.saveSplits();
}
}
}
19 changes: 18 additions & 1 deletion src/ui/SettingsEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface GeneralSettings {
frameRate: FrameRateSetting,
showControlButtons: boolean,
showManualGameTime: boolean,
// saveOnReset: boolean,
saveOnReset: boolean,
speedrunComIntegration: boolean,
splitsIoIntegration: boolean,
serverUrl: string | undefined,
Expand Down Expand Up @@ -103,6 +103,13 @@ export class SettingsEditor extends React.Component<Props, State> {
tooltip: "Shows a text box beneath the timer that allows you to manually input the game time. You start the timer and do splits by pressing the Enter key in the text box. Make sure to compare against \"Game Time\".",
value: { Bool: this.state.generalSettings.showManualGameTime },
},
{
text: "Save On Reset",
tooltip: "Determines whether to automatically save the splits when resetting the timer.",
value: {
Bool: this.state.generalSettings.saveOnReset,
},
},
],
}}
editorUrlCache={this.props.urlCache}
Expand Down Expand Up @@ -142,6 +149,16 @@ export class SettingsEditor extends React.Component<Props, State> {
});
}
break;
case 3:
if ("Bool" in value) {
this.setState({
generalSettings: {
...this.state.generalSettings,
saveOnReset: value.Bool,
},
});
}
break;
}
}}
/>
Expand Down
19 changes: 3 additions & 16 deletions src/ui/SplitsSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as SplitsIO from "../util/SplitsIO";
import { toast } from "react-toastify";
import { openFileAsArrayBuffer, exportFile, convertFileToArrayBuffer } from "../util/FileUtil";
import { Option, maybeDisposeAndThen } from "../util/OptionUtil";
import * as Storage from "../storage";
import DragUpload from "./DragUpload";
import { ContextMenuTrigger, ContextMenu, MenuItem } from "react-contextmenu";
import { GeneralSettings } from "./SettingsEditor";
Expand Down Expand Up @@ -37,6 +36,7 @@ interface Callbacks {
setSplitsKey(newKey?: number): void,
openTimerView(): void,
renderViewWithSidebar(renderedView: JSX.Element, sidebarContent: JSX.Element): JSX.Element,
saveSplits(): Promise<void>,
}

export class SplitsSelection extends React.Component<Props, State> {
Expand Down Expand Up @@ -321,21 +321,8 @@ export class SplitsSelection extends React.Component<Props, State> {
}

private async saveSplits() {
try {
const openedSplitsKey = await Storage.storeSplits(
(callback) => {
callback(this.props.eventSink.getRun(), this.props.eventSink.saveAsLssBytes());
this.props.eventSink.markAsUnmodified();
},
this.props.openedSplitsKey,
);
if (this.props.openedSplitsKey !== openedSplitsKey) {
this.props.callbacks.setSplitsKey(openedSplitsKey);
}
this.refreshDb();
} catch (_) {
toast.error("Failed to save the splits.");
}
await this.props.callbacks.saveSplits();
this.refreshDb();
}

private async uploadSplitsToSplitsIO(key: number): Promise<Option<Window>> {
Expand Down
4 changes: 2 additions & 2 deletions src/util/AutoRefresh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export interface Props {
}

export default class AutoRefresh extends React.Component<Props> {
private reqId: number | null = null;
private reqId?: number;
private previousTime: number = 0;

public componentWillMount() {
public componentDidMount() {
this.startAnimation();
}

Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as sass from "sass";
function parseChangelog() {
return execSync("git log --grep \"^Changelog: \" -10")
.toString()
.split(/^commit /)
.split(/^commit /m)
.slice(1)
.map((commit) => {
const changelogIndex = commit.indexOf(" Changelog: ");
Expand Down

0 comments on commit bbdce95

Please sign in to comment.