Update test injection, fix broken tests

pull/820/head
Alex Baker 6 years ago
parent b37e9870f6
commit c77f49de80

@ -24,7 +24,7 @@ import static org.tasks.makers.TaskMaker.REMINDER_LAST;
import static org.tasks.makers.TaskMaker.SNOOZE_TIME; import static org.tasks.makers.TaskMaker.SNOOZE_TIME;
import static org.tasks.makers.TaskMaker.newTask; import static org.tasks.makers.TaskMaker.newTask;
import androidx.test.runner.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.Task;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -62,7 +62,7 @@ public class ReminderServiceTest extends InjectingTestCase {
jobs = mock(NotificationQueue.class); jobs = mock(NotificationQueue.class);
random = mock(Random.class); random = mock(Random.class);
when(random.nextFloat()).thenReturn(1.0f); when(random.nextFloat()).thenReturn(1.0f);
preferences.reset(); preferences.clear();
service = new ReminderService(preferences, jobs, random, taskDao); service = new ReminderService(preferences, jobs, random, taskDao);
} }

@ -163,7 +163,7 @@ public class TitleParserTest extends InjectingTestCase {
} }
private Task insertTitleAddTask(String title) { private Task insertTitleAddTask(String title) {
return taskCreator.createWithValues(null, title); return taskCreator.createWithValues(title);
} }
// ----------------Days begin----------------// // ----------------Days begin----------------//
@ -172,13 +172,13 @@ public class TitleParserTest extends InjectingTestCase {
Calendar today = Calendar.getInstance(); Calendar today = Calendar.getInstance();
String title = "Jog today"; String title = "Jog today";
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
DateTime date = newDateTime(task.getDueDate()); DateTime date = newDateTime(task.getDueDate());
assertEquals(date.getDayOfWeek(), today.get(Calendar.DAY_OF_WEEK)); assertEquals(date.getDayOfWeek(), today.get(Calendar.DAY_OF_WEEK));
// Calendar starts 1-6, date.getDay() starts at 0 // Calendar starts 1-6, date.getDay() starts at 0
title = "Jog tomorrow"; title = "Jog tomorrow";
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
date = newDateTime(task.getDueDate()); date = newDateTime(task.getDueDate());
assertEquals((date.getDayOfWeek()) % 7, (today.get(Calendar.DAY_OF_WEEK) + 1) % 7); assertEquals((date.getDayOfWeek()) % 7, (today.get(Calendar.DAY_OF_WEEK) + 1) % 7);
@ -189,12 +189,12 @@ public class TitleParserTest extends InjectingTestCase {
for (int i = 1; i <= 6; i++) { for (int i = 1; i <= 6; i++) {
title = "Jog " + days[i]; title = "Jog " + days[i];
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
date = newDateTime(task.getDueDate()); date = newDateTime(task.getDueDate());
assertEquals(date.getDayOfWeek(), i + 1); assertEquals(date.getDayOfWeek(), i + 1);
title = "Jog " + abrevDays[i]; title = "Jog " + abrevDays[i];
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
date = newDateTime(task.getDueDate()); date = newDateTime(task.getDueDate());
assertEquals(date.getDayOfWeek(), i + 1); assertEquals(date.getDayOfWeek(), i + 1);
} }
@ -210,12 +210,12 @@ public class TitleParserTest extends InjectingTestCase {
String[] acceptedStrings = {"priority 0", "least priority", "lowest priority", "bang 0"}; String[] acceptedStrings = {"priority 0", "least priority", "lowest priority", "bang 0"};
for (String acceptedString : acceptedStrings) { for (String acceptedString : acceptedStrings) {
String title = "Jog " + acceptedString; String title = "Jog " + acceptedString;
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.NONE); assertEquals((int) task.getPriority(), Priority.NONE);
} }
for (String acceptedString : acceptedStrings) { for (String acceptedString : acceptedStrings) {
String title = acceptedString + " jog"; String title = acceptedString + " jog";
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
assertNotSame(task.getPriority(), Priority.NONE); assertNotSame(task.getPriority(), Priority.NONE);
} }
} }
@ -257,20 +257,20 @@ public class TitleParserTest extends InjectingTestCase {
String[] acceptedStringsAnywhere = {"!2", "!!"}; String[] acceptedStringsAnywhere = {"!2", "!!"};
for (String acceptedStringAtEnd : acceptedStringsAtEnd) { for (String acceptedStringAtEnd : acceptedStringsAtEnd) {
String title = "Jog " + acceptedStringAtEnd; String title = "Jog " + acceptedStringAtEnd;
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.MEDIUM); assertEquals((int) task.getPriority(), Priority.MEDIUM);
title = acceptedStringAtEnd + " jog"; title = acceptedStringAtEnd + " jog";
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertNotSame(task.getPriority(), Priority.MEDIUM); assertNotSame(task.getPriority(), Priority.MEDIUM);
} }
for (String acceptedStringAnywhere : acceptedStringsAnywhere) { for (String acceptedStringAnywhere : acceptedStringsAnywhere) {
String title = "Jog " + acceptedStringAnywhere; String title = "Jog " + acceptedStringAnywhere;
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.MEDIUM); assertEquals((int) task.getPriority(), Priority.MEDIUM);
title = acceptedStringAnywhere + " jog"; title = acceptedStringAnywhere + " jog";
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.MEDIUM); assertEquals((int) task.getPriority(), Priority.MEDIUM);
} }
} }
@ -287,20 +287,20 @@ public class TitleParserTest extends InjectingTestCase {
String[] acceptedStringsAnywhere = {"!3", "!!!", "!6", "!!!!!!!!!!!!!"}; String[] acceptedStringsAnywhere = {"!3", "!!!", "!6", "!!!!!!!!!!!!!"};
for (String acceptedStringAtEnd : acceptedStringsAtEnd) { for (String acceptedStringAtEnd : acceptedStringsAtEnd) {
String title = "Jog " + acceptedStringAtEnd; String title = "Jog " + acceptedStringAtEnd;
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.HIGH); assertEquals((int) task.getPriority(), Priority.HIGH);
title = acceptedStringAtEnd + " jog"; title = acceptedStringAtEnd + " jog";
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertNotSame(task.getPriority(), Priority.HIGH); assertNotSame(task.getPriority(), Priority.HIGH);
} }
for (String acceptedStringAnywhere : acceptedStringsAnywhere) { for (String acceptedStringAnywhere : acceptedStringsAnywhere) {
String title = "Jog " + acceptedStringAnywhere; String title = "Jog " + acceptedStringAnywhere;
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.HIGH); assertEquals((int) task.getPriority(), Priority.HIGH);
title = acceptedStringAnywhere + " jog"; title = acceptedStringAnywhere + " jog";
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals((int) task.getPriority(), Priority.HIGH); assertEquals((int) task.getPriority(), Priority.HIGH);
} }
} }
@ -313,7 +313,7 @@ public class TitleParserTest extends InjectingTestCase {
@Test @Test
public void testDailyWithNoDueDate() { public void testDailyWithNoDueDate() {
String title = "Jog daily"; String title = "Jog daily";
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
RRule rrule = new RRule(); RRule rrule = new RRule();
rrule.setFreq(Frequency.DAILY); rrule.setFreq(Frequency.DAILY);
rrule.setInterval(1); rrule.setInterval(1);
@ -322,7 +322,7 @@ public class TitleParserTest extends InjectingTestCase {
assertFalse(task.hasDueDate()); assertFalse(task.hasDueDate());
title = "Jog every day"; title = "Jog every day";
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal()); assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime()); assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate()); assertFalse(task.hasDueDate());
@ -330,7 +330,7 @@ public class TitleParserTest extends InjectingTestCase {
for (int i = 1; i <= 12; i++) { for (int i = 1; i <= 12; i++) {
title = "Jog every " + i + " days."; title = "Jog every " + i + " days.";
rrule.setInterval(i); rrule.setInterval(i);
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal()); assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime()); assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate()); assertFalse(task.hasDueDate());
@ -341,7 +341,7 @@ public class TitleParserTest extends InjectingTestCase {
@Test @Test
public void testWeeklyWithNoDueDate() { public void testWeeklyWithNoDueDate() {
String title = "Jog weekly"; String title = "Jog weekly";
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
RRule rrule = new RRule(); RRule rrule = new RRule();
rrule.setFreq(Frequency.WEEKLY); rrule.setFreq(Frequency.WEEKLY);
rrule.setInterval(1); rrule.setInterval(1);
@ -350,7 +350,7 @@ public class TitleParserTest extends InjectingTestCase {
assertFalse(task.hasDueDate()); assertFalse(task.hasDueDate());
title = "Jog every week"; title = "Jog every week";
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal()); assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime()); assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate()); assertFalse(task.hasDueDate());
@ -358,7 +358,7 @@ public class TitleParserTest extends InjectingTestCase {
for (int i = 1; i <= 12; i++) { for (int i = 1; i <= 12; i++) {
title = "Jog every " + i + " weeks"; title = "Jog every " + i + " weeks";
rrule.setInterval(i); rrule.setInterval(i);
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal()); assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime()); assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate()); assertFalse(task.hasDueDate());
@ -369,7 +369,7 @@ public class TitleParserTest extends InjectingTestCase {
@Test @Test
public void testMonthlyFromNoDueDate() { public void testMonthlyFromNoDueDate() {
String title = "Jog monthly"; String title = "Jog monthly";
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
RRule rrule = new RRule(); RRule rrule = new RRule();
rrule.setFreq(Frequency.MONTHLY); rrule.setFreq(Frequency.MONTHLY);
rrule.setInterval(1); rrule.setInterval(1);
@ -378,7 +378,7 @@ public class TitleParserTest extends InjectingTestCase {
assertFalse(task.hasDueDate()); assertFalse(task.hasDueDate());
title = "Jog every month"; title = "Jog every month";
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal()); assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime()); assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate()); assertFalse(task.hasDueDate());
@ -386,7 +386,7 @@ public class TitleParserTest extends InjectingTestCase {
for (int i = 1; i <= 12; i++) { for (int i = 1; i <= 12; i++) {
title = "Jog every " + i + " months"; title = "Jog every " + i + " months";
rrule.setInterval(i); rrule.setInterval(i);
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal()); assertEquals(task.getRecurrence(), rrule.toIcal());
assertFalse(task.hasDueTime()); assertFalse(task.hasDueTime());
assertFalse(task.hasDueDate()); assertFalse(task.hasDueDate());
@ -396,7 +396,7 @@ public class TitleParserTest extends InjectingTestCase {
@Test @Test
public void testDailyFromDueDate() { public void testDailyFromDueDate() {
String title = "Jog daily starting from today"; String title = "Jog daily starting from today";
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
RRule rrule = new RRule(); RRule rrule = new RRule();
rrule.setFreq(Frequency.DAILY); rrule.setFreq(Frequency.DAILY);
rrule.setInterval(1); rrule.setInterval(1);
@ -404,14 +404,14 @@ public class TitleParserTest extends InjectingTestCase {
assertTrue(task.hasDueDate()); assertTrue(task.hasDueDate());
title = "Jog every day starting from today"; title = "Jog every day starting from today";
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal()); assertEquals(task.getRecurrence(), rrule.toIcal());
assertTrue(task.hasDueDate()); assertTrue(task.hasDueDate());
for (int i = 1; i <= 12; i++) { for (int i = 1; i <= 12; i++) {
title = "Jog every " + i + " days starting from today"; title = "Jog every " + i + " days starting from today";
rrule.setInterval(i); rrule.setInterval(i);
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal()); assertEquals(task.getRecurrence(), rrule.toIcal());
assertTrue(task.hasDueDate()); assertTrue(task.hasDueDate());
} }
@ -420,7 +420,7 @@ public class TitleParserTest extends InjectingTestCase {
@Test @Test
public void testWeeklyFromDueDate() { public void testWeeklyFromDueDate() {
String title = "Jog weekly starting from today"; String title = "Jog weekly starting from today";
Task task = taskCreator.createWithValues(null, title); Task task = taskCreator.createWithValues(title);
RRule rrule = new RRule(); RRule rrule = new RRule();
rrule.setFreq(Frequency.WEEKLY); rrule.setFreq(Frequency.WEEKLY);
rrule.setInterval(1); rrule.setInterval(1);
@ -428,14 +428,14 @@ public class TitleParserTest extends InjectingTestCase {
assertTrue(task.hasDueDate()); assertTrue(task.hasDueDate());
title = "Jog every week starting from today"; title = "Jog every week starting from today";
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal()); assertEquals(task.getRecurrence(), rrule.toIcal());
assertTrue(task.hasDueDate()); assertTrue(task.hasDueDate());
for (int i = 1; i <= 12; i++) { for (int i = 1; i <= 12; i++) {
title = "Jog every " + i + " weeks starting from today"; title = "Jog every " + i + " weeks starting from today";
rrule.setInterval(i); rrule.setInterval(i);
task = taskCreator.createWithValues(null, title); task = taskCreator.createWithValues(title);
assertEquals(task.getRecurrence(), rrule.toIcal()); assertEquals(task.getRecurrence(), rrule.toIcal());
assertTrue(task.hasDueDate()); assertTrue(task.hasDueDate());
} }

@ -1,8 +1,9 @@
package org.tasks.injection; package org.tasks.injection;
import static androidx.test.InstrumentationRegistry.getTargetContext; import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static org.tasks.TestUtilities.initializeMockito; import static org.tasks.TestUtilities.initializeMockito;
import android.content.Context;
import org.junit.Before; import org.junit.Before;
import timber.log.Timber; import timber.log.Timber;
@ -12,10 +13,14 @@ public abstract class InjectingTestCase {
public void setUp() { public void setUp() {
Thread.setDefaultUncaughtExceptionHandler((t, e) -> Timber.e(e)); Thread.setDefaultUncaughtExceptionHandler((t, e) -> Timber.e(e));
initializeMockito(getTargetContext()); Context context = getApplicationContext();
initializeMockito(context);
TestComponent component = TestComponent component =
DaggerTestComponent.builder().testModule(new TestModule(getTargetContext())).build(); DaggerTestComponent.builder()
.applicationModule(new ApplicationModule(context))
.testModule(new TestModule()).build();
inject(component); inject(component);
} }

@ -3,124 +3,34 @@ package org.tasks.injection;
import android.content.Context; import android.content.Context;
import androidx.room.Room; import androidx.room.Room;
import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.TaskDao;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import org.tasks.data.AlarmDao;
import org.tasks.data.CaldavDao;
import org.tasks.data.DeletionDao;
import org.tasks.data.FilterDao;
import org.tasks.data.GoogleTaskDao;
import org.tasks.data.GoogleTaskListDao;
import org.tasks.data.LocationDao;
import org.tasks.data.TagDao;
import org.tasks.data.TagDataDao;
import org.tasks.data.TaskAttachmentDao;
import org.tasks.data.TaskListMetadataDao;
import org.tasks.data.UserActivityDao;
import org.tasks.jobs.WorkManager;
import org.tasks.notifications.NotificationDao;
import org.tasks.preferences.PermissionChecker; import org.tasks.preferences.PermissionChecker;
import org.tasks.preferences.PermissivePermissionChecker; import org.tasks.preferences.PermissivePermissionChecker;
import org.tasks.preferences.Preferences;
@Module @Module(includes = ApplicationModule.class)
public class TestModule { public class TestModule {
private final Context context; public static Preferences newPreferences(Context context) {
return new Preferences(context, "test_preferences");
TestModule(Context context) {
this.context = context;
} }
@Provides @Provides
@ApplicationScope @ApplicationScope
public Database getDatabase() { public Database getDatabase(@ForApplication Context context) {
return Room.inMemoryDatabaseBuilder(context, Database.class) return Room.inMemoryDatabaseBuilder(context, Database.class)
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.build(); .build();
} }
@Provides @Provides
public NotificationDao getNotificationDao(Database appDatabase) { public PermissionChecker getPermissionChecker(@ForApplication Context context) {
return appDatabase.notificationDao(); return new PermissivePermissionChecker(context);
}
@Provides
public TagDataDao getTagDataDao(Database database) {
return database.getTagDataDao();
}
@Provides
public UserActivityDao getUserActivityDao(Database database) {
return database.getUserActivityDao();
}
@Provides
public TaskListMetadataDao getTaskListMetadataDao(Database database) {
return database.getTaskListMetadataDao();
}
@Provides
public GoogleTaskListDao getGoogleTaskListDao(Database database) {
return database.getGoogleTaskListDao();
}
@Provides
public AlarmDao getAlarmDao(Database database) {
return database.getAlarmDao();
}
@Provides
public GoogleTaskDao getGoogleTaskDao(Database database) {
return database.getGoogleTaskDao();
}
@Provides
public TagDao getTagDao(Database database) {
return database.getTagDao();
}
@Provides
public LocationDao getLocationDao(Database database) {
return database.getLocationDao();
}
@Provides
public TaskDao getTaskDao(WorkManager workManager, Database database) {
TaskDao taskDao = database.getTaskDao();
taskDao.initialize(workManager);
return taskDao;
}
@Provides
public CaldavDao getCaldavDao(Database database) {
return database.getCaldavDao();
}
@Provides
public FilterDao getFilterDao(Database database) {
return database.getFilterDao();
}
@Provides
public DeletionDao getDeletionDao(Database database) {
return database.getDeletionDao();
}
@Provides
public TaskAttachmentDao getTaskAttachmentDao(Database database) {
return database.getTaskAttachmentDao();
}
@ApplicationScope
@Provides
@ForApplication
public Context getContext() {
return context;
} }
@Provides @Provides
public PermissionChecker getPermissionChecker() { public Preferences getPreferences(@ForApplication Context context) {
return new PermissivePermissionChecker(context); return newPreferences(context);
} }
} }

@ -1,7 +1,8 @@
package org.tasks.preferences; package org.tasks.preferences;
import static androidx.test.InstrumentationRegistry.getTargetContext; import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static org.tasks.injection.TestModule.newPreferences;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
@ -22,7 +23,7 @@ public class PreferenceTests {
@Before @Before
public void setUp() { public void setUp() {
preferences = new Preferences(getTargetContext()); preferences = newPreferences(getApplicationContext());
preferences.clear(); preferences.clear();
preferences.setBoolean(R.string.p_rmd_enable_quiet, true); preferences.setBoolean(R.string.p_rmd_enable_quiet, true);
} }

@ -21,7 +21,7 @@ import org.tasks.jobs.BackupServiceTests;
@ApplicationScope @ApplicationScope
@Component(modules = TestModule.class) @Component(modules = TestModule.class)
public interface TestComponent { public interface TestComponent extends ApplicationComponent {
void inject(GtasksIndentActionTest gtasksIndentActionTest); void inject(GtasksIndentActionTest gtasksIndentActionTest);

@ -6,7 +6,7 @@ import org.tasks.dashclock.DashClockExtension;
import org.tasks.widget.ScrollableWidgetUpdateService; import org.tasks.widget.ScrollableWidgetUpdateService;
@ApplicationScope @ApplicationScope
@Component(modules = ApplicationModule.class) @Component(modules = {ApplicationModule.class, ProductionModule.class})
public interface ApplicationComponent { public interface ApplicationComponent {
void inject(DashClockExtension dashClockExtension); void inject(DashClockExtension dashClockExtension);

@ -3,7 +3,6 @@ package org.tasks.injection;
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastMarshmallow; import static com.todoroo.andlib.utility.AndroidUtilities.atLeastMarshmallow;
import android.content.Context; import android.content.Context;
import androidx.room.Room;
import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao;
import dagger.Module; import dagger.Module;
@ -24,7 +23,6 @@ import org.tasks.data.TagDataDao;
import org.tasks.data.TaskAttachmentDao; import org.tasks.data.TaskAttachmentDao;
import org.tasks.data.TaskListMetadataDao; import org.tasks.data.TaskListMetadataDao;
import org.tasks.data.UserActivityDao; import org.tasks.data.UserActivityDao;
import org.tasks.db.Migrations;
import org.tasks.jobs.WorkManager; import org.tasks.jobs.WorkManager;
import org.tasks.locale.Locale; import org.tasks.locale.Locale;
import org.tasks.notifications.NotificationDao; import org.tasks.notifications.NotificationDao;
@ -52,15 +50,6 @@ public class ApplicationModule {
return context; return context;
} }
@Provides
@ApplicationScope
public Database getAppDatabase() {
return Room.databaseBuilder(context, Database.class, Database.NAME)
.allowMainThreadQueries() // TODO: remove me
.addMigrations(Migrations.MIGRATIONS)
.build();
}
@Provides @Provides
@ApplicationScope @ApplicationScope
public NotificationDao getNotificationDao(Database database) { public NotificationDao getNotificationDao(Database database) {

@ -4,7 +4,7 @@ import com.todoroo.astrid.provider.Astrid2TaskProvider;
import dagger.Component; import dagger.Component;
@ApplicationScope @ApplicationScope
@Component(modules = {ApplicationModule.class, ContentProviderModule.class}) @Component(modules = ContentProviderModule.class)
public interface ContentProviderComponent { public interface ContentProviderComponent {
void inject(Astrid2TaskProvider astrid2TaskProvider); void inject(Astrid2TaskProvider astrid2TaskProvider);

@ -2,5 +2,5 @@ package org.tasks.injection;
import dagger.Module; import dagger.Module;
@Module @Module(includes = ProductionModule.class)
public class ContentProviderModule {} public class ContentProviderModule {}

@ -22,6 +22,7 @@ class Dagger {
applicationComponent = applicationComponent =
DaggerApplicationComponent.builder() DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(localeContext)) .applicationModule(new ApplicationModule(localeContext))
.productionModule(new ProductionModule())
.build(); .build();
} }

@ -11,6 +11,7 @@ public abstract class InjectingContentProvider extends ContentProvider {
inject( inject(
DaggerContentProviderComponent.builder() DaggerContentProviderComponent.builder()
.applicationModule(new ApplicationModule(context.getApplicationContext())) .applicationModule(new ApplicationModule(context.getApplicationContext()))
.productionModule(new ProductionModule())
.contentProviderModule(new ContentProviderModule()) .contentProviderModule(new ContentProviderModule())
.build()); .build());

@ -0,0 +1,26 @@
package org.tasks.injection;
import android.content.Context;
import androidx.room.Room;
import com.todoroo.astrid.dao.Database;
import dagger.Module;
import dagger.Provides;
import org.tasks.db.Migrations;
import org.tasks.preferences.Preferences;
@Module(includes = ApplicationModule.class)
public class ProductionModule {
@Provides
@ApplicationScope
public Database getAppDatabase(@ForApplication Context context) {
return Room.databaseBuilder(context, Database.class, Database.NAME)
.allowMainThreadQueries() // TODO: remove me
.addMigrations(Migrations.MIGRATIONS)
.build();
}
@Provides
public Preferences getPreferences(@ForApplication Context context) {
return new Preferences(context);
}
}

@ -28,11 +28,9 @@ import com.todoroo.astrid.data.Task;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import org.tasks.R; import org.tasks.R;
import org.tasks.billing.Purchase; import org.tasks.billing.Purchase;
import org.tasks.data.TaskAttachment; import org.tasks.data.TaskAttachment;
import org.tasks.injection.ForApplication;
import org.tasks.time.DateTime; import org.tasks.time.DateTime;
import timber.log.Timber; import timber.log.Timber;
@ -46,14 +44,21 @@ public class Preferences {
private final SharedPreferences prefs; private final SharedPreferences prefs;
private final SharedPreferences publicPrefs; private final SharedPreferences publicPrefs;
@Inject public Preferences(Context context) {
public Preferences(@ForApplication Context context) { this(context, getSharedPreferencesName(context));
}
public Preferences(Context context, String name) {
this.context = context; this.context = context;
prefs = PreferenceManager.getDefaultSharedPreferences(context); prefs = context.getSharedPreferences(name, Context.MODE_PRIVATE);
publicPrefs = publicPrefs =
context.getSharedPreferences(AstridApiConstants.PUBLIC_PREFS, Context.MODE_PRIVATE); context.getSharedPreferences(AstridApiConstants.PUBLIC_PREFS, Context.MODE_PRIVATE);
} }
private static String getSharedPreferencesName(Context context) {
return context.getPackageName() + "_preferences";
}
public boolean backButtonSavesTask() { public boolean backButtonSavesTask() {
return getBoolean(R.string.p_back_button_saves_task, false); return getBoolean(R.string.p_back_button_saves_task, false);
} }

Loading…
Cancel
Save