Skip to content

Commit

Permalink
[CastIt] Show the played time in each file
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Sep 19, 2020
1 parent 61ed498 commit 476152d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
3 changes: 1 addition & 2 deletions CastIt/Common/AppConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public static string AppVersion
=> FileVersionInfo.GetVersionInfo(typeof(AppConstants).Assembly.Location).FileVersion;
#endif


public static IReadOnlyList<string> AppAccentColors => new List<string>
{
AccentColorLightBlue, AccentColorLimeGreen, AccentColorPink,
Expand All @@ -101,7 +100,7 @@ public static string FormatDuration(double seconds)
//here backslash is used to tell that colon is
//not the part of format, it just a character that we want in output
var time = TimeSpan.FromSeconds(seconds);
return time.ToString(time.Hours > 0 ? AppConstants.FullElapsedTimeFormat : AppConstants.ShortElapsedTimeFormat);
return time.ToString(time.Hours > 0 ? FullElapsedTimeFormat : ShortElapsedTimeFormat);
}
}
}
17 changes: 15 additions & 2 deletions CastIt/ViewModels/Items/FileItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using MvvmCross.Logging;
using MvvmCross.Plugin.Messenger;
using MvvmCross.ViewModels;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -33,6 +32,7 @@ public class FileItemViewModel : BaseViewModel
private double _playedPercentage;
private bool _isBeingPlayed;
private bool _loop;
private string _playedTime;
#endregion

#region Properties
Expand Down Expand Up @@ -137,6 +137,16 @@ public bool IsCached
=> !string.IsNullOrWhiteSpace(Name) && !string.IsNullOrWhiteSpace(Description) && !string.IsNullOrWhiteSpace(Path);

public FFProbeFileInfo FileInfo { get; set; }

public double PlayedSeconds
=> PlayedPercentage * TotalSeconds / 100;

//had to do it this way, so the ui does not call this prop each time i scroll
public string PlayedTime
{
get => _playedTime ??= AppConstants.FormatDuration(PlayedSeconds);
set => this.RaiseAndSetIfChanged(ref _playedTime, value);
}
#endregion

#region Commands
Expand Down Expand Up @@ -258,7 +268,10 @@ public void CleanUp()
}

private void OnPositionChanged(double position)
=> PlayedPercentage = position;
{
PlayedPercentage = position;
PlayedTime = AppConstants.FormatDuration(PlayedSeconds);
}

private void OnEndReached()
=> OnPositionChanged(100);
Expand Down
38 changes: 29 additions & 9 deletions CastIt/ViewModels/Items/PlayListItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,24 @@ public FileItemViewModel SelectedItem

public bool ShowTotalDuration
=> _appSettings.ShowPlayListTotalDuration;

public string PlayedTime
{
get
{
var playedSeconds = Items.Sum(i => i.PlayedSeconds);
var formatted = AppConstants.FormatDuration(playedSeconds);
return $"{formatted}";
}
}

public string TotalDuration
{
get
{
var totalSeconds = Items.Select(i => i.TotalSeconds).Sum();
var totalSeconds = Items.Where(i => i.TotalSeconds >= 0).Sum(i => i.TotalSeconds);
var formatted = AppConstants.FormatDuration(totalSeconds);
return $"{GetText("Total")}: {formatted}";
return $"{PlayedTime} / {formatted}";
}
}
#endregion
Expand Down Expand Up @@ -205,7 +216,11 @@ public override void RegisterMessages()
base.RegisterMessages();
SubscriptionTokens.AddRange(new[]
{
Messenger.Subscribe<ShowPlayListTotalDurationMessage>(_ => RaisePropertyChanged(() => ShowTotalDuration))
Messenger.Subscribe<ShowPlayListTotalDurationMessage>(async _ =>
{
await RaisePropertyChanged(() => ShowTotalDuration);
await UpdatePlayedTime();
})
});
}

Expand All @@ -219,7 +234,7 @@ public async Task SetFilesInfo(CancellationToken token)
await item.SetFileInfo(token, false);
}

await RaisePropertyChanged(() => TotalDuration);
await UpdatePlayedTime();
IsBusy = false;
}

Expand Down Expand Up @@ -258,7 +273,12 @@ public async Task RemoveFile(long id)
SelectedItems.Clear();
SetPositionIfChanged();
_appWebServer.OnFileDeleted?.Invoke(Id);
await RaisePropertyChanged(() => TotalDuration);
await UpdatePlayedTime();
}

public Task UpdatePlayedTime()
{
return !_appSettings.ShowPlayListTotalDuration ? Task.CompletedTask : RaisePropertyChanged(() => TotalDuration);
}

private Task OnFolderAdded(string[] folders)
Expand Down Expand Up @@ -326,7 +346,7 @@ private async Task OnFilesAdded(string[] paths)
finally
{
IsBusy = false;
await RaisePropertyChanged(() => TotalDuration);
await UpdatePlayedTime();
}
}

Expand Down Expand Up @@ -395,7 +415,7 @@ private async Task OnUrlAdded(string url)
finally
{
IsBusy = false;
await RaisePropertyChanged(() => TotalDuration);
await UpdatePlayedTime();
}
}

Expand All @@ -412,7 +432,7 @@ private async Task RemoveSelectedFiles()
SetPositionIfChanged();

_appWebServer.OnFileDeleted?.Invoke(Id);
await RaisePropertyChanged(() => TotalDuration);
await UpdatePlayedTime();
}

private async Task RemoveAllMissing()
Expand All @@ -426,7 +446,7 @@ private async Task RemoveAllMissing()
SetPositionIfChanged();

_appWebServer.OnFileDeleted?.Invoke(Id);
await RaisePropertyChanged(() => TotalDuration);
await UpdatePlayedTime();
}

private void SelectAll()
Expand Down
9 changes: 9 additions & 0 deletions CastIt/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,9 @@ private void OnFileDurationChanged(double seconds)
ElapsedTimeString = $"{elapsed}";
else
ElapsedTimeString = $"{elapsed} / {total}";

var playlist = PlayLists.FirstOrDefault(pl => pl.Id == _currentlyPlayedFile.PlayListId);
playlist?.UpdatePlayedTime();
}

private void OnFilePositionChanged(double playedPercentage)
Expand All @@ -1035,6 +1038,12 @@ private void OnFileEndReached()

IsPaused = false;

if (_currentlyPlayedFile != null)
{
var playlist = PlayLists.FirstOrDefault(pl => pl.Id == _currentlyPlayedFile.PlayListId);
playlist?.UpdatePlayedTime();
}

if (_currentlyPlayedFile?.Loop == true)
{
Logger.Info($"{nameof(OnFileEndReached)}: Looping file = {_currentlyPlayedFile?.Path}");
Expand Down
12 changes: 10 additions & 2 deletions CastIt/Views/UserControls/FileItem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,22 @@
Visibility="{Binding Loop, Converter={StaticResource BooleanToVisibilityConverter}}" />

<TextBlock
Grid.Row="0"
Grid.RowSpan="5"
Grid.Column="2"
Style="{StaticResource FileItemTextStyleWithTriggers}"
Text="{Binding Duration}"
TextAlignment="Right" />
TextAlignment="Right">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} / {1}">
<Binding Path="PlayedTime" />
<Binding Path="Duration" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>

<ProgressBar
Grid.Row="4"
Grid.Column="0"
Grid.ColumnSpan="3"
Margin="0,5"
VerticalAlignment="Center"
Expand Down

0 comments on commit 476152d

Please sign in to comment.