Skip to content
This repository was archived by the owner on Oct 27, 2024. It is now read-only.

Commit 86225cf

Browse files
author
Pulkit Kumar
committed
optimized di
1 parent 12d3e11 commit 86225cf

File tree

7 files changed

+52
-49
lines changed

7 files changed

+52
-49
lines changed

app/src/main/java/com/esoxjem/movieguide/BaseApplication.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.StrictMode;
66

77
import com.esoxjem.movieguide.di.AppComponent;
8+
import com.esoxjem.movieguide.di.AppModule;
89
import com.esoxjem.movieguide.di.DaggerAppComponent;
910
import com.esoxjem.movieguide.di.DetailsModule;
1011
import com.esoxjem.movieguide.di.FavoritesModule;
@@ -17,7 +18,7 @@
1718
*/
1819
public class BaseApplication extends Application
1920
{
20-
AppComponent component;
21+
private AppComponent component;
2122

2223
@Override
2324
public void onCreate()
@@ -30,6 +31,7 @@ public static AppComponent getAppComponent(Context context) {
3031
BaseApplication app = (BaseApplication) context.getApplicationContext();
3132
if (app.component == null) {
3233
app.component = DaggerAppComponent.builder()
34+
.appModule(app.getAppModule())
3335
.networkModule(app.getNetworkModule())
3436
.detailsModule(app.getDetailsModule())
3537
.favoritesModule(app.getFavoritesModule())
@@ -40,14 +42,19 @@ public static AppComponent getAppComponent(Context context) {
4042
return app.component;
4143
}
4244

45+
private AppModule getAppModule()
46+
{
47+
return new AppModule(this);
48+
}
49+
4350
private DetailsModule getDetailsModule()
4451
{
4552
return new DetailsModule();
4653
}
4754

4855
private FavoritesModule getFavoritesModule()
4956
{
50-
return new FavoritesModule(this);
57+
return new FavoritesModule();
5158
}
5259

5360
private ListingModule getListingModule()
@@ -57,7 +64,7 @@ private ListingModule getListingModule()
5764

5865
private SortingModule getSortingModule()
5966
{
60-
return new SortingModule(this);
67+
return new SortingModule();
6168
}
6269

6370
private NetworkModule getNetworkModule()

app/src/main/java/com/esoxjem/movieguide/di/AppComponent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
@Singleton
1515
@Component(modules = {
16+
AppModule.class,
1617
NetworkModule.class,
1718
DetailsModule.class,
1819
FavoritesModule.class,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.esoxjem.movieguide.di;
2+
3+
import android.app.Application;
4+
import android.content.Context;
5+
6+
import com.esoxjem.movieguide.BaseApplication;
7+
8+
import dagger.Module;
9+
import dagger.Provides;
10+
11+
/**
12+
* Created by pulkitkumar on 17/09/16.
13+
*/
14+
@Module
15+
public class AppModule
16+
{
17+
private Application app;
18+
19+
public AppModule(Application application)
20+
{
21+
app = application;
22+
}
23+
24+
@Provides
25+
public Context provideContext()
26+
{
27+
return app;
28+
}
29+
}
Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package com.esoxjem.movieguide.di;
22

3-
import android.app.Application;
4-
import android.content.Context;
5-
import android.content.SharedPreferences;
6-
73
import com.esoxjem.movieguide.favorites.FavoritesInteractor;
84
import com.esoxjem.movieguide.favorites.FavoritesStore;
95
import com.esoxjem.movieguide.favorites.IFavoritesInteractor;
106

11-
import javax.inject.Named;
127
import javax.inject.Singleton;
138

149
import dagger.Module;
@@ -17,30 +12,13 @@
1712
/**
1813
* Created by pulkitkumar on 17/09/16.
1914
*/
20-
@Module
15+
@Module (includes = AppModule.class)
2116
public class FavoritesModule
2217
{
23-
private static final String PREF_NAME = "FavoritesStore";
24-
public static final String FAVOURITES = "favourites";
25-
private Application app;
26-
27-
public FavoritesModule (Application app)
28-
{
29-
this.app = app;
30-
}
31-
3218
@Provides
3319
@Singleton
3420
IFavoritesInteractor provideFavouritesInteractor(FavoritesStore store)
3521
{
3622
return new FavoritesInteractor(store);
3723
}
38-
39-
@Provides
40-
@Singleton
41-
FavoritesStore provideFavouritesStore()
42-
{
43-
SharedPreferences pref = app.getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
44-
return new FavoritesStore(pref);
45-
}
4624
}

app/src/main/java/com/esoxjem/movieguide/di/SortingModule.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,9 @@
1919
/**
2020
* Created by pulkitkumar on 17/09/16.
2121
*/
22-
@Module
22+
@Module(includes = AppModule.class)
2323
public class SortingModule
2424
{
25-
private Application app;
26-
private static final String PREF_NAME = "SortingOptionStore";
27-
28-
public SortingModule(Application app)
29-
{
30-
this.app = app;
31-
}
32-
33-
@Provides
34-
@Singleton
35-
SortingOptionStore providesSortingOptionStore()
36-
{
37-
SharedPreferences pref = app.getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
38-
return new SortingOptionStore(pref);
39-
}
40-
4125
@Provides
4226
@Singleton
4327
ISortingDialogInteractor providesSortingDialogInteractor(SortingOptionStore store)

app/src/main/java/com/esoxjem/movieguide/favorites/FavoritesStore.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.esoxjem.movieguide.favorites;
22

3+
import android.content.Context;
34
import android.content.SharedPreferences;
45
import android.text.TextUtils;
56

6-
import com.esoxjem.movieguide.BaseApplication;
77
import com.esoxjem.movieguide.entities.Movie;
88
import com.squareup.moshi.JsonAdapter;
99
import com.squareup.moshi.Moshi;
@@ -14,18 +14,21 @@
1414
import java.util.Map;
1515

1616
import javax.inject.Inject;
17-
import javax.inject.Named;
17+
import javax.inject.Singleton;
1818

1919
/**
2020
* @author arun
2121
*/
22+
@Singleton
2223
public class FavoritesStore
2324
{
25+
private static final String PREF_NAME = "FavoritesStore";
2426
private SharedPreferences pref;
2527

26-
public FavoritesStore(SharedPreferences pref)
28+
@Inject
29+
public FavoritesStore(Context context)
2730
{
28-
this.pref = pref;
31+
pref = context.getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
2932
}
3033

3134
public void setFavorite(Movie movie)

app/src/main/java/com/esoxjem/movieguide/sorting/SortingOptionStore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.esoxjem.movieguide.sorting;
22

3+
import android.content.Context;
34
import android.content.SharedPreferences;
45

56
import com.esoxjem.movieguide.BaseApplication;
@@ -17,9 +18,9 @@ public class SortingOptionStore
1718
private static final String SELECTED_OPTION = "selectedOption";
1819
private static final String PREF_NAME = "SortingOptionStore";
1920

20-
public SortingOptionStore(SharedPreferences pref)
21+
@Inject public SortingOptionStore(Context context)
2122
{
22-
this.pref = pref;
23+
pref = context.getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
2324
}
2425

2526
public void setSelectedOption(SortType sortType)

0 commit comments

Comments
 (0)