@ -5,7 +5,7 @@ import { ConfigService } from '../_services/config.service';
import { Rect } from '../_models/generated/Rect' ;
import { ShortcutService } from '../_services/shortcut.service' ;
import { CarouselComponent } from '../carousel/carousel.component' ;
import { PlanService } from '../_services/plan.service' ;
import { PlanService , UrlError } from '../_services/plan.service' ;
import { Plan , PlanElement , PlanMetadata } from '../_models/plan' ;
import { WorldAreaService } from '../_services/world-area.service' ;
import { WorldArea } from '../_models/world-area' ;
@ -22,7 +22,6 @@ import { Event } from '@tauri-apps/api/event';
styleUrls : [ './plan-display.component.scss' ]
} )
export class PlanDisplayComponent implements AfterViewInit , OnInit {
@Input ( ) backgroundColor? : String ;
draggable : boolean = true ;
rect? : Rect ;
@ -44,6 +43,9 @@ export class PlanDisplayComponent implements AfterViewInit, OnInit {
currentPlan? : Plan ;
previousPlans : PlanMetadata [ ] = [ ] ;
checkingPlanUpdate : boolean = false ;
recentUpdateAttempts : Map < string , Date | UrlError > = new Map < string , Date | UrlError > ( ) ;
constructor ( private events : EventsService , public configService : ConfigService , private cdr : ChangeDetectorRef , private shortcut : ShortcutService , public planService : PlanService , public worldAreaService : WorldAreaService , public overlayService : OverlayService , private zone : NgZone ) {
window . addEventListener ( "resize" , ( ) = > {
@ -53,6 +55,7 @@ export class PlanDisplayComponent implements AfterViewInit, OnInit {
} ) ;
this . planService . getStoredPlans ( ) . subscribe ( plans = > {
console . log ( "got new stored plans" ) ;
this . previousPlans = plans ;
} )
@ -66,6 +69,10 @@ export class PlanDisplayComponent implements AfterViewInit, OnInit {
this . registerOnZoneEnter ( ) ;
}
get disablePlans ( ) : boolean {
return this . checkingPlanUpdate ;
}
registerOnZoneEnter() {
appWindow . listen ( "entered" , ( entered ) = > {
if ( this . currentPlan ) {
@ -80,7 +87,6 @@ export class PlanDisplayComponent implements AfterViewInit, OnInit {
} ) ;
}
windowInitHandler() {
if ( window . innerWidth > 0 ) {
this . ngAfterViewInit ( ) ;
@ -304,7 +310,7 @@ export class PlanDisplayComponent implements AfterViewInit, OnInit {
this . planService . loadFromUrl ( ) . subscribe ( plan = > {
if ( plan ) {
this . planService . savePlanAtStore ( plan . name ! , plan ) . subscribe ( ( path ) = > {
if ( path ) {
if ( path ) {
plan . setPath ( path ) ;
}
} ) ;
@ -312,4 +318,66 @@ export class PlanDisplayComponent implements AfterViewInit, OnInit {
}
} ) ;
}
checkForPlanUpdate ( plan : PlanMetadata ) {
this . checkingPlanUpdate = true ;
this . planService . checkForPlanUpdate ( plan ) . subscribe ( {
next : updatedPlan = > {
console . log ( "check update: " , updatedPlan ) ;
this . planService . loadPlanFromPath ( plan . stored_path ! , false ) . subscribe ( oldPlan = > {
console . log ( "oldPlan" , oldPlan ) ;
const newCurrent = commonUUIDToNewCurrent ( oldPlan , updatedPlan ) ;
updatedPlan . current = newCurrent ;
//TODO: Interface to determine if we want to save the plan or not (allow not doing it in case user regrets... Also needs position error fix).
this . planService . savePlanAtStore ( oldPlan . name ! , updatedPlan , true ) . subscribe ( ) ;
} )
} ,
complete : ( ) = > {
this . checkingPlanUpdate = false
this . recentUpdateAttempts . set ( plan . update_url ! , new Date ( ) ) ;
} ,
error : ( err ) = > {
this . recentUpdateAttempts . set ( plan . update_url ! , err ) ;
if ( err instanceof UrlError ) {
alert ( "Error retrieving plan update (" + err . status + "): " + err . message ) ;
} else if ( err instanceof Error ) {
alert ( "Unexpected error: " + err . message ) ;
}
this . checkingPlanUpdate = false ;
}
} ) ;
}
isErr ( plan : PlanMetadata ) {
return this . recentUpdateAttempts . get ( plan . update_url ! ) instanceof UrlError ;
}
hasUpdate ( plan : PlanMetadata ) : any {
return this . recentUpdateAttempts . has ( plan . update_url ! ) ;
}
recentPlanTitle ( plan : PlanMetadata ) {
if ( ! this . hasUpdate ( plan ) ) return "Check for update" ;
if ( this . hasUpdate ( plan ) && ! this . isErr ( plan ) ) return "Up to date" ;
if ( this . hasUpdate ( plan ) && this . isErr ( plan ) ) return "Error updating" ;
return "" ;
}
}
function commonUUIDToNewCurrent ( oldPlan : Plan , updatedPlan : Plan ) : number {
let bestOldCurrent = oldPlan . current ;
let newIndex = 0 ;
//Attempt current plan element's UUID first, otherwise step backwards until we find a match
while ( bestOldCurrent >= 0 ) {
const tempNewIndex = updatedPlan . plan . findIndex ( e = > e . uuid === oldPlan . plan [ bestOldCurrent ] . uuid ) ;
if ( tempNewIndex !== - 1 ) {
newIndex = tempNewIndex ;
break ;
}
bestOldCurrent -- ;
}
return newIndex ;
}