Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4692 from toggl-open-source/fix/timeline_ui/extra…
Browse files Browse the repository at this point in the history
…_gap_block

Fix incorrect gap blocks generation with selected date other than today
  • Loading branch information
skel35 authored Nov 18, 2020
2 parents 0a6a2ea + 05512b7 commit 2b11602
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public TimelineViewModel()
tuple.TimeEntries.Where(b => b.Key != tuple.Running?.GUID)
.ToDictionary(pair => pair.Key, pair => pair.Value))
.ToPropertyEx(this, x => x.TimeEntryBlocks);
blocksObservable.Select(blocks => GenerateGapTimeEntryBlocks(blocks.Values.ToList(), SelectedScaleMode)).ToPropertyEx(this, x => x.GapTimeEntryBlocks);
blocksObservable.Select(blocks => GenerateGapTimeEntryBlocks(blocks.Values
.Where(timeEntryBlock => timeEntryBlock.StartTime().Date <= SelectedDate && timeEntryBlock.EndTime().Date >= SelectedDate).ToList(), SelectedScaleMode))
.ToPropertyEx(this, x => x.GapTimeEntryBlocks);

this.WhenAnyValue(x => x.TimeEntryBlocks)
.Where(blocks => blocks != null && blocks.Any())
Expand Down Expand Up @@ -147,7 +149,7 @@ private static List<ActivityBlock> ConvertChunksToActivityBlocks(List<Toggl.Time
{
if (chunk.Events.Any())
{
var start = Toggl.DateTimeFromUnix(chunk.Started);
var start = chunk.StartTime();
var block = new ActivityBlock()
{
Offset = ConvertTimeIntervalToHeight(selectedDate, start, selectedScaleMode),
Expand Down Expand Up @@ -199,7 +201,7 @@ private static Dictionary<string, TimeEntryBlock> ConvertTimeEntriesToBlocks(Lis
{
if (blocks.ContainsKey(entry.GUID)) continue;

var startTime = Toggl.DateTimeFromUnix(entry.Started);
var startTime = entry.StartTime();
var ended = entry.GUID == runningEntry?.GUID
? TimelineUtils.ConvertOffsetToTime(currentTimeOffset - 1, selectedDate, TimelineConstants.ScaleModes[selectedScaleMode])
: entry.Ended;
Expand Down Expand Up @@ -293,23 +295,23 @@ private static List<GapTimeEntryBlock> GenerateGapTimeEntryBlocks(List<TimeEntry
{
var gaps = new List<GapTimeEntryBlock>();
timeEntries.Sort((te1,te2) => te1.Started.CompareTo(te2.Started));
ulong? prevEnd = null;
TimeEntryBlock lastTimeEntry = null;
foreach (var entry in timeEntries)
{
if (prevEnd != null && entry.Started > prevEnd.Value)
if (lastTimeEntry != null && entry.Started > lastTimeEntry.Ended)
{
var start = Toggl.DateTimeFromUnix(prevEnd.Value+1);
var start = lastTimeEntry.EndTime().AddSeconds(-1);
var block = new GapTimeEntryBlock((offset, height) => AddNewTimeEntry(offset, height, selectedScaleMode, start.Date))
{
Height = ConvertTimeIntervalToHeight(start, Toggl.DateTimeFromUnix(entry.Started - 1), selectedScaleMode),
Height = ConvertTimeIntervalToHeight(start, entry.StartTime().AddSeconds(-1), selectedScaleMode),
VerticalOffset =
ConvertTimeIntervalToHeight(new DateTime(start.Year, start.Month, start.Day), start, selectedScaleMode),
HorizontalOffset = 0
};
if (block.Height > 10) // Don't display to small gaps not to obstruct the view
gaps.Add(block);
}
prevEnd = !prevEnd.HasValue || entry.Ended > prevEnd ? entry.Ended : prevEnd;
lastTimeEntry = lastTimeEntry == null || entry.Ended > lastTimeEntry.Ended ? entry : lastTimeEntry;
}

return gaps;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using TogglDesktop.ViewModels;

namespace TogglDesktop
{
public class TimelineUtils
public static class TimelineUtils
{
public static ulong ConvertOffsetToTime(double height, DateTime date, double hourHeight)
{
Expand All @@ -11,5 +12,17 @@ public static ulong ConvertOffsetToTime(double height, DateTime date, double hou
var unixTime = Toggl.UnixFromDateTime(dateTime);
return unixTime >= 0 ? (ulong)unixTime : 0;
}

public static DateTime StartTime(this TimeEntryBlock block) => Toggl.DateTimeFromUnix(block.Started);

public static DateTime EndTime(this TimeEntryBlock block) => Toggl.DateTimeFromUnix(block.Ended);

public static DateTime StartTime(this Toggl.TogglTimeEntryView te) => Toggl.DateTimeFromUnix(te.Started);

public static DateTime EndTime(this Toggl.TogglTimeEntryView te) => Toggl.DateTimeFromUnix(te.Ended);

public static DateTime StartTime(this Toggl.TimelineChunkView chunk) => Toggl.DateTimeFromUnix(chunk.Started);

public static DateTime EndTime(this Toggl.TimelineChunkView chunk) => Toggl.DateTimeFromUnix(chunk.Ended);
}
}

0 comments on commit 2b11602

Please sign in to comment.