Broken but initial cleanup code...

cleanup
isark 1 year ago
parent 48d1dda124
commit 033b061fb6

@ -85,7 +85,16 @@ fn load_plan(path: PathBuf, state: tauri::State<Mutex<Storage>>) -> Option<Plan>
fn save_plan(path: PathBuf, plan: Plan) -> bool { fn save_plan(path: PathBuf, plan: Plan) -> bool {
if let Some(path_string) = path.with_extension("json").to_str() { if let Some(path_string) = path.with_extension("json").to_str() {
log::trace!("Attempting to save plan at path {path_string}"); log::trace!("Attempting to save plan at path {path_string}");
return Storage::save_plan(path_string, plan).is_ok(); return Storage::save_plan_at_path(path_string, plan).is_ok();
}
false
}
#[tauri::command]
fn save_plan_at_path(path: PathBuf, plan: Plan) -> bool {
if let Some(path_string) = path.with_extension("json").to_str() {
return Storage::save_plan_at_path(path_string, plan).is_ok();
} }
false false

@ -73,13 +73,11 @@ impl Storage {
} }
} }
pub fn save_plan<T: Into<String>>(file: T, plan: Plan) -> Result<(), Box<dyn Error>> { pub fn save_plan_at_path<T: Into<String>>(file: T, plan: Plan) -> Result<(), Box<dyn Error>> {
std::fs::write(&file.into(), serde_json::to_string(&plan)?)?; std::fs::write(&file.into(), serde_json::to_string(&plan)?)?;
Ok(()) Ok(())
} }
pub fn load_plan<T: Into<String>>(file: T) -> Option<Plan> { pub fn load_plan<T: Into<String>>(file: T) -> Option<Plan> {
let mut file = file.into(); let mut file = file.into();
let plan_file = Path::new(&file); let plan_file = Path::new(&file);

@ -14,9 +14,9 @@ export class Plan {
current: number; current: number;
update_url?: string; update_url?: string;
name?: string; name?: string;
private path?: string;
private saveSubject: Subject<void> = new Subject<void>();
private path?: string;
private selfSaveSubject: Subject<void> = new Subject<void>();
constructor(plan: PlanInterface) { constructor(plan: PlanInterface) {
this.plan = plan.plan; this.plan = plan.plan;
@ -24,7 +24,7 @@ export class Plan {
if (plan.stored_path) { if (plan.stored_path) {
this.path = plan.stored_path; this.path = plan.stored_path;
} }
this.saveSubject.pipe(debounceTime(500)).subscribe(() => this.underlyingSave()); this.selfSaveSubject.pipe(debounceTime(500)).subscribe(() => this.directSelfSave());
} }
setPath(path: string) { setPath(path: string) {
@ -34,38 +34,24 @@ export class Plan {
next() { next() {
if (this.current + 1 < this.plan!.length) { if (this.current + 1 < this.plan!.length) {
this.current++; this.current++;
this.save(); this.requestSelfSave();
} }
} }
prev() { prev() {
if (this.current - 1 >= 0) { if (this.current - 1 >= 0) {
this.current--; this.current--;
this.save(); this.requestSelfSave();
} }
} }
setPrevious(prev: string) { private requestSelfSave() {
from(invoke<string>('path_for_previous', { prev })).subscribe(path => {
if (path) {
this.setPath(path);
}
});
}
setPreviousPlan(prev: Plan) {
if (prev.path) {
this.setPath(prev.path);
}
}
private save() {
if (this.path) { if (this.path) {
this.saveSubject.next(); this.selfSaveSubject.next();
} }
} }
private underlyingSave() { private directSelfSave() {
console.log("Underlying save"); console.log("Underlying save");
invoke('save_plan', { invoke('save_plan', {
plan: { plan: {
@ -74,6 +60,7 @@ export class Plan {
}, path: this.path }, path: this.path
}); });
} }
} }
export interface PlanElement { export interface PlanElement {

@ -10,115 +10,39 @@ import { fetch } from '@tauri-apps/api/http';
providedIn: 'root' providedIn: 'root'
}) })
export class PlanService { export class PlanService {
currentPlan?: Plan;
planStore: Map<string, Plan> = new Map<string, Plan>();
basePlan?: Plan;
private basePlanSubj: Subject<Plan> = new ReplaySubject<Plan>(1);
private _currentPlanSubject: Subject<Plan> = new ReplaySubject<Plan>(1);
private _basePlanSubject: Subject<Plan> = new ReplaySubject<Plan>(1);
constructor(private dialog: MatDialog) { constructor(private dialog: MatDialog) {
this.getPreviousPlans();
this.loadBasePlan(); this.loadBasePlan();
} }
loadPlan(path: string) { public getBasePlan(): Observable<Plan> {
return from(invoke<PlanInterface>('load_plan', { path })).pipe( return this._basePlanSubject.asObservable();
map(plan => {
this.currentPlan = new Plan(plan);
return plan
})
);
}
loadPlanNoSave(path: string) {
return from(invoke<PlanInterface>('load_plan', { path })).pipe(
map(plan => {
this.currentPlan = new Plan(plan);
this.currentPlan.setPath(path);
return plan
})
);
}
loadBasePlan() {
if (!this.basePlan) {
from(invoke<PlanInterface>('base_plan')).subscribe(plan => {
plan.plan.forEach(elem => { elem.edited = false; });
this.basePlan = new Plan(plan);
this.basePlanSubj?.next(this.basePlan);
});
}
return this.basePlanSubj!.asObservable();
}
savePlan(path: string, plan: Plan) {
plan.plan.forEach(elem => {
if (!elem.notes) { elem.notes = "" }
});
return from(invoke<boolean>('save_plan', {
path,
plan: {
plan: plan.plan,
current: plan.current
},
})).subscribe(status => {
});
} }
savePlanByName(name: string, plan: Plan) { public getCurrentPlan(): Observable<Plan> {
plan.plan.forEach(elem => { return this._currentPlanSubject.asObservable();
if (!elem.notes) { elem.notes = "" }
});
return from(invoke<boolean>('save_plan_by_name', {
name,
plan: {
plan: plan.plan,
current: plan.current,
update_url: plan.update_url
},
})).subscribe(status => {
});
} }
getPreviousPlans() { public setCurrentPlan(plan: Plan) {
from(invoke<Map<string, Plan>>('load_stored_plans')).subscribe(plans => this.planStore = plans); this._currentPlanSubject.next(plan);
} }
loadPrevious(name: string) { public enumerateStoredPlans(): Observable<string[]> {
return from(invoke<PlanInterface>('load_previous', { name })).pipe(tap(plan => { return from(invoke<string[]>('enumerate_stored_plans'));
console.log("previous loaded: ", plan);
this.currentPlan = new Plan(plan);
this.currentPlan.setPrevious(name);
}));
} }
loadPreviousPlan(plan: Plan): Observable<PlanInterface> { public getPathFromKnownName(name: string): Observable<string> {
return new Observable<Plan>(observer => { return from(invoke<string>('path_from_name', { name }));
observer.next(plan);
}).pipe(map(plan => {
console.log("previous loaded: ", plan);
this.currentPlan = plan;
this.currentPlan.setPreviousPlan(plan);
return plan as PlanInterface;
}));
} }
zoneFromUuid(uuid: string) { public loadPlanFromPath(path: string): Observable<Plan> {
if (!this.basePlan) { return from(invoke<Plan>('load_plan', { path }));
return undefined;
}
return this.basePlan.plan.find(elem => elem.uuid === uuid);
} }
loadFromUrl(url?: string, name?: string): Observable<PlanInterface> { public loadFromUrl(url?: string, name?: string): Observable<Plan> {
if (!url || !name) { if (!url || !name) {
const dialogRef = this.dialog.open(UrlDialog, { const dialogRef = this.dialog.open(UrlDialog, {
data: { data: {
@ -128,12 +52,11 @@ export class PlanService {
}); });
return dialogRef.afterClosed().pipe(switchMap(data => { return dialogRef.afterClosed().pipe(switchMap(data => {
console.log("import from url data: ", data);
if (data.url) { if (data.url) {
return this._loadFromUrl(data.url, data.name); return this._loadFromUrl(data.url, data.name);
} }
return new Observable<PlanInterface>((s) => s.complete()); return new Observable<Plan>((s) => s.complete());
})); }));
} else { } else {
@ -141,22 +64,29 @@ export class PlanService {
} }
} }
private _loadFromUrl(url: string, name: string): Observable<PlanInterface> { public savePlanAtPath(path: string, plan: Plan) {
invoke('save_plan_at_path', { path, plan: plan });
}
private _loadFromUrl(url: string, name: string): Observable<Plan> {
//Tauri fetch
return from(fetch( return from(fetch(
url, url,
{ {
method: 'GET', method: 'GET',
timeout: 30 timeout: 30
})).pipe(map(response => { })).pipe(map(response => {
return response.data as PlanInterface; return new Plan(response.data as PlanInterface);
})).pipe(tap(plan => { })).pipe(tap(plan => {
plan.update_url = url; plan.update_url = url;
const planObj = new Plan(plan); plan.name = name;
planObj.update_url = url;
console.log("plan and plan interface: ", planObj, plan);
this.savePlanByName(name, planObj);
this.currentPlan = planObj;
this.getPreviousPlans();
})); }));
} }
private loadBasePlan() {
from(invoke<PlanInterface>('base_plan')).subscribe(plan => {
plan.plan.forEach(elem => { elem.edited = false; });
this._basePlanSubject?.next(new Plan(plan));
});
}
} }

@ -1,6 +1,6 @@
import { Injectable, NgZone } from '@angular/core'; import { Injectable, NgZone } from '@angular/core';
import { ShortcutHandler, register, unregister } from '@tauri-apps/api/globalShortcut'; import { ShortcutHandler, register, unregister } from '@tauri-apps/api/globalShortcut';
import { EMPTY, Observable, Subscriber, TeardownLogic, from } from 'rxjs'; import { Observable, Subscriber } from 'rxjs';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -8,8 +8,7 @@ import { EMPTY, Observable, Subscriber, TeardownLogic, from } from 'rxjs';
export class ShortcutService { export class ShortcutService {
private internalHandlers: Map<string, [ShortcutHandler, Subscriber<string>, () => void]> = new Map<string, [ShortcutHandler, Subscriber<string>, () => void]>(); private internalHandlers: Map<string, [ShortcutHandler, Subscriber<string>, () => void]> = new Map<string, [ShortcutHandler, Subscriber<string>, () => void]>();
constructor(private zone: NgZone) { constructor(private zone: NgZone) {}
}
register(shortcut: string) { register(shortcut: string) {
return new Observable<string>((subscriber) => { return new Observable<string>((subscriber) => {
@ -29,12 +28,6 @@ export class ShortcutService {
}); });
} }
unregister(shortcut: string) {
this.internalHandlers.get(shortcut)?.[1].complete();
this.internalHandlers.delete(shortcut);
}
rebind_from_to(previousShortcut: string, nextShortcut: string) { rebind_from_to(previousShortcut: string, nextShortcut: string) {
let [oldHandler, subscriber, teardown] = this.internalHandlers.get(previousShortcut)!; let [oldHandler, subscriber, teardown] = this.internalHandlers.get(previousShortcut)!;

@ -51,10 +51,10 @@ interface Act {
], ],
}) })
export class EditorComponent implements OnInit { export class EditorComponent implements OnInit {
planInEditing: Plan;
areas?: WorldArea[]; areas?: WorldArea[];
planAreas: WorldArea[]; planAreas: WorldArea[];
plan: Plan;
areasMap?: Map<String, WorldArea>; areasMap?: Map<String, WorldArea>;
areaSearchString: string = ""; areaSearchString: string = "";
planSearchString: string = ""; planSearchString: string = "";
@ -69,7 +69,7 @@ export class EditorComponent implements OnInit {
constructor(public worldAreaService: WorldAreaService, private cdr: ChangeDetectorRef, private planService: PlanService, public dialog: MatDialog) { constructor(public worldAreaService: WorldAreaService, private cdr: ChangeDetectorRef, private planService: PlanService, public dialog: MatDialog) {
this.plan = new Plan({ this.planInEditing = new Plan({
plan: [], plan: [],
current: 0 current: 0
}); });
@ -77,7 +77,7 @@ export class EditorComponent implements OnInit {
this.autoScrollToEnd = false; this.autoScrollToEnd = false;
this.planFuzzer = new Fuzzr(this.plan.plan, { this.planFuzzer = new Fuzzr(this.planInEditing.plan, {
toString: (e: PlanElement) => { toString: (e: PlanElement) => {
return this.areasMap?.get(e.area_key)?.name; return this.areasMap?.get(e.area_key)?.name;
} }
@ -113,11 +113,11 @@ export class EditorComponent implements OnInit {
dropHandler(event: CdkDragDrop<WorldArea[]> | CdkDragDrop<PlanElement[]>) { dropHandler(event: CdkDragDrop<WorldArea[]> | CdkDragDrop<PlanElement[]>) {
if (event.previousContainer === event.container && !isWorldAreaEvent(event)) { if (event.previousContainer === event.container && !isWorldAreaEvent(event)) {
const realCurrent = this.plan.plan.indexOf(event.previousContainer.data[event.currentIndex]); const realCurrent = this.planInEditing.plan.indexOf(event.previousContainer.data[event.currentIndex]);
const realPrev = this.plan.plan.indexOf(event.previousContainer.data[event.previousIndex]); const realPrev = this.planInEditing.plan.indexOf(event.previousContainer.data[event.previousIndex]);
moveItemInArray(this.plan.plan, realPrev, realCurrent); moveItemInArray(this.planInEditing.plan, realPrev, realCurrent);
} else } else
if (this.plan && this.areas && isWorldAreaEvent(event)) { if (this.planInEditing && this.areas && isWorldAreaEvent(event)) {
if (event.container.data.length > 0 && 'connections_world_areas_keys' in event.container.data[0]) { if (event.container.data.length > 0 && 'connections_world_areas_keys' in event.container.data[0]) {
return; return;
} }
@ -127,17 +127,15 @@ export class EditorComponent implements OnInit {
if(bounds) { if(bounds) {
index += bounds[0]; index += bounds[0];
} }
this.plan.plan.splice(index, 0, this.planItemFromArea(event.previousContainer.data[event.previousIndex])); this.planInEditing.plan.splice(index, 0, this.planItemFromArea(event.previousContainer.data[event.previousIndex]));
} }
} }
dropEndHandler(event: CdkDragDrop<WorldArea[]> | CdkDragDrop<PlanElement[]>) { dropEndHandler(event: CdkDragDrop<WorldArea[]> | CdkDragDrop<PlanElement[]>) {
if (isWorldAreaEvent(event) && this.areas) { if (isWorldAreaEvent(event) && this.areas) {
this.plan.plan.splice(this.getEnd(), 0, this.planItemFromArea(event.previousContainer.data[event.previousIndex])); this.planInEditing.plan.splice(this.getEnd(), 0, this.planItemFromArea(event.previousContainer.data[event.previousIndex]));
} else { } else {
moveItemInArray(this.plan.plan, event.previousIndex, this.getEnd()); moveItemInArray(this.planInEditing.plan, event.previousIndex, this.getEnd());
} }
this.scrollToEnd(); this.scrollToEnd();
@ -148,12 +146,12 @@ export class EditorComponent implements OnInit {
if (bounds) { if (bounds) {
return bounds[1]; return bounds[1];
} else { } else {
return this.plan.plan.length; return this.planInEditing.plan.length;
} }
} }
remove(item: PlanElement) { remove(item: PlanElement) {
this.plan.plan.splice(this.planIndexOf(item), 1); this.planInEditing.plan.splice(this.planIndexOf(item), 1);
} }
canDrop = () => { canDrop = () => {
@ -192,13 +190,13 @@ export class EditorComponent implements OnInit {
} }
doubleClickArea(item: WorldArea) { doubleClickArea(item: WorldArea) {
this.plan.plan.splice(this.plan.plan.length, 0, this.planItemFromArea(item)); this.planInEditing.plan.splice(this.planInEditing.plan.length, 0, this.planItemFromArea(item));
this.scrollToEnd(); this.scrollToEnd();
} }
planElemFilterBounds() { planElemFilterBounds() {
if(this.planFilterAct.value !== 0) { if(this.planFilterAct.value !== 0) {
let bounds = this.plan.plan.filter(item => item.anchor_act === this.planFilterAct.value || this.planFilterAct.value + 1 === item.anchor_act).map((value) => this.planIndexOf(value)); let bounds = this.planInEditing.plan.filter(item => item.anchor_act === this.planFilterAct.value || this.planFilterAct.value + 1 === item.anchor_act).map((value) => this.planIndexOf(value));
console.log("bounds, ", bounds); console.log("bounds, ", bounds);
if (bounds.length == 2) { if (bounds.length == 2) {
return bounds; return bounds;
@ -206,7 +204,7 @@ export class EditorComponent implements OnInit {
if(bounds.length == 1 && this.planFilterAct.value == 10) { if(bounds.length == 1 && this.planFilterAct.value == 10) {
console.log("Setting bound to last index"); console.log("Setting bound to last index");
bounds[1] = this.plan.plan.length; bounds[1] = this.planInEditing.plan.length;
return bounds; return bounds;
} }
} }
@ -241,7 +239,7 @@ export class EditorComponent implements OnInit {
} }
} else { } else {
return this.plan.plan; return this.planInEditing.plan;
} }
} }
@ -253,12 +251,12 @@ export class EditorComponent implements OnInit {
} }
planIndexOf(planElement: PlanElement) { planIndexOf(planElement: PlanElement) {
const index = this.plan.plan.indexOf(planElement); const index = this.planInEditing.plan.indexOf(planElement);
return index; return index;
} }
clearPlan() { clearPlan() {
this.plan.plan.length = 0; this.planInEditing.plan.length = 0;
this.cdr.detectChanges(); this.cdr.detectChanges();
} }
@ -270,7 +268,7 @@ export class EditorComponent implements OnInit {
}] }]
})).subscribe(file => { })).subscribe(file => {
if (file) { if (file) {
this.planService.savePlan(file as string, this.plan); this.planService.savePlan(file as string, this.planInEditing);
} }
}); });
} }
@ -287,8 +285,8 @@ export class EditorComponent implements OnInit {
})).subscribe(file => { })).subscribe(file => {
if (file) { if (file) {
this.planService.loadPlanNoSave(file as string).subscribe(plan => { this.planService.loadPlanNoSave(file as string).subscribe(plan => {
this.plan.plan.length = 0; this.planInEditing.plan.length = 0;
plan.plan.forEach(p => this.plan.plan.push(p)); plan.plan.forEach(p => this.planInEditing.plan.push(p));
}); });
} }
}); });
@ -296,8 +294,8 @@ export class EditorComponent implements OnInit {
loadBasePlan() { loadBasePlan() {
this.planService.loadBasePlan().subscribe(plan => { this.planService.loadBasePlan().subscribe(plan => {
this.plan.plan.length = 0; this.planInEditing.plan.length = 0;
plan.plan.forEach(p => this.plan.plan.push(p)); plan.plan.forEach(p => this.planInEditing.plan.push(p));
}) })
} }

@ -1,7 +1,7 @@
import { AfterViewInit, Component, ElementRef, Inject, Input, ViewChild, ViewEncapsulation } from '@angular/core'; import { AfterViewInit, Component, ElementRef, Inject, Input, ViewChild, ViewEncapsulation } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef, MatDialogModule } from '@angular/material/dialog'; import { MAT_DIALOG_DATA, MatDialogRef, MatDialogModule } from '@angular/material/dialog';
import { CommonModule, NgIf } from '@angular/common'; import { CommonModule } from '@angular/common';
import { MatButton, MatButtonModule } from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { MatInputModule } from '@angular/material/input'; import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field'; import { MatFormFieldModule } from '@angular/material/form-field';

Loading…
Cancel
Save