-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVSDataExporterImpl.m
170 lines (137 loc) · 4.57 KB
/
CVSDataExporterImpl.m
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// DataExporterImpl.m
// BPTracker
//
// Created by Robert Saccone on 12/1/11.
// Copyright (c) 2017 Robert Saccone. All rights reserved.
//
#import "CVSDataExporterImpl.h"
#import <SLexUtil/CHCSVWriter.h>
@interface CVSDataExporterImpl ()
- (void)addReadingToFile:(BloodPressureReading *)bpReading;
- (void) writeHeader;
@property(nonatomic, readwrite, strong) CHCSVWriter *csvWriter;
@property(nonatomic, readwrite, strong) NSFileHandle *fileHandle;
@property(nonatomic, readwrite, strong) id<DataLogBuilder> dataLogBuilder;
@property(nonatomic, readwrite, strong) NSDateFormatter *dateFormatter;
@end
@implementation CVSDataExporterImpl
{
@private
id<DataLogBuilder> dataLogBuilder_;
NSFileHandle *fileHandle_;
CHCSVWriter *csvWriter_;
NSDateFormatter *dateFormatter_;
}
@synthesize csvWriter = csvWriter_;
@synthesize fileHandle=fileHandle_;
@synthesize dataLogBuilder=dataLogBuilder_;
@synthesize dateFormatter=dateFormatter_;
static NSString *const columnHeaderKeys[] =
{
@"CSV_DATE_FIELD_TITLE",
@"CSV_TIME_FIELD_TITLE",
@"CSV_SYSTOLIC_FIELD_TITLE",
@"CSV_DIASTOLIC_FIELD_TITLE",
@"CSV_PULSE_FIELD_TITLE",
@"CSV_WEIGHT_FIELD_TITLE",
@"CSV_NOTE_FIELD_TITLE",
nil
};
// Designated initializer
- (id)initWithFilename:(NSString *)filename
{
self = [super init];
if (self != nil)
{
if (filename == nil)
{
ALog(@"DataExportImpl: nil filename passed to initWithFileName!");
return nil;
}
csvWriter_ = [[CHCSVWriter alloc] initWithCSVFile:filename atomic:NO];
if (csvWriter_ != nil)
{
[self writeHeader];
}
else
{
ALog(@"Couldn't alloc CHCSWriter.");
return nil;
}
dateFormatter_ = [[NSDateFormatter alloc] init];
if (dateFormatter_ != nil)
{
[dateFormatter_ setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter_ setTimeStyle:NSDateFormatterMediumStyle];
}
else
{
return nil;
}
}
return self;
}
- (void) writeHeader
{
for (NSString *const *currColHdrKey = columnHeaderKeys; *currColHdrKey != nil; ++currColHdrKey)
{
NSString *colHdrStr = NSLocalizedString(*currColHdrKey, nil);
[self.csvWriter writeField:colHdrStr];
}
[self.csvWriter writeLine];
}
- (void)addReadingToFile:(BloodPressureReading *)bpReading
{
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSString *dateStr = [self.dateFormatter stringFromDate:bpReading.readingDate];
[self.dateFormatter setDateStyle:NSDateFormatterNoStyle];
[self.dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *timeStr = [self.dateFormatter stringFromDate:bpReading.readingDate];
// Encapsulate the note field string in "" in case there are embedded ','.
[self.csvWriter writeLineOfFields:dateStr,
timeStr,
bpReading.systolic,
bpReading.diastolic,
bpReading.pulse,
bpReading.weight,
bpReading.note,
nil];
}
- (void)addReadings:(NSArray *)bpReadings updateProgress:(void (^)(float))progressBlock
{
NSAssert(bpReadings != nil, @"DataExporterImpl: bpReadings is nil!");
id<DataLogBuilder> dataLogBuilder = self.dataLogBuilder;
if (progressBlock != nil)
{
NSUInteger itemCount = bpReadings.count;
NSUInteger progressInterval = 10;
NSUInteger itemsProcessed = 0;
for (BloodPressureReading *bpReading in bpReadings)
{
[self addReadingToFile:bpReading];
if ((++itemsProcessed % progressInterval) == 0)
{
progressBlock(((float)itemsProcessed) / (float)itemCount);
}
}
progressBlock(1.0f);
}
else
{
for (BloodPressureReading *bpReading in bpReadings)
{
[dataLogBuilder addReading:bpReading];
}
}
NSString *contents = [dataLogBuilder consumeContents];
NSData *data = [contents dataUsingEncoding:NSUTF8StringEncoding];
// TBD - writeData throws an exception when it fails. Add handling here.
[self.fileHandle writeData:data];
}
- (void)done
{
[self.fileHandle closeFile];
}
@end