Pārlūkot izejas kodu

Fix ThemeUtils injection for MediaControlView

Inflation of this view was failing as there was no proper constructor

Signed-off-by: Álvaro Brey Vilas <alvaro.brey@nextcloud.com>
Álvaro Brey Vilas 3 gadi atpakaļ
vecāks
revīzija
b58749d459

+ 3 - 0
app/src/main/java/com/nextcloud/client/di/AppComponent.java

@@ -30,6 +30,7 @@ import com.nextcloud.client.network.NetworkModule;
 import com.nextcloud.client.onboarding.OnboardingModule;
 import com.nextcloud.client.preferences.PreferencesModule;
 import com.owncloud.android.MainApp;
+import com.owncloud.android.media.MediaControlView;
 
 import javax.inject.Singleton;
 
@@ -55,6 +56,8 @@ public interface AppComponent {
 
     void inject(MainApp app);
 
+    void inject(MediaControlView mediaControlView);
+
     @Component.Builder
     interface Builder {
         @BindsInstance

+ 27 - 4
app/src/main/java/com/owncloud/android/MainApp.java

@@ -45,6 +45,7 @@ import com.nextcloud.client.appinfo.AppInfo;
 import com.nextcloud.client.core.Clock;
 import com.nextcloud.client.device.PowerManagementService;
 import com.nextcloud.client.di.ActivityInjector;
+import com.nextcloud.client.di.AppComponent;
 import com.nextcloud.client.di.DaggerAppComponent;
 import com.nextcloud.client.errorhandling.ExceptionHandler;
 import com.nextcloud.client.jobs.BackgroundJobManager;
@@ -178,6 +179,8 @@ public class MainApp extends MultiDexApplication implements HasAndroidInjector {
     @SuppressWarnings("unused")
     private boolean mBound;
 
+    private static AppComponent appComponent;
+
     /**
      * Temporary hack
      */
@@ -226,10 +229,7 @@ public class MainApp extends MultiDexApplication implements HasAndroidInjector {
         super.attachBaseContext(base);
 
         initGlobalContext(this);
-        DaggerAppComponent.builder()
-            .application(this)
-            .build()
-            .inject(this);
+        initDagger();
 
         // we don't want to handle crashes occurring inside crash reporter activity/process;
         // let the platform deal with those
@@ -243,6 +243,29 @@ public class MainApp extends MultiDexApplication implements HasAndroidInjector {
         }
     }
 
+    private void initDagger() {
+        appComponent = DaggerAppComponent.builder()
+            .application(this)
+            .build();
+
+        appComponent.inject(this);
+    }
+
+    /**
+     * <b>USE SPARINGLY!</b> This should only be used for injection of Theme classes in custom Views, and they need to
+     * be added as methods in the {@link AppComponent} itself.
+     * <p>
+     * Once we adopt Hilt this won't be necessary either, as View is a supported target in Hilt.
+     *
+     * @return the {@link AppComponent} for this app
+     */
+    public static AppComponent getAppComponent() {
+        if (appComponent == null) {
+            throw new IllegalStateException("Dagger not initialized!");
+        }
+        return appComponent;
+    }
+
     @SuppressFBWarnings("ST")
     @Override
     public void onCreate() {

+ 11 - 7
app/src/main/java/com/owncloud/android/media/MediaControlView.java

@@ -38,6 +38,7 @@ import android.widget.SeekBar;
 import android.widget.SeekBar.OnSeekBarChangeListener;
 import android.widget.TextView;
 
+import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.utils.theme.ThemeBarUtils;
@@ -46,6 +47,8 @@ import com.owncloud.android.utils.theme.ThemeColorUtils;
 import java.util.Formatter;
 import java.util.Locale;
 
+import javax.inject.Inject;
+
 
 /**
  * View containing controls for a {@link MediaPlayer}.
@@ -67,17 +70,18 @@ public class MediaControlView extends FrameLayout implements OnClickListener, On
     private ImageButton pauseButton;
     private ImageButton forwardButton;
     private ImageButton rewindButton;
-    private final ThemeColorUtils themeColorUtils;
-    private final ThemeBarUtils themeBarUtils;
+
+    @Inject
+    ThemeColorUtils themeColorUtils;
+    @Inject
+    ThemeBarUtils themeBarUtils;
+
 
     public MediaControlView(Context context,
-                            AttributeSet attrs,
-                            ThemeColorUtils themeColorUtils,
-                            ThemeBarUtils themeBarUtils) {
+                            AttributeSet attrs) {
         super(context, attrs);
 
-        this.themeColorUtils = themeColorUtils;
-        this.themeBarUtils = themeBarUtils;
+        MainApp.getAppComponent().inject(this);
 
         FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
             ViewGroup.LayoutParams.MATCH_PARENT,