improve dependency injection (#723)
This commit is contained in:
parent
61f3f6c928
commit
7f648b1acd
5 changed files with 28 additions and 27 deletions
|
@ -185,7 +185,7 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
|
||||||
tintTab(tab, true);
|
tintTab(tab, true);
|
||||||
|
|
||||||
if (tab.getPosition() == 1) {
|
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
|
// Setup push notifications
|
||||||
if (NotificationHelper.areNotificationsEnabled(this)) {
|
if (NotificationHelper.areNotificationsEnabled(this, accountManager)) {
|
||||||
enablePushNotifications();
|
enablePushNotifications();
|
||||||
} else {
|
} else {
|
||||||
disablePushNotifications();
|
disablePushNotifications();
|
||||||
|
@ -217,7 +217,7 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
NotificationHelper.clearNotificationsForActiveAccount(this);
|
NotificationHelper.clearNotificationsForActiveAccount(this, accountManager);
|
||||||
|
|
||||||
/* After editing a profile, the profile header in the navigation drawer needs to be
|
/* After editing a profile, the profile header in the navigation drawer needs to be
|
||||||
* refreshed */
|
* refreshed */
|
||||||
|
@ -433,7 +433,7 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
|
||||||
|
|
||||||
AccountEntity newAccount = accountManager.logActiveAccountOut();
|
AccountEntity newAccount = accountManager.logActiveAccountOut();
|
||||||
|
|
||||||
if (!NotificationHelper.areNotificationsEnabled(MainActivity.this))
|
if (!NotificationHelper.areNotificationsEnabled(MainActivity.this, accountManager))
|
||||||
disablePushNotifications();
|
disablePushNotifications();
|
||||||
|
|
||||||
Intent intent;
|
Intent intent;
|
||||||
|
|
|
@ -44,8 +44,6 @@ import dagger.android.HasServiceInjector;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
|
||||||
public class TuskyApplication extends Application implements HasActivityInjector, HasServiceInjector, HasBroadcastReceiverInjector {
|
public class TuskyApplication extends Application implements HasActivityInjector, HasServiceInjector, HasBroadcastReceiverInjector {
|
||||||
private static AppDatabase db;
|
|
||||||
private AccountManager accountManager;
|
|
||||||
@Inject
|
@Inject
|
||||||
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
|
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
|
||||||
@Inject
|
@Inject
|
||||||
|
@ -54,33 +52,36 @@ public class TuskyApplication extends Application implements HasActivityInjector
|
||||||
DispatchingAndroidInjector<BroadcastReceiver> dispatchingBroadcastReceiverInjector;
|
DispatchingAndroidInjector<BroadcastReceiver> dispatchingBroadcastReceiverInjector;
|
||||||
@Inject
|
@Inject
|
||||||
NotificationPullJobCreator notificationPullJobCreator;
|
NotificationPullJobCreator notificationPullJobCreator;
|
||||||
@Inject OkHttpClient okHttpClient;
|
@Inject
|
||||||
|
OkHttpClient okHttpClient;
|
||||||
|
|
||||||
public static AppDatabase getDB() {
|
private AppDatabase appDatabase;
|
||||||
return db;
|
private AccountManager accountManager;
|
||||||
}
|
|
||||||
|
private ServiceLocator serviceLocator;
|
||||||
|
|
||||||
public static TuskyApplication getInstance(@NonNull Context context) {
|
public static TuskyApplication getInstance(@NonNull Context context) {
|
||||||
return (TuskyApplication) context.getApplicationContext();
|
return (TuskyApplication) context.getApplicationContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ServiceLocator serviceLocator;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
|
||||||
db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tuskyDB")
|
appDatabase = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tuskyDB")
|
||||||
.allowMainThreadQueries()
|
.allowMainThreadQueries()
|
||||||
.addMigrations(AppDatabase.MIGRATION_2_3, AppDatabase.MIGRATION_3_4, AppDatabase.MIGRATION_4_5, AppDatabase.MIGRATION_5_6, AppDatabase.MIGRATION_6_7)
|
.addMigrations(AppDatabase.MIGRATION_2_3, AppDatabase.MIGRATION_3_4, AppDatabase.MIGRATION_4_5, AppDatabase.MIGRATION_5_6, AppDatabase.MIGRATION_6_7)
|
||||||
.build();
|
.build();
|
||||||
accountManager = new AccountManager(db);
|
accountManager = new AccountManager(appDatabase);
|
||||||
serviceLocator = new ServiceLocator() {
|
serviceLocator = new ServiceLocator() {
|
||||||
@Override
|
@Override
|
||||||
public <T> T get(Class<T> clazz) {
|
public <T> T get(Class<T> clazz) {
|
||||||
if (clazz.equals(AccountManager.class)) {
|
if (clazz.equals(AccountManager.class)) {
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
return (T) accountManager;
|
return (T) accountManager;
|
||||||
|
} else if (clazz.equals(AppDatabase.class)) {
|
||||||
|
//noinspection unchecked
|
||||||
|
return (T) appDatabase;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Unknown service " + clazz);
|
throw new IllegalArgumentException("Unknown service " + clazz);
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,5 +74,7 @@ class AppModule {
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun providesDatabase(app: TuskyApplication): AppDatabase = TuskyApplication.getDB()
|
fun providesDatabase(app: TuskyApplication): AppDatabase {
|
||||||
|
return app.serviceLocator.get(AppDatabase::class.java)
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -70,12 +70,13 @@ public abstract class SFragment extends BaseFragment {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public MastodonApi mastodonApi;
|
public MastodonApi mastodonApi;
|
||||||
|
@Inject
|
||||||
|
public AccountManager accountManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
AccountEntity activeAccount = TuskyApplication.getInstance(getContext()).getServiceLocator()
|
AccountEntity activeAccount = accountManager.getActiveAccount();
|
||||||
.get(AccountManager.class).getActiveAccount();
|
|
||||||
if (activeAccount != null) {
|
if (activeAccount != null) {
|
||||||
loggedInAccountId = activeAccount.getAccountId();
|
loggedInAccountId = activeAccount.getAccountId();
|
||||||
loggedInUsername = activeAccount.getUsername();
|
loggedInUsername = activeAccount.getUsername();
|
||||||
|
|
|
@ -27,6 +27,7 @@ import android.graphics.BitmapFactory;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.support.v4.app.NotificationManagerCompat;
|
import android.support.v4.app.NotificationManagerCompat;
|
||||||
|
@ -39,7 +40,6 @@ import android.util.Log;
|
||||||
import com.keylesspalace.tusky.BuildConfig;
|
import com.keylesspalace.tusky.BuildConfig;
|
||||||
import com.keylesspalace.tusky.MainActivity;
|
import com.keylesspalace.tusky.MainActivity;
|
||||||
import com.keylesspalace.tusky.R;
|
import com.keylesspalace.tusky.R;
|
||||||
import com.keylesspalace.tusky.TuskyApplication;
|
|
||||||
import com.keylesspalace.tusky.db.AccountEntity;
|
import com.keylesspalace.tusky.db.AccountEntity;
|
||||||
import com.keylesspalace.tusky.db.AccountManager;
|
import com.keylesspalace.tusky.db.AccountManager;
|
||||||
import com.keylesspalace.tusky.entity.Notification;
|
import com.keylesspalace.tusky.entity.Notification;
|
||||||
|
@ -316,7 +316,7 @@ public class NotificationHelper {
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
|
||||||
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
|
||||||
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
|
||||||
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
|
||||||
// on Android >= O, notifications are enabled, if at least one channel is enabled
|
// on Android >= O, notifications are enabled, if at least one channel is enabled
|
||||||
|
@ -419,15 +419,12 @@ public class NotificationHelper {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// on Android < O, notifications are enabled, if at least one account has notification enabled
|
// on Android < O, notifications are enabled, if at least one account has notification enabled
|
||||||
return TuskyApplication.getInstance(context).getServiceLocator()
|
return accountManager.areNotificationsEnabled();
|
||||||
.get(AccountManager.class).areNotificationsEnabled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void clearNotificationsForActiveAccount(Context context) {
|
public static void clearNotificationsForActiveAccount(@NonNull Context context, @NonNull AccountManager accountManager) {
|
||||||
AccountManager accountManager = TuskyApplication.getInstance(context).getServiceLocator()
|
|
||||||
.get(AccountManager.class);
|
|
||||||
AccountEntity account = accountManager.getActiveAccount();
|
AccountEntity account = accountManager.getActiveAccount();
|
||||||
if (account != null) {
|
if (account != null) {
|
||||||
account.setActiveNotifications("[]");
|
account.setActiveNotifications("[]");
|
||||||
|
|
Loading…
Reference in a new issue