Skip to content

Commit ab297a2

Browse files
committed
dagger support
1 parent a1928ab commit ab297a2

22 files changed

+430
-194
lines changed

.idea/dictionaries/Chichaykin.xml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ dependencies {
4343
apt "com.github.Raizlabs.DBFlow:dbflow-processor:${DB_FLOW_VER}"
4444
compile "com.github.Raizlabs.DBFlow:dbflow-core:${DB_FLOW_VER}"
4545
compile "com.github.Raizlabs.DBFlow:dbflow:${DB_FLOW_VER}"
46-
compile 'javax.annotation:jsr250-api:1.0'
46+
//compile 'javax.annotation:jsr250-api:1.0'
47+
48+
apt 'com.google.dagger:dagger-compiler:2.5'
49+
compile 'com.google.dagger:dagger:2.5'
50+
provided 'javax.annotation:jsr250-api:1.0'
4751

4852
compile 'com.google.code.gson:gson:2.4'
4953
compile "com.squareup.retrofit:retrofit:${RETROFIT_VER}"

app/proguard-rules.pro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@
2020
-keep class retrofit2.** { *; }
2121
-keepattributes Signature
2222
-keepattributes Exceptions
23+
-keepattributes *Annotation*
24+
-keepclassmembers class * extends uk.co.ribot.easyadapter.ItemViewHolder {
25+
public <init>(...);
26+
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
android:allowBackup="true"
1515
android:icon="@mipmap/ic_launcher"
1616
android:label="@string/app_name"
17-
android:supportsRtl="true"
17+
android:supportsRtl="false"
1818
android:theme="@style/AppTheme"
1919
android:name=".App"
2020
tools:ignore="GoogleAppIndexingWarning">

app/src/main/java/com/mich/weather/App.java

Lines changed: 12 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import android.location.LocationManager;
99

1010
import com.mich.weather.data.WeatherResponse;
11+
import com.mich.weather.di.components.ActivityComponent;
12+
import com.mich.weather.di.modules.AppModule;
13+
import com.mich.weather.di.modules.WeatherModule;
1114
import com.mich.weather.repositories.WeatherLocationPojo;
1215
import com.mich.weather.services.api.location.LocationService;
13-
import com.mich.weather.services.api.weather.WeatherService;
1416
import com.mich.weather.services.api.weather.WeatherServiceApi;
1517
import com.mich.weather.utils.L;
1618
import com.raizlabs.android.dbflow.config.FlowManager;
@@ -26,95 +28,27 @@
2628
import rx.functions.Func1;
2729

2830
public class App extends Application {
31+
private static final String BASE_URL = "http://api.openweathermap.org";
32+
private static final long CACHE_SIZE = 1024 * 1024 * 10;
2933

30-
public static String CURRENT_LOCATION_NAME;
31-
public static Context sContext;
32-
private static App sInstance;
33-
private SharedPreferences mPreferences;
34-
private WeatherServiceApi mWeatherService;
35-
private LocationService mLocationService;
3634

37-
public static App getInstance() {
38-
return sInstance;
39-
}
35+
private ActivityComponent mActivityComponent;
4036

4137
@Override
4238
public void onCreate() {
4339
super.onCreate();
4440
FlowManager.init(this);
45-
mPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
46-
47-
sInstance = this;
48-
sContext = getApplicationContext();
49-
mWeatherService = WeatherService.apiService();
50-
CURRENT_LOCATION_NAME = sContext.getResources().getString(R.string.Current);
51-
final LocationManager locationManager = (LocationManager) sContext
52-
.getSystemService(Context.LOCATION_SERVICE);
53-
mLocationService = new LocationService(locationManager);
54-
}
55-
56-
private List<WeatherLocationPojo> createDB() {
57-
List<WeatherLocationPojo> list = new ArrayList<>();
58-
Resources resources = sContext.getResources();
59-
//current
60-
WeatherLocationPojo current = new WeatherLocationPojo(CURRENT_LOCATION_NAME, null);
61-
current.insert();
62-
list.add(current);
63-
64-
for(String city : resources.getStringArray(R.array.default_cities)) {
65-
current = new WeatherLocationPojo(city, null);
66-
current.insert();
67-
list.add(current);
68-
}
69-
return list;
70-
}
7141

72-
public SharedPreferences getPreferences() {
73-
return mPreferences;
74-
}
75-
76-
public Observable<List<WeatherLocationPojo>> getStoredLocations() {
77-
return Observable.defer(new Func0<Observable<List<WeatherLocationPojo>>>() {
78-
@Override
79-
public Observable<List<WeatherLocationPojo>> call() {
80-
List<WeatherLocationPojo> list = new Select().from(WeatherLocationPojo.class).queryList();
81-
L.d("Stored locations: %s", list.toString());
82-
if (list.isEmpty()) {
83-
list = createDB();
84-
}
85-
return Observable.just(list);
86-
}
87-
});
88-
}
42+
mAppComponent = DaggerAppComponent.builder()
43+
.applicationModule(new AppModule(this))
44+
.build();
45+
mAppComponent.inject(this);
8946

90-
private WeatherServiceApi getWeatherService() {
91-
return mWeatherService;
92-
}
9347

94-
private Observable<WeatherResponse> getCurrentLocation(LocationService locationService) {
95-
return locationService.getLocation()
96-
.flatMap(new Func1<Location, Observable<WeatherResponse>>() {
97-
@Override
98-
public Observable<WeatherResponse> call(Location location) {
99-
return getWeatherService().getCurrentWeather(
100-
location.getLongitude(), location.getLatitude());
101-
}
102-
});
10348
}
10449

105-
public Observable<WeatherResponse> getCurrentLocationObservable(boolean resetCachedLocation) {
106-
if (resetCachedLocation) {
107-
mLocationService.resetLocation();
108-
}
109-
return getCurrentLocation(mLocationService);
110-
}
11150

112-
public Observable<WeatherResponse> geCityObservable(WeatherLocationPojo location) {
113-
StringBuilder builder = new StringBuilder(location.city);
114-
if (!StringUtils.isBlank(location.country)) {
115-
builder.append(",").append(location.country);
116-
}
117-
return getWeatherService().getCityWeather(builder.toString());
51+
public ActivityComponent getActivityComponent() {
52+
return mActivityComponent;
11853
}
119-
12054
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.mich.weather.di.components;
2+
3+
4+
import com.mich.weather.di.modules.ActivityModule;
5+
import com.mich.weather.di.scopes.UserScope;
6+
import com.mich.weather.ui.MainActivity;
7+
8+
import dagger.Component;
9+
10+
@UserScope
11+
@Component(dependencies = AppComponent.class, modules = ActivityModule.class)
12+
public interface ActivityComponent {
13+
void inject(MainActivity activity);
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.mich.weather.di.components;
2+
3+
4+
import android.content.SharedPreferences;
5+
import android.location.LocationManager;
6+
7+
import com.mich.weather.di.modules.AppModule;
8+
import com.mich.weather.di.modules.WeatherModule;
9+
import com.mich.weather.services.api.weather.WeatherServiceApi;
10+
11+
import javax.inject.Singleton;
12+
13+
import dagger.Component;
14+
15+
@Singleton
16+
@Component(modules={AppModule.class, WeatherModule.class})
17+
public interface AppComponent {
18+
19+
WeatherServiceApi WeatherApi();
20+
SharedPreferences sharedPreferences();
21+
LocationManager locationManager();
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.mich.weather.di.modules;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
6+
import com.mich.weather.di.scopes.ActivityContext;
7+
8+
import dagger.Module;
9+
import dagger.Provides;
10+
11+
@Module
12+
public class ActivityModule {
13+
private Activity mActivity;
14+
15+
public ActivityModule(Activity activity) {
16+
mActivity = activity;
17+
}
18+
19+
@Provides
20+
Activity provideActivity() {
21+
return mActivity;
22+
}
23+
24+
@Provides
25+
@ActivityContext
26+
Context providesContext() {
27+
return mActivity;
28+
}
29+
30+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.mich.weather.di.modules;
2+
3+
import android.app.Application;
4+
import android.content.Context;
5+
import android.content.SharedPreferences;
6+
import android.content.res.Resources;
7+
import android.location.Location;
8+
import android.location.LocationManager;
9+
10+
import com.mich.weather.R;
11+
import com.mich.weather.data.WeatherResponse;
12+
import com.mich.weather.repositories.WeatherLocationPojo;
13+
import com.mich.weather.services.api.location.LocationService;
14+
import com.mich.weather.services.api.weather.WeatherServiceApi;
15+
import com.mich.weather.utils.L;
16+
import com.raizlabs.android.dbflow.sql.language.Select;
17+
18+
import org.apache.commons.lang3.StringUtils;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import javax.inject.Singleton;
24+
25+
import dagger.Module;
26+
import dagger.Provides;
27+
import rx.Observable;
28+
import rx.functions.Func0;
29+
import rx.functions.Func1;
30+
31+
@Module
32+
public class AppModule {
33+
34+
private Application mApplication;
35+
private LocationService mLocationService;
36+
37+
public AppModule(Application application) {
38+
mApplication = application;
39+
}
40+
41+
@Provides
42+
@Singleton
43+
Application providesApplication() {
44+
return mApplication;
45+
}
46+
47+
@Provides
48+
@Singleton
49+
Context provideContext() {
50+
return mApplication;
51+
}
52+
53+
@Provides
54+
@Singleton
55+
public SharedPreferences getPreferences() {
56+
return mApplication.getSharedPreferences("config", Context.MODE_PRIVATE);
57+
}
58+
59+
private List<WeatherLocationPojo> createDB() {
60+
List<WeatherLocationPojo> list = new ArrayList<>();
61+
Resources resources = mApplication.getResources();
62+
//current
63+
WeatherLocationPojo current = new WeatherLocationPojo(CURRENT_LOCATION_NAME, null);
64+
current.insert();
65+
list.add(current);
66+
67+
for(String city : resources.getStringArray(R.array.default_cities)) {
68+
current = new WeatherLocationPojo(city, null);
69+
current.insert();
70+
list.add(current);
71+
}
72+
return list;
73+
}
74+
75+
76+
@Provides
77+
public Observable<List<WeatherLocationPojo>> getStoredLocations() {
78+
return Observable.defer(new Func0<Observable<List<WeatherLocationPojo>>>() {
79+
@Override
80+
public Observable<List<WeatherLocationPojo>> call() {
81+
List<WeatherLocationPojo> list = new Select().from(WeatherLocationPojo.class).queryList();
82+
L.d("Stored locations: %s", list.toString());
83+
if (list.isEmpty()) {
84+
list = createDB();
85+
}
86+
return Observable.just(list);
87+
}
88+
});
89+
}
90+
91+
private Observable<WeatherResponse> getCurrentLocation(LocationService locationService, final WeatherServiceApi api) {
92+
return locationService.getLocation()
93+
.flatMap(new Func1<Location, Observable<WeatherResponse>>() {
94+
@Override
95+
public Observable<WeatherResponse> call(Location location) {
96+
return api.getCurrentWeather(
97+
location.getLongitude(), location.getLatitude());
98+
}
99+
});
100+
}
101+
102+
public Observable<WeatherResponse> getCurrentLocationObservable(boolean resetCachedLocation) {
103+
if (resetCachedLocation) {
104+
mLocationService.resetLocation();
105+
}
106+
return getCurrentLocation(mLocationService, );
107+
}
108+
109+
public Observable<WeatherResponse> geCityObservable(WeatherLocationPojo location) {
110+
StringBuilder builder = new StringBuilder(location.city);
111+
if (!StringUtils.isBlank(location.country)) {
112+
builder.append(",").append(location.country);
113+
}
114+
return getWeatherService().getCityWeather(builder.toString());
115+
}
116+
117+
@Provides
118+
@Singleton
119+
public LocationService locationService() {
120+
CURRENT_LOCATION_NAME = mApplication.getResources().getString(R.string.Current);
121+
final LocationManager locationManager = (LocationManager) mApplication
122+
.getSystemService(Context.LOCATION_SERVICE);
123+
mLocationService = new LocationService(locationManager);
124+
}
125+
}

0 commit comments

Comments
 (0)