From 7f648b1acdc532b5b9d33bf83005c769eafe9fd3 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Mon, 23 Jul 2018 21:59:10 +0200 Subject: [PATCH] improve dependency injection (#723) --- .../com/keylesspalace/tusky/MainActivity.java | 8 +++---- .../keylesspalace/tusky/TuskyApplication.java | 21 ++++++++++--------- .../com/keylesspalace/tusky/di/AppModule.kt | 4 +++- .../tusky/fragment/SFragment.java | 5 +++-- .../tusky/util/NotificationHelper.java | 17 +++++++-------- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index a60db7001..673fac02a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -185,7 +185,7 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut tintTab(tab, true); if (tab.getPosition() == 1) { - NotificationHelper.clearNotificationsForActiveAccount(MainActivity.this); + NotificationHelper.clearNotificationsForActiveAccount(MainActivity.this, accountManager); } } @@ -204,7 +204,7 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut } // Setup push notifications - if (NotificationHelper.areNotificationsEnabled(this)) { + if (NotificationHelper.areNotificationsEnabled(this, accountManager)) { enablePushNotifications(); } else { disablePushNotifications(); @@ -217,7 +217,7 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut protected void onResume() { super.onResume(); - NotificationHelper.clearNotificationsForActiveAccount(this); + NotificationHelper.clearNotificationsForActiveAccount(this, accountManager); /* After editing a profile, the profile header in the navigation drawer needs to be * refreshed */ @@ -433,7 +433,7 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut AccountEntity newAccount = accountManager.logActiveAccountOut(); - if (!NotificationHelper.areNotificationsEnabled(MainActivity.this)) + if (!NotificationHelper.areNotificationsEnabled(MainActivity.this, accountManager)) disablePushNotifications(); Intent intent; diff --git a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java index 11628516d..bb840da7c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java +++ b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java @@ -44,8 +44,6 @@ import dagger.android.HasServiceInjector; import okhttp3.OkHttpClient; public class TuskyApplication extends Application implements HasActivityInjector, HasServiceInjector, HasBroadcastReceiverInjector { - private static AppDatabase db; - private AccountManager accountManager; @Inject DispatchingAndroidInjector dispatchingAndroidInjector; @Inject @@ -54,33 +52,36 @@ public class TuskyApplication extends Application implements HasActivityInjector DispatchingAndroidInjector dispatchingBroadcastReceiverInjector; @Inject NotificationPullJobCreator notificationPullJobCreator; - @Inject OkHttpClient okHttpClient; + @Inject + OkHttpClient okHttpClient; - public static AppDatabase getDB() { - return db; - } + private AppDatabase appDatabase; + private AccountManager accountManager; + + private ServiceLocator serviceLocator; public static TuskyApplication getInstance(@NonNull Context context) { return (TuskyApplication) context.getApplicationContext(); } - private ServiceLocator serviceLocator; - @Override public void onCreate() { super.onCreate(); - db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tuskyDB") + appDatabase = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tuskyDB") .allowMainThreadQueries() .addMigrations(AppDatabase.MIGRATION_2_3, AppDatabase.MIGRATION_3_4, AppDatabase.MIGRATION_4_5, AppDatabase.MIGRATION_5_6, AppDatabase.MIGRATION_6_7) .build(); - accountManager = new AccountManager(db); + accountManager = new AccountManager(appDatabase); serviceLocator = new ServiceLocator() { @Override public T get(Class clazz) { if (clazz.equals(AccountManager.class)) { //noinspection unchecked return (T) accountManager; + } else if (clazz.equals(AppDatabase.class)) { + //noinspection unchecked + return (T) appDatabase; } else { throw new IllegalArgumentException("Unknown service " + clazz); } diff --git a/app/src/main/java/com/keylesspalace/tusky/di/AppModule.kt b/app/src/main/java/com/keylesspalace/tusky/di/AppModule.kt index a6a6660ae..4690e0928 100644 --- a/app/src/main/java/com/keylesspalace/tusky/di/AppModule.kt +++ b/app/src/main/java/com/keylesspalace/tusky/di/AppModule.kt @@ -74,5 +74,7 @@ class AppModule { @Provides @Singleton - fun providesDatabase(app: TuskyApplication): AppDatabase = TuskyApplication.getDB() + fun providesDatabase(app: TuskyApplication): AppDatabase { + return app.serviceLocator.get(AppDatabase::class.java) + } } \ No newline at end of file diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java index 3d6069350..e1a285e0e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java @@ -70,12 +70,13 @@ public abstract class SFragment extends BaseFragment { @Inject public MastodonApi mastodonApi; + @Inject + public AccountManager accountManager; @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - AccountEntity activeAccount = TuskyApplication.getInstance(getContext()).getServiceLocator() - .get(AccountManager.class).getActiveAccount(); + AccountEntity activeAccount = accountManager.getActiveAccount(); if (activeAccount != null) { loggedInAccountId = activeAccount.getAccountId(); loggedInUsername = activeAccount.getUsername(); diff --git a/app/src/main/java/com/keylesspalace/tusky/util/NotificationHelper.java b/app/src/main/java/com/keylesspalace/tusky/util/NotificationHelper.java index 4875eedda..7bedce377 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/NotificationHelper.java +++ b/app/src/main/java/com/keylesspalace/tusky/util/NotificationHelper.java @@ -27,6 +27,7 @@ import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Build; import android.provider.Settings; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; @@ -39,7 +40,6 @@ import android.util.Log; import com.keylesspalace.tusky.BuildConfig; import com.keylesspalace.tusky.MainActivity; import com.keylesspalace.tusky.R; -import com.keylesspalace.tusky.TuskyApplication; import com.keylesspalace.tusky.db.AccountEntity; import com.keylesspalace.tusky.db.AccountManager; import com.keylesspalace.tusky.entity.Notification; @@ -316,7 +316,7 @@ public class NotificationHelper { PendingIntent.FLAG_UPDATE_CURRENT); } - public static void createNotificationChannelsForAccount(AccountEntity account, Context context) { + public static void createNotificationChannelsForAccount(@NonNull AccountEntity account, @NonNull Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); @@ -368,7 +368,7 @@ public class NotificationHelper { } } - public static void deleteNotificationChannelsForAccount(AccountEntity account, Context context) { + public static void deleteNotificationChannelsForAccount(@NonNull AccountEntity account, @NonNull Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); @@ -379,7 +379,7 @@ public class NotificationHelper { } } - public static void deleteLegacyNotificationChannels(Context context, AccountManager accountManager) { + public static void deleteLegacyNotificationChannels(@NonNull Context context, @NonNull AccountManager accountManager) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); @@ -398,7 +398,7 @@ public class NotificationHelper { } } - public static boolean areNotificationsEnabled(Context context) { + public static boolean areNotificationsEnabled(@NonNull Context context, @NonNull AccountManager accountManager) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // on Android >= O, notifications are enabled, if at least one channel is enabled @@ -419,15 +419,12 @@ public class NotificationHelper { } else { // on Android < O, notifications are enabled, if at least one account has notification enabled - return TuskyApplication.getInstance(context).getServiceLocator() - .get(AccountManager.class).areNotificationsEnabled(); + return accountManager.areNotificationsEnabled(); } } - public static void clearNotificationsForActiveAccount(Context context) { - AccountManager accountManager = TuskyApplication.getInstance(context).getServiceLocator() - .get(AccountManager.class); + public static void clearNotificationsForActiveAccount(@NonNull Context context, @NonNull AccountManager accountManager) { AccountEntity account = accountManager.getActiveAccount(); if (account != null) { account.setActiveNotifications("[]");