-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimelineAdapter.cs
More file actions
143 lines (122 loc) · 4.31 KB
/
TimelineAdapter.cs
File metadata and controls
143 lines (122 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using Android.Views;
using Android.Content;
using Android.Graphics;
using Android.Text.Format;
using static Android.Views.ViewGroup;
namespace StorageHistory.Analysis
{
using Shared.UI;
using Shared.Logic;
/// <summary>
/// Manages the conversion of <see cref="Analysis.Timeline"/> data into a list of graphs for the most consequential directories.
/// </summary>
class TimelineAdapter: BaseAdapter<Timeline.Directory>
{
private DateTime startTime;
private DateTime endTime;
private Context context;
public string basePath;
public TimelineAdapter(Context context) => this.context= context;
public TimelineAdapter(Context context, Timeline source) {
this.context= context;
this.Timeline= source;
}
public Timeline Timeline {
set {
if ( value.directories == null )
@base= null;
else {
@base= new Timeline.Directory [ value.directories.Count ];
value.directories.CopyTo(@base, 0);
Array.Sort( @base );
}
startTime= value.startTime;
endTime= value.endTime;
}
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view= convertView as Graph;
if ( view == null )
view= new Graph( context );
view.minTime= this.startTime;
view.maxTime= this.endTime;
view.basePath= this.basePath;
view.Source= @base[ position ];
view.Color= view.Source.GetHashColor(); // a unique color based on the hash code of the directory path
return view;
}
/// <summary>
/// A graph that shows the change in size of a directory over time.
/// </summary>
class Graph: View
{
private Paint paint;
private Paint textPaint;
private float textPadding;
private int verticalMargins;
public string basePath;
public DateTime minTime;
public DateTime maxTime;
public Timeline.Directory Source;
private static readonly Paint AxisPaint= new Paint(PaintFlags.AntiAlias) { Color= new Color(0x7F888888) };
private static readonly Path AxisPath= new Path();
private static void initAxis(Context context)
{
float density= context.Resources.DisplayMetrics.Density,
dashLength= 8f * density;
AxisPaint.SetPathEffect( new DashPathEffect( new float[]{ dashLength, dashLength }, dashLength ) ) ;
AxisPaint.SetStyle(Paint.Style.Stroke);
AxisPaint.StrokeWidth= density;
}
public Color Color { set { paint.Color= value; textPaint.Color= value; } }
public Graph(Context context): base(context)
{
paint= new Paint(PaintFlags.AntiAlias);
textPaint= new Paint(PaintFlags.AntiAlias);
textPadding= Resources.GetDimension(Resource.Dimension.analysis_item_text_padding);
verticalMargins= Resources.GetDimensionPixelSize(Resource.Dimension.analysis_item_vertical_margins);
LayoutParameters= new LayoutParams( LayoutParams.MatchParent, Resources.GetDimensionPixelSize(Resource.Dimension.analysis_item_height) + verticalMargins );
if ( AxisPaint.PathEffect == null )
initAxis(context);
}
protected override void OnDraw(Canvas canvas)
{
// generate the graph of size changes
Source.GenerateOutput(minTime, maxTime, canvas.Width, canvas.Height-verticalMargins);
canvas.Translate( 0, verticalMargins / 2.0f );
// draw the dashed line that represents the x-axis i.e. no change in size
if ( (int)Source.AxisHeight + 1 < canvas.Height ) // if it's above the bottom of the graph
{
AxisPath.Rewind();
AxisPath.MoveTo(0, Source.AxisHeight);
AxisPath.LineTo(canvas.Width, Source.AxisHeight);
canvas.DrawPath(AxisPath, AxisPaint);
}
// draw the graph of size changes
canvas.DrawLines(Source.Output, paint);
// write the name of the directory
textPaint.TextAlign= Paint.Align.Left;
canvas.DrawText(Source.AbsoluteLocation.ToUserPath(basePath), textPadding, paint.TextSize + textPadding, textPaint);
// write the amount of the directory's change in size
textPaint.TextAlign= Paint.Align.Right;
canvas.DrawText(SizeDeltaString, canvas.Width-textPadding, canvas.Height-textPadding, textPaint);
}
private string SizeDeltaString
{
get {
long sizeDelta= Source.SizeDelta;
string prefix;
if ( sizeDelta < 0 )
{
sizeDelta= -sizeDelta;
prefix= "− ";
}
else prefix= "+ ";
return prefix + Formatter.FormatFileSize(Context, sizeDelta);
}
}
}
}
}