Skip to content

Commit 59a2826

Browse files
committed
Using interfaces for the repositories
1 parent 147f121 commit 59a2826

File tree

9 files changed

+224
-145
lines changed

9 files changed

+224
-145
lines changed

PomodoroTests/src/com/nullpointerengineering/android/pomodoro/persistence/EventRepositoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class EventRepositoryTests extends AndroidTestCase {
4040

4141
@Override
4242
protected void setUp(){
43-
repository = new EventRepository(getContext());
43+
repository = new SqlEventRepository(getContext());
4444
nowMillis = Instant.now().getMillis();
4545
}
4646

PomodoroTests/src/com/nullpointerengineering/android/pomodoro/persistence/TaskRepositoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class TaskRepositoryTests extends AndroidTestCase {
3838

3939
@Override
4040
protected void setUp(){
41-
repository = new TaskRepository(getContext());
41+
repository = new SqlTaskRepository(getContext());
4242
taskId = repository.createTask("Test task",1,5).getId();
4343
}
4444

src/com/nullpointerengineering/android/pomodoro/persistence/EventRepository.java

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -16,80 +16,19 @@
1616

1717
package com.nullpointerengineering.android.pomodoro.persistence;
1818

19-
import android.content.ContentResolver;
20-
import android.content.ContentUris;
21-
import android.content.ContentValues;
22-
import android.content.Context;
23-
import android.database.Cursor;
24-
import android.net.Uri;
25-
26-
import static com.nullpointerengineering.android.pomodoro.persistence.database.DatabaseConstants.*;
27-
import static com.nullpointerengineering.android.pomodoro.persistence.database.EventProvider.*;
28-
2919
/**
3020
* Created with IntelliJ IDEA.
3121
* User: Stratos
32-
* Date: 18/03/13
33-
* Time: 8:43 PM
34-
* Event Repository backed by a content provider
22+
* Date: 01/07/13
23+
* Time: 5:18 PM
24+
* To change this template use File | Settings | File Templates.
3525
*/
36-
public class EventRepository {
37-
38-
private ContentResolver resolver;
39-
40-
public EventRepository(Context context) {
41-
resolver = context.getContentResolver();
42-
}
43-
44-
public Event createEvent(String type, long timeStarted, long totalDuration, long actualDuration ) {
45-
ContentValues eventValues = new ContentValues();
46-
eventValues.put(EVENT_TIME_STARTED, timeStarted);
47-
eventValues.put(EVENT_TYPE, type);
48-
eventValues.put(EVENT_TOTAL_DURATION, totalDuration);
49-
eventValues.put(EVENT_ACTUAL_DURATION, actualDuration);
50-
51-
Uri uri = resolver.insert(CONTENT_URI, eventValues);
52-
long id = Long.parseLong(uri.getPathSegments().get(EVENT_ID_PATH_POSITION));
53-
54-
return findEventById(id);
55-
}
56-
57-
public Event findEventById(long id) {
58-
Uri eventUri = CONTENT_ID_URI_BASE;
59-
String[] projection = {
60-
EVENT_KEY_ID,
61-
EVENT_TIME_STARTED,
62-
EVENT_TYPE,
63-
EVENT_TOTAL_DURATION,
64-
EVENT_ACTUAL_DURATION
65-
};
66-
Cursor cursor = resolver.query( eventUri, projection, EVENT_KEY_ID + "=" +id, null, null);
67-
cursor.moveToFirst();
68-
return cursorToEvent(cursor);
69-
}
26+
public interface EventRepository {
27+
Event createEvent(String type, long timeStarted, long totalDuration, long actualDuration);
7028

71-
public void saveEvent(Event event) {
72-
ContentValues eventValues = new ContentValues();
73-
eventValues.put(EVENT_KEY_ID, event.getId());
74-
eventValues.put(EVENT_TIME_STARTED, event.getTimeCreated().toInstant().getMillis());
75-
eventValues.put(EVENT_TYPE, event.getType().toString());
76-
eventValues.put(EVENT_TOTAL_DURATION, event.getTotalDuration().getMillis());
77-
eventValues.put(EVENT_ACTUAL_DURATION, event.getActualDuration().getMillis());
78-
Uri eventUri = ContentUris.withAppendedId(CONTENT_ID_URI_BASE, event.getId());
79-
resolver.update(eventUri, eventValues, null, null);
80-
}
29+
Event findEventById(long id);
8130

82-
public int deleteEvent(long id) {
83-
Uri eventUri = ContentUris.withAppendedId(CONTENT_ID_URI_BASE, id);
84-
return resolver.delete(eventUri, null, null);
85-
}
31+
void saveEvent(Event event);
8632

87-
private Event cursorToEvent(Cursor cursor){
88-
long id = cursor.getLong(0);
89-
long timeCreated = cursor.getLong(1);
90-
String type = cursor.getString(2);
91-
long totalTime = cursor.getLong(3);
92-
long actualTime = cursor.getLong(4);
93-
return new Event(id, timeCreated, type, totalTime, actualTime);
94-
}
33+
int deleteEvent(long id);
9534
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2013 Efstratios Xakoustos.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.nullpointerengineering.android.pomodoro.persistence;
18+
19+
import android.content.ContentResolver;
20+
import android.content.ContentUris;
21+
import android.content.ContentValues;
22+
import android.content.Context;
23+
import android.database.Cursor;
24+
import android.net.Uri;
25+
26+
import static com.nullpointerengineering.android.pomodoro.persistence.database.DatabaseConstants.*;
27+
import static com.nullpointerengineering.android.pomodoro.persistence.database.EventProvider.*;
28+
29+
/**
30+
* Created with IntelliJ IDEA.
31+
* User: Stratos
32+
* Date: 18/03/13
33+
* Time: 8:43 PM
34+
* Event Repository backed by a content provider
35+
*/
36+
public class SqlEventRepository implements EventRepository {
37+
38+
private ContentResolver resolver;
39+
40+
public SqlEventRepository(Context context) {
41+
resolver = context.getContentResolver();
42+
}
43+
44+
@Override
45+
public Event createEvent(String type, long timeStarted, long totalDuration, long actualDuration) {
46+
ContentValues eventValues = new ContentValues();
47+
eventValues.put(EVENT_TIME_STARTED, timeStarted);
48+
eventValues.put(EVENT_TYPE, type);
49+
eventValues.put(EVENT_TOTAL_DURATION, totalDuration);
50+
eventValues.put(EVENT_ACTUAL_DURATION, actualDuration);
51+
52+
Uri uri = resolver.insert(CONTENT_URI, eventValues);
53+
long id = Long.parseLong(uri.getPathSegments().get(EVENT_ID_PATH_POSITION));
54+
55+
return findEventById(id);
56+
}
57+
58+
@Override
59+
public Event findEventById(long id) {
60+
Uri eventUri = CONTENT_ID_URI_BASE;
61+
String[] projection = {
62+
EVENT_KEY_ID,
63+
EVENT_TIME_STARTED,
64+
EVENT_TYPE,
65+
EVENT_TOTAL_DURATION,
66+
EVENT_ACTUAL_DURATION
67+
};
68+
Cursor cursor = resolver.query( eventUri, projection, EVENT_KEY_ID + "=" +id, null, null);
69+
cursor.moveToFirst();
70+
return cursorToEvent(cursor);
71+
}
72+
73+
@Override
74+
public void saveEvent(Event event) {
75+
ContentValues eventValues = new ContentValues();
76+
eventValues.put(EVENT_KEY_ID, event.getId());
77+
eventValues.put(EVENT_TIME_STARTED, event.getTimeCreated().toInstant().getMillis());
78+
eventValues.put(EVENT_TYPE, event.getType().toString());
79+
eventValues.put(EVENT_TOTAL_DURATION, event.getTotalDuration().getMillis());
80+
eventValues.put(EVENT_ACTUAL_DURATION, event.getActualDuration().getMillis());
81+
Uri eventUri = ContentUris.withAppendedId(CONTENT_ID_URI_BASE, event.getId());
82+
resolver.update(eventUri, eventValues, null, null);
83+
}
84+
85+
@Override
86+
public int deleteEvent(long id) {
87+
Uri eventUri = ContentUris.withAppendedId(CONTENT_ID_URI_BASE, id);
88+
return resolver.delete(eventUri, null, null);
89+
}
90+
91+
private Event cursorToEvent(Cursor cursor){
92+
long id = cursor.getLong(0);
93+
long timeCreated = cursor.getLong(1);
94+
String type = cursor.getString(2);
95+
long totalTime = cursor.getLong(3);
96+
long actualTime = cursor.getLong(4);
97+
return new Event(id, timeCreated, type, totalTime, actualTime);
98+
}
99+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2013 Efstratios Xakoustos.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.nullpointerengineering.android.pomodoro.persistence;
18+
19+
import android.content.*;
20+
import android.database.Cursor;
21+
import android.net.Uri;
22+
23+
import static com.nullpointerengineering.android.pomodoro.persistence.database.DatabaseConstants.*;
24+
import static com.nullpointerengineering.android.pomodoro.persistence.database.TaskProvider.*;
25+
26+
/**
27+
* Created with IntelliJ IDEA.
28+
* User: Stratos
29+
* Date: 27/01/13
30+
* Time: 6:49 PM
31+
* Task repository backed by a content provider
32+
*/
33+
34+
public class SqlTaskRepository implements TaskRepository {
35+
36+
private ContentResolver resolver;
37+
38+
public SqlTaskRepository(Context context) {
39+
resolver = context.getContentResolver();
40+
}
41+
42+
@Override
43+
public Task createTask(String title, int priority, int estimate) {
44+
ContentValues taskValues = new ContentValues();
45+
taskValues.put(TASK_TITLE, title);
46+
taskValues.put(TASK_PRIORITY, priority);
47+
taskValues.put(TASK_ESTIMATE, estimate);
48+
Uri uri = resolver.insert(CONTENT_URI, taskValues);
49+
50+
long id = Long.parseLong(uri.getPathSegments().get(TASK_ID_PATH_POSITION));
51+
52+
return findTaskById(id);
53+
}
54+
55+
@Override
56+
public Task findTaskById(long id) {
57+
Uri taskUri = CONTENT_ID_URI_BASE;
58+
String[] projection = {
59+
TASK_KEY_ID,
60+
TASK_TITLE,
61+
TASK_PRIORITY,
62+
TASK_ESTIMATE,
63+
TASK_ACTUAL,
64+
TASK_CREATED_DATE,
65+
TASK_DONE_DATE};
66+
Cursor cursor = resolver.query( taskUri, projection, TASK_KEY_ID +"="+ id, null, null);
67+
cursor.moveToFirst();
68+
return cursorToTask(cursor);
69+
}
70+
71+
@Override
72+
public void saveTask(Task task) {
73+
ContentValues taskValues = new ContentValues();
74+
taskValues.put(TASK_KEY_ID, task.getId());
75+
taskValues.put(TASK_TITLE, task.getTitle());
76+
taskValues.put(TASK_PRIORITY, task.getPriority());
77+
taskValues.put(TASK_ESTIMATE, task.getEstimate());
78+
taskValues.put(TASK_ACTUAL, task.getActual());
79+
taskValues.put(TASK_CREATED_DATE, task.getTimeCreated().getMillis());
80+
taskValues.put(TASK_DONE_DATE, task.getTimeDone().getMillis());
81+
Uri taskUri = ContentUris.withAppendedId(CONTENT_ID_URI_BASE, task.getId());
82+
resolver.update(taskUri, taskValues, null, null);
83+
}
84+
85+
@Override
86+
public int deleteTask(long id) {
87+
Uri taskUri = ContentUris.withAppendedId(CONTENT_ID_URI_BASE, id);
88+
return resolver.delete(taskUri, null, null);
89+
}
90+
91+
private Task cursorToTask(Cursor cursor) {
92+
long id = cursor.getLong(0);
93+
String title = cursor.getString(1);
94+
int priority = cursor.getShort(2);
95+
int estimate = cursor.getShort(3);
96+
int actual = cursor.getShort(4);
97+
long timeCreated = cursor.getLong(5);
98+
long timeDone = cursor.getLong(6);
99+
return new Task(id, title, priority, estimate, actual, timeCreated, timeDone);
100+
}
101+
}

src/com/nullpointerengineering/android/pomodoro/persistence/TaskRepository.java

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,82 +16,19 @@
1616

1717
package com.nullpointerengineering.android.pomodoro.persistence;
1818

19-
import android.content.*;
20-
import android.database.Cursor;
21-
import android.net.Uri;
22-
23-
import static com.nullpointerengineering.android.pomodoro.persistence.database.DatabaseConstants.*;
24-
import static com.nullpointerengineering.android.pomodoro.persistence.database.TaskProvider.*;
25-
2619
/**
2720
* Created with IntelliJ IDEA.
2821
* User: Stratos
29-
* Date: 27/01/13
30-
* Time: 6:49 PM
31-
* Task repository backed by a content provider
22+
* Date: 01/07/13
23+
* Time: 5:22 PM
24+
* To change this template use File | Settings | File Templates.
3225
*/
26+
public interface TaskRepository {
27+
Task createTask(String title, int priority, int estimate);
3328

34-
public class TaskRepository {
35-
36-
private ContentResolver resolver;
37-
38-
public TaskRepository(Context context) {
39-
resolver = context.getContentResolver();
40-
}
41-
42-
public Task createTask(String title, int priority, int estimate ) {
43-
ContentValues taskValues = new ContentValues();
44-
taskValues.put(TASK_TITLE, title);
45-
taskValues.put(TASK_PRIORITY, priority);
46-
taskValues.put(TASK_ESTIMATE, estimate);
47-
Uri uri = resolver.insert(CONTENT_URI, taskValues);
48-
49-
long id = Long.parseLong(uri.getPathSegments().get(TASK_ID_PATH_POSITION));
50-
51-
return findTaskById(id);
52-
}
53-
54-
public Task findTaskById(long id) {
55-
Uri taskUri = CONTENT_ID_URI_BASE;
56-
String[] projection = {
57-
TASK_KEY_ID,
58-
TASK_TITLE,
59-
TASK_PRIORITY,
60-
TASK_ESTIMATE,
61-
TASK_ACTUAL,
62-
TASK_CREATED_DATE,
63-
TASK_DONE_DATE};
64-
Cursor cursor = resolver.query( taskUri, projection, TASK_KEY_ID +"="+ id, null, null);
65-
cursor.moveToFirst();
66-
return cursorToTask(cursor);
67-
}
68-
69-
public void saveTask(Task task) {
70-
ContentValues taskValues = new ContentValues();
71-
taskValues.put(TASK_KEY_ID, task.getId());
72-
taskValues.put(TASK_TITLE, task.getTitle());
73-
taskValues.put(TASK_PRIORITY, task.getPriority());
74-
taskValues.put(TASK_ESTIMATE, task.getEstimate());
75-
taskValues.put(TASK_ACTUAL, task.getActual());
76-
taskValues.put(TASK_CREATED_DATE, task.getTimeCreated().getMillis());
77-
taskValues.put(TASK_DONE_DATE, task.getTimeDone().getMillis());
78-
Uri taskUri = ContentUris.withAppendedId(CONTENT_ID_URI_BASE, task.getId());
79-
resolver.update(taskUri, taskValues, null, null);
80-
}
29+
Task findTaskById(long id);
8130

82-
public int deleteTask(long id) {
83-
Uri taskUri = ContentUris.withAppendedId(CONTENT_ID_URI_BASE, id);
84-
return resolver.delete(taskUri, null, null);
85-
}
31+
void saveTask(Task task);
8632

87-
private Task cursorToTask(Cursor cursor) {
88-
long id = cursor.getLong(0);
89-
String title = cursor.getString(1);
90-
int priority = cursor.getShort(2);
91-
int estimate = cursor.getShort(3);
92-
int actual = cursor.getShort(4);
93-
long timeCreated = cursor.getLong(5);
94-
long timeDone = cursor.getLong(6);
95-
return new Task(id, title, priority, estimate, actual, timeCreated, timeDone);
96-
}
33+
int deleteTask(long id);
9734
}

0 commit comments

Comments
 (0)