Files
spark/src/client/spark/Modules/Market/MarketView/MarketViewRowAnimationModel.cs
T
2022-12-08 13:30:54 -05:00

120 lines
5.4 KiB
C#

#nullable enable
namespace mbts.spark {
using mbts.ui;
using Spark.Core;
using Spark.Core.Async;
using System;
using System.Collections.Generic;
using System.Windows.Media;
using CellAnimation = ui.spark.CellAnimation;
using ColType = MarketViewColumnType;
class MarketViewRowAnimationModel {
static readonly TimeSpan _flash_duration = TimeSpan.FromSeconds(2);
static readonly MarketViewAnimations _bank = new() {
position_increase = new(ColType.Position, CellAnimation.Type.UpArrow, Brushes.Green),
position_decrease = new(ColType.Position, CellAnimation.Type.DownArrow, Brushes.Red),
day_change_increase = new(ColType.DayChange, CellAnimation.Type.UpArrow, Brushes.Green),
day_change_decrease = new(ColType.DayChange, CellAnimation.Type.DownArrow, Brushes.Red),
working_bids_active = new(ColType.WorkingBids, CellAnimation.Type.Border, BTSColors.UI.warning.to_brush()),
working_bids_inside = new(ColType.WorkingBids, CellAnimation.Type.Border, BTSColors.MarketData.bid.to_brush()),
working_bids_filled = new(ColType.WorkingBids, CellAnimation.Type.Border, Brushes.Green),
working_asks_active = new(ColType.WorkingAsks, CellAnimation.Type.Border, BTSColors.UI.warning.to_brush()),
working_asks_inside = new(ColType.WorkingAsks, CellAnimation.Type.Border, BTSColors.MarketData.ask.to_brush()),
working_asks_filled = new(ColType.WorkingAsks, CellAnimation.Type.Border, Brushes.Red)
};
readonly ValueTimeline<CellAnimation?> _position;
readonly ValueTimeline<CellAnimation?> _day_change;
readonly ValueTimeline<CellAnimation?> _working_bids_active;
readonly ValueTimeline<CellAnimation?> _working_bids_inside;
readonly ValueTimeline<CellAnimation?> _working_bids_filled;
readonly ValueTimeline<CellAnimation?> _working_asks_active;
readonly ValueTimeline<CellAnimation?> _working_asks_inside;
readonly ValueTimeline<CellAnimation?> _working_asks_filled;
readonly DeferrableAction _refresh;
public MarketViewRowAnimationModel() {
_refresh = new(() => animations_changed?.Invoke());
_position = mk_animation_model();
_day_change = mk_animation_model();
_working_bids_filled = mk_animation_model();
_working_bids_inside = mk_animation_model();
_working_bids_active = mk_animation_model();
_working_asks_filled = mk_animation_model();
_working_asks_inside = mk_animation_model();
_working_asks_active = mk_animation_model();
ValueTimeline<CellAnimation?> mk_animation_model() {
var animation_model = new ValueTimeline<CellAnimation?>(null);
animation_model.value_changed += _refresh.Invoke;
return animation_model;
}
}
public event Action? animations_changed;
public IEnumerable<CellAnimation> animations {
get {
return impl().SelectWhereValues();
IEnumerable<CellAnimation?> impl() {
yield return _position.value;
yield return _day_change.value;
yield return _working_bids_active.value;
yield return _working_bids_inside.value;
yield return _working_bids_filled.value;
yield return _working_asks_active.value;
yield return _working_asks_inside.value;
yield return _working_asks_filled.value;
}
}
}
public void animate_position(bool increased) {
var anim = increased ? _bank.position_increase : _bank.position_decrease;
_position.begin((anim, _flash_duration)).ignore();
}
public void animate_day_change(bool increased) {
var anim = increased ? _bank.day_change_increase : _bank.day_change_decrease;
_day_change.begin((anim, _flash_duration)).ignore();
}
public void animate_working_bids_active(bool active) =>
_working_bids_active.value = active ? _bank.working_bids_active : null;
public void animate_working_bids_inside(bool inside) =>
_working_bids_inside.value = inside ? _bank.working_bids_active : null;
public void animate_working_bids_filled() =>
_working_bids_filled.begin((
_bank.working_bids_filled, _flash_duration)).ignore();
public void animate_working_asks_active(bool active) =>
_working_asks_active.value = active ? _bank.working_asks_active : null;
public void animate_working_asks_inside(bool inside) =>
_working_asks_inside.value = inside ? _bank.working_asks_inside : null;
public void animate_working_asks_filled() =>
_working_asks_filled.begin((
_bank.working_asks_filled, _flash_duration)).ignore();
readonly struct MarketViewAnimations {
public CellAnimation position_increase { get; init; }
public CellAnimation position_decrease { get; init; }
public CellAnimation day_change_increase { get; init; }
public CellAnimation day_change_decrease { get; init; }
public CellAnimation working_bids_active { get; init; }
public CellAnimation working_bids_inside { get; init; }
public CellAnimation working_bids_filled { get; init; }
public CellAnimation working_asks_active { get; init; }
public CellAnimation working_asks_inside { get; init; }
public CellAnimation working_asks_filled { get; init; }
}
}
}