improve dependency injection (#723)

This commit is contained in:
Konrad Pozniak 2018-07-23 21:59:10 +02:00 committed by GitHub
parent 61f3f6c928
commit 7f648b1acd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 27 deletions

View file

@ -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;

View file

@ -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<Activity> dispatchingAndroidInjector;
@Inject
@ -54,33 +52,36 @@ public class TuskyApplication extends Application implements HasActivityInjector
DispatchingAndroidInjector<BroadcastReceiver> 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> T get(Class<T> 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);
}

View file

@ -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)
}
}

View file

@ -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();

View file

@ -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("[]");