From 5f390b378bf2911d0ef1311dd9b907774213ebbd Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Mon, 11 Mar 2019 17:25:11 +0100 Subject: [PATCH] Improve Dependency Injection (#1120) * inject TimelineCases directly into SFragment * inject ThemeUtils --- .../com/keylesspalace/tusky/BaseActivity.java | 4 ++-- .../keylesspalace/tusky/PreferencesActivity.kt | 2 +- .../conversation/ConversationsFragment.kt | 7 ------- .../tusky/fragment/NotificationsFragment.java | 8 -------- .../keylesspalace/tusky/fragment/SFragment.java | 17 +++++++++-------- .../tusky/fragment/SearchFragment.kt | 12 +----------- .../tusky/fragment/TimelineFragment.java | 8 -------- .../tusky/fragment/ViewThreadFragment.java | 8 -------- .../keylesspalace/tusky/util/ThemeUtils.java | 8 ++++++++ 9 files changed, 21 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index 1b0175485..8f2a75e37 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -54,11 +54,11 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab protected List callList; + @Inject + public ThemeUtils themeUtils; @Inject public AccountManager accountManager; - ThemeUtils themeUtils = new ThemeUtils(); - protected static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1; @Override diff --git a/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.kt b/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.kt index 5d7cdf4f8..96d7c7587 100644 --- a/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.kt @@ -124,7 +124,7 @@ class PreferencesActivity : BaseActivity(), SharedPreferences.OnSharedPreference "appTheme" -> { val theme = sharedPreferences.getNonNullString("appTheme", ThemeUtils.APP_THEME_DEFAULT) Log.d("activeTheme", theme) - ThemeUtils().setAppNightMode(theme, this) + themeUtils.setAppNightMode(theme, this) restartActivitiesOnExit = true this.restartCurrentActivity() diff --git a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsFragment.kt index 2cda3a96b..5dd6739c6 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/conversation/ConversationsFragment.kt @@ -35,7 +35,6 @@ import com.keylesspalace.tusky.di.Injectable import com.keylesspalace.tusky.di.ViewModelFactory import com.keylesspalace.tusky.fragment.SFragment import com.keylesspalace.tusky.interfaces.StatusActionListener -import com.keylesspalace.tusky.network.TimelineCases import com.keylesspalace.tusky.util.NetworkState import com.keylesspalace.tusky.util.ThemeUtils import com.keylesspalace.tusky.util.hide @@ -44,8 +43,6 @@ import javax.inject.Inject class ConversationsFragment : SFragment(), StatusActionListener, Injectable { - @Inject - lateinit var timelineCases: TimelineCases @Inject lateinit var viewModelFactory: ViewModelFactory @Inject @@ -165,10 +162,6 @@ class ConversationsFragment : SFragment(), StatusActionListener, Injectable { startActivity(intent) } - override fun timelineCases(): TimelineCases { - return timelineCases - } - override fun removeItem(position: Int) { viewModel.remove(position) } diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java index 68a98cd45..6401928ff 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java @@ -43,7 +43,6 @@ import com.keylesspalace.tusky.entity.Notification; import com.keylesspalace.tusky.entity.Status; import com.keylesspalace.tusky.interfaces.ActionButtonActivity; import com.keylesspalace.tusky.interfaces.StatusActionListener; -import com.keylesspalace.tusky.network.TimelineCases; import com.keylesspalace.tusky.util.CollectionUtil; import com.keylesspalace.tusky.util.Either; import com.keylesspalace.tusky.util.HttpHeaderLink; @@ -122,8 +121,6 @@ public class NotificationsFragment extends SFragment implements } } - @Inject - public TimelineCases timelineCases; @Inject AccountManager accountManager; @Inject @@ -144,11 +141,6 @@ public class NotificationsFragment extends SFragment implements private String bottomId; private boolean alwaysShowSensitiveMedia; - @Override - protected TimelineCases timelineCases() { - return timelineCases; - } - // Each element is either a Notification for loading data or a Placeholder private final PairedList, NotificationViewData> notifications = new PairedList<>(new Function, NotificationViewData>() { 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 8f8d05139..88d613c5a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java @@ -35,6 +35,7 @@ import com.keylesspalace.tusky.ViewMediaActivity; import com.keylesspalace.tusky.ViewTagActivity; import com.keylesspalace.tusky.db.AccountEntity; import com.keylesspalace.tusky.db.AccountManager; +import com.keylesspalace.tusky.di.Injectable; import com.keylesspalace.tusky.entity.Attachment; import com.keylesspalace.tusky.entity.Status; import com.keylesspalace.tusky.network.MastodonApi; @@ -61,9 +62,7 @@ import androidx.core.view.ViewCompat; * adapters. I feel like the profile pages and thread viewer, which I haven't made yet, will also * overlap functionality. So, I'm momentarily leaving it and hopefully working on those will clear * up what needs to be where. */ -public abstract class SFragment extends BaseFragment { - - protected abstract TimelineCases timelineCases(); +public abstract class SFragment extends BaseFragment implements Injectable { protected abstract void removeItem(int position); @@ -75,6 +74,8 @@ public abstract class SFragment extends BaseFragment { public MastodonApi mastodonApi; @Inject public AccountManager accountManager; + @Inject + public TimelineCases timelineCases; @Override public void startActivity(Intent intent) { @@ -83,7 +84,7 @@ public abstract class SFragment extends BaseFragment { } @Override - public void onAttach(Context context) { + public void onAttach(@NonNull Context context) { super.onAttach(context); if (context instanceof BottomSheetActivity) { bottomSheetActivity = (BottomSheetActivity) context; @@ -236,11 +237,11 @@ public abstract class SFragment extends BaseFragment { return true; } case R.id.status_mute: { - timelineCases().mute(accountId); + timelineCases.mute(accountId); return true; } case R.id.status_block: { - timelineCases().block(accountId); + timelineCases.block(accountId); return true; } case R.id.status_report: { @@ -260,7 +261,7 @@ public abstract class SFragment extends BaseFragment { return true; } case R.id.pin: { - timelineCases().pin(status, !status.isPinned()); + timelineCases.pin(status, !status.isPinned()); return true; } } @@ -321,7 +322,7 @@ public abstract class SFragment extends BaseFragment { new AlertDialog.Builder(getActivity()) .setMessage(R.string.dialog_delete_toot_warning) .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> { - timelineCases().delete(id); + timelineCases.delete(id); removeItem(position); }) .setNegativeButton(android.R.string.cancel, null) diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/SearchFragment.kt b/app/src/main/java/com/keylesspalace/tusky/fragment/SearchFragment.kt index 9f772b6c4..a2ad598e5 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/SearchFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/SearchFragment.kt @@ -29,10 +29,8 @@ import com.keylesspalace.tusky.AccountActivity import com.keylesspalace.tusky.R import com.keylesspalace.tusky.ViewTagActivity import com.keylesspalace.tusky.adapter.SearchResultsAdapter -import com.keylesspalace.tusky.di.Injectable import com.keylesspalace.tusky.entity.SearchResults import com.keylesspalace.tusky.interfaces.StatusActionListener -import com.keylesspalace.tusky.network.TimelineCases import com.keylesspalace.tusky.util.ViewDataUtils import com.keylesspalace.tusky.viewdata.StatusViewData import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider.from @@ -42,12 +40,8 @@ import kotlinx.android.synthetic.main.fragment_search.* import retrofit2.Call import retrofit2.Callback import retrofit2.Response -import javax.inject.Inject -class SearchFragment : SFragment(), StatusActionListener, Injectable { - - @Inject - lateinit var timelineCases: TimelineCases +class SearchFragment : SFragment(), StatusActionListener { private lateinit var searchAdapter: SearchResultsAdapter @@ -129,10 +123,6 @@ class SearchFragment : SFragment(), StatusActionListener, Injectable { } } - override fun timelineCases(): TimelineCases { - return timelineCases - } - override fun removeItem(position: Int) { searchAdapter.removeStatusAtPosition(position) } diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java index 7ab89c7c7..89171f916 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/TimelineFragment.java @@ -47,7 +47,6 @@ import com.keylesspalace.tusky.entity.Status; import com.keylesspalace.tusky.interfaces.ActionButtonActivity; import com.keylesspalace.tusky.interfaces.StatusActionListener; import com.keylesspalace.tusky.network.MastodonApi; -import com.keylesspalace.tusky.network.TimelineCases; import com.keylesspalace.tusky.repository.Placeholder; import com.keylesspalace.tusky.repository.TimelineRepository; import com.keylesspalace.tusky.repository.TimelineRequestMode; @@ -126,8 +125,6 @@ public class TimelineFragment extends SFragment implements MIDDLE } - @Inject - public TimelineCases timelineCases; @Inject public EventHub eventHub; @Inject @@ -160,11 +157,6 @@ public class TimelineFragment extends SFragment implements private boolean alwaysShowSensitiveMedia; private boolean initialUpdateFailed = false; - @Override - protected TimelineCases timelineCases() { - return timelineCases; - } - private PairedList, StatusViewData> statuses = new PairedList<>(new Function, StatusViewData>() { @Override diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java index 360549ea5..313a11ae5 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java @@ -46,7 +46,6 @@ import com.keylesspalace.tusky.entity.Status; import com.keylesspalace.tusky.entity.StatusContext; import com.keylesspalace.tusky.interfaces.StatusActionListener; import com.keylesspalace.tusky.network.MastodonApi; -import com.keylesspalace.tusky.network.TimelineCases; import com.keylesspalace.tusky.util.ListStatusAccessibilityDelegate; import com.keylesspalace.tusky.util.PairedList; import com.keylesspalace.tusky.util.SmartLengthInputFilter; @@ -83,8 +82,6 @@ public final class ViewThreadFragment extends SFragment implements SwipeRefreshLayout.OnRefreshListener, StatusActionListener, Injectable { private static final String TAG = "ViewThreadFragment"; - @Inject - public TimelineCases timelineCases; @Inject public MastodonApi mastodonApi; @Inject @@ -118,11 +115,6 @@ public final class ViewThreadFragment extends SFragment implements return fragment; } - @Override - protected TimelineCases timelineCases() { - return timelineCases; - } - @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.java b/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.java index c079bb27d..2831e2813 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.java +++ b/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.java @@ -29,11 +29,19 @@ import androidx.appcompat.app.AppCompatDelegate; import android.provider.Settings; import android.util.TypedValue; +import javax.inject.Inject; +import javax.inject.Singleton; + /** * Provides runtime compatibility to obtain theme information and re-theme views, especially where * the ability to do so is not supported in resource files. */ +@Singleton public class ThemeUtils { + + @Inject + public ThemeUtils(){} + public static final String APP_THEME_DEFAULT = ThemeUtils.THEME_NIGHT; private static final String THEME_NIGHT = "night";