Skip to content

Commit

Permalink
Introduce multiple dagger components and modules
Browse files Browse the repository at this point in the history
This started off as a quick way to adding dagger to
:dribbble but then got a little out of hand due to dependencies
into :core and :app, especially within DataManager.

Also there are still issues with application wide singletons.
  • Loading branch information
keyboardsurfer authored and florina-muntenescu committed Oct 11, 2018
1 parent f7ab649 commit 7513172
Show file tree
Hide file tree
Showing 46 changed files with 937 additions and 206 deletions.
2 changes: 1 addition & 1 deletion about/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<dist:fusing dist:include="true" />
</dist:module>

<application>
<application android:name="io.plaidapp.ui.PlaidApplication">
<activity
android:name="io.plaidapp.about.ui.AboutActivity"
android:theme="@style/Plaid.Translucent.About"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ import dagger.Module
import dagger.Provides
import io.plaidapp.about.ui.AboutActivity
import io.plaidapp.about.ui.AboutStyler
import javax.inject.Singleton
import io.plaidapp.core.dagger.scope.ModuleScope

/**
* Dagger module providing stuff from [AboutActivity].
* Dagger module providing stuff for [AboutActivity].
*/
@Module class AboutActivityModule(private val activity: AboutActivity) {

@Provides
@Singleton
@ModuleScope
fun provideContext(): AboutActivity = activity

@Provides
@Singleton
@ModuleScope
fun provideResources(): Resources = activity.resources

@Provides
@Singleton
@ModuleScope
fun provideAboutStyler() = AboutStyler(activity)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import dagger.BindsInstance
import dagger.Component
import io.plaidapp.about.ui.AboutActivity
import io.plaidapp.core.dagger.MarkdownModule
import javax.inject.Singleton
import io.plaidapp.core.dagger.scope.ModuleScope

/**
* Dagger component for `about` feature module.
*/
@Singleton
@ModuleScope
@Component(modules = [AboutActivityModule::class, MarkdownModule::class])
interface AboutComponent {

Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ dependencies {
androidTestImplementation "com.android.support.test:rules:${versions.test_rules}"
androidTestImplementation "com.android.support.test.espresso:espresso-contrib:${versions.espresso}"
androidTestImplementation "com.android.support.test.uiautomator:uiautomator-v18:${versions.ui_automator}"
kapt "com.google.dagger:dagger-compiler:${versions.dagger}"
}

// Must be applied after dependencies. See https://stackoverflow.com/a/38018985
Expand Down
54 changes: 54 additions & 0 deletions app/src/main/java/io/plaidapp/dagger/HomeComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.dagger

import android.content.Context
import dagger.BindsInstance
import dagger.Component
import io.plaidapp.core.dagger.CoroutinesContextProviderModule
import io.plaidapp.core.dagger.DataManagerModule
import io.plaidapp.core.dagger.DribbbleDataModule
import io.plaidapp.core.dagger.DribbbleRetrofitModule
import io.plaidapp.core.dagger.FilterAdapterModule
import io.plaidapp.core.dagger.OnDataLoadedModule
import io.plaidapp.core.dagger.DribbleSearchServiceProvider
import io.plaidapp.core.dagger.ShotsRepositoryModule
import io.plaidapp.ui.HomeActivity

@Component(modules = [HomeModule::class, ShotsRepositoryModule::class])
interface HomeComponent {

fun inject(activity: HomeActivity)

@Component.Builder
interface Builder {
fun build(): HomeComponent

@BindsInstance
fun context(context: Context): Builder

fun coroutinesContextProviderModule(module: CoroutinesContextProviderModule): Builder
fun dataLoadedModule(module: OnDataLoadedModule): Builder
fun dataManagerModule(module: DataManagerModule): Builder
fun dribbbleDataModule(module: DribbbleDataModule): Builder
fun retrofitModule(module: DribbbleRetrofitModule): Builder
fun homeModule(module: HomeModule): Builder
fun filterAdapterModule(module: FilterAdapterModule): Builder
fun searchRemoteDataSourceModule(module: DribbleSearchServiceProvider): Builder
fun shotsRepositoryModule(module: ShotsRepositoryModule): Builder
}
}
55 changes: 55 additions & 0 deletions app/src/main/java/io/plaidapp/dagger/HomeModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.dagger

import android.app.Activity
import android.content.Context
import com.bumptech.glide.util.ViewPreloadSizeProvider
import dagger.Module
import dagger.Provides
import io.plaidapp.R
import io.plaidapp.core.dagger.CoroutinesContextProviderModule
import io.plaidapp.core.dagger.DataManagerModule
import io.plaidapp.core.dagger.DribbbleDataModule
import io.plaidapp.core.dagger.FilterAdapterModule
import io.plaidapp.core.dagger.OnDataLoadedModule
import io.plaidapp.core.dagger.ShotsRepositoryModule
import io.plaidapp.core.data.pocket.PocketUtils
import io.plaidapp.core.dribbble.data.api.model.Shot

@Module(
includes = [
CoroutinesContextProviderModule::class,
DataManagerModule::class,
DribbbleDataModule::class,
FilterAdapterModule::class,
ShotsRepositoryModule::class,
OnDataLoadedModule::class
]
)
class HomeModule(private val activity: Activity) {

@Provides fun context(): Context = activity

@Provides fun activity(): Activity = activity

@Provides fun columns(): Int = activity.resources.getInteger(R.integer.num_columns)

@Provides fun viewPreloadSizeProvider() = ViewPreloadSizeProvider<Shot>()

@Provides fun isPocketInstalled() = PocketUtils.isPocketInstalled(activity)
}
57 changes: 57 additions & 0 deletions app/src/main/java/io/plaidapp/dagger/Injector.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.dagger

import io.plaidapp.core.dagger.CoroutinesContextProviderModule
import io.plaidapp.core.dagger.DataManagerModule
import io.plaidapp.core.dagger.DribbbleDataModule
import io.plaidapp.core.dagger.DribbbleRetrofitModule
import io.plaidapp.core.dagger.FilterAdapterModule
import io.plaidapp.core.dagger.OnDataLoadedModule
import io.plaidapp.core.dagger.DribbleSearchServiceProvider
import io.plaidapp.core.dagger.ShotsRepositoryModule
import io.plaidapp.core.data.BaseDataManager
import io.plaidapp.core.data.PlaidItem
import io.plaidapp.ui.HomeActivity

/**
* Injector for HomeActivity.
*
* TODO: Convert to extension function once [HomeActivity] is converted to Kotlin.
*/
object Injector {

@JvmStatic
fun inject(
activity: HomeActivity,
dataLoadedCallback: BaseDataManager.OnDataLoadedCallback<List<PlaidItem>>
) {
DaggerHomeComponent.builder()
.context(activity)
.coroutinesContextProviderModule(CoroutinesContextProviderModule())
.dataLoadedModule(OnDataLoadedModule(dataLoadedCallback))
.dataManagerModule(DataManagerModule(activity))
.dribbbleDataModule(DribbbleDataModule())
.homeModule(HomeModule(activity))
.filterAdapterModule(FilterAdapterModule(activity))
.retrofitModule(DribbbleRetrofitModule())
.searchRemoteDataSourceModule(DribbleSearchServiceProvider())
.shotsRepositoryModule(ShotsRepositoryModule())
.build()
.inject(activity)
}
}
81 changes: 29 additions & 52 deletions app/src/main/java/io/plaidapp/ui/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.*;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
Expand All @@ -42,61 +38,37 @@
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.Annotation;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.*;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.transition.TransitionManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import android.view.*;
import android.widget.*;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import com.bumptech.glide.util.ViewPreloadSizeProvider;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.plaidapp.R;
import io.plaidapp.core.data.DataManager;
import io.plaidapp.core.data.PlaidItem;
import io.plaidapp.core.data.Source;
import io.plaidapp.core.designernews.Injection;
import io.plaidapp.core.designernews.data.login.LoginRepository;
import io.plaidapp.core.dribbble.data.api.model.Shot;
import io.plaidapp.core.data.pocket.PocketUtils;
import io.plaidapp.core.data.prefs.SourceManager;
import io.plaidapp.core.designernews.data.poststory.PostStoryService;
import io.plaidapp.core.designernews.data.stories.model.Story;
import io.plaidapp.core.dribbble.data.api.model.Shot;
import io.plaidapp.core.ui.FeedAdapter;
import io.plaidapp.core.ui.FilterAdapter;
import io.plaidapp.core.ui.HomeGridItemAnimator;
import io.plaidapp.core.ui.recyclerview.InfiniteScrollListener;
import io.plaidapp.core.util.Activities;
import io.plaidapp.core.util.ActivityHelper;
import io.plaidapp.core.util.AnimUtils;
import io.plaidapp.core.util.DrawableUtils;
import io.plaidapp.core.util.ShortcutHelper;
import io.plaidapp.core.util.ViewUtils;
import io.plaidapp.core.ui.transitions.FabTransform;
import io.plaidapp.core.util.*;
import io.plaidapp.ui.recyclerview.FilterTouchHelperCallback;
import io.plaidapp.ui.recyclerview.GridItemDividerDecoration;
import io.plaidapp.core.ui.transitions.FabTransform;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static io.plaidapp.dagger.Injector.inject;

public class HomeActivity extends Activity {

Expand All @@ -110,7 +82,8 @@ public class HomeActivity extends Activity {
private ImageButton fab;
private RecyclerView filtersList;
private ProgressBar loading;
private @Nullable ImageView noConnection;
private @Nullable
ImageView noConnection;
ImageButton fabPosting;
GridLayoutManager layoutManager;
private int columns;
Expand All @@ -119,8 +92,11 @@ public class HomeActivity extends Activity {
private boolean monitoringConnectivity = false;

// data
@Inject
DataManager dataManager;
@Inject
FeedAdapter adapter;
@Inject
FilterAdapter filtersAdapter;
private LoginRepository loginRepository;

Expand All @@ -129,6 +105,10 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
bindResources();
inject(this, data -> {
adapter.addAndResort(data);
checkEmptyState();
});

drawer.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
Expand All @@ -141,16 +121,13 @@ protected void onCreate(Bundle savedInstanceState) {
setExitSharedElementCallback(FeedAdapter.createSharedElementReenterCallback(this));

loginRepository = Injection.provideLoginRepository(this);
filtersAdapter = new FilterAdapter(this, SourceManager.getSources(this));
dataManager = new DataManager(this, filtersAdapter) {
@Override
public void onDataLoaded(List<? extends PlaidItem> data) {
adapter.addAndResort(data);
checkEmptyState();
}
};
// filtersAdapter = new FilterAdapter(this, SourceManager.getSources(this));
// dataManager = new DataManager(this, data -> {
// adapter.addAndResort(data);
// checkEmptyState();
// });
ViewPreloadSizeProvider<Shot> shotPreloadSizeProvider = new ViewPreloadSizeProvider<>();
adapter = new FeedAdapter(this, dataManager, columns, PocketUtils.isPocketInstalled(this), shotPreloadSizeProvider);
// adapter = new FeedAdapter(this, dataManager, columns, PocketUtils.isPocketInstalled(this), shotPreloadSizeProvider);

grid.setAdapter(adapter);
layoutManager = new GridLayoutManager(this, columns);
Expand Down
Loading

0 comments on commit 7513172

Please sign in to comment.