-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathHomeActivity.java
69 lines (51 loc) · 2.31 KB
/
HomeActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package net.grzechocinski.android.dagger2example.home;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import java.util.List;
import javax.inject.Inject;
import net.grzechocinski.android.dagger2example.D2EApplication;
import net.grzechocinski.android.dagger2example.R;
import net.grzechocinski.android.dagger2example.home.twitter.Twitter;
import net.grzechocinski.android.dagger2example.utils.D2ECollectionUtils;
import net.grzechocinski.android.dagger2example.utils.NetworkStateManager;
public class HomeActivity extends BaseActivity {
private static final String PREF_KEY_SAMPLE = "PREF_KEY_SAMPLE";
@Inject
NetworkStateManager networkStateManager;
@Inject
Twitter twitter;
@InjectView(R.id.d2e_id_tv_pref)
TextView preferenceValueTextView;
@InjectView(R.id.d2e_id_tv_util)
TextView utilOutputTextView;
@InjectView(R.id.d2e_id_tv_network_state)
TextView networkStateTextView;
@InjectView(R.id.d2e_id_btn_load_preference)
Button button;
private D2ECollectionUtils d2EStringUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.inject(this);
//Inject to non-private fields
D2EApplication.component(this).inject(this);
//Inject to private fields or local variables
d2EStringUtils = D2EApplication.component(this).getD2EStringUtils();
//Lambda usage example
button.setOnClickListener(v -> {
sharedPreferences.edit().putString(PREF_KEY_SAMPLE, "foo").apply();
preferenceValueTextView.setText(sharedPreferences.getString(PREF_KEY_SAMPLE, "bar"));
twitter.tweet("Hey, I clicked button!");
});
//Method reference example
List<String> sortedAscending = d2EStringUtils.toSortedList(String::compareTo, "zombie", "dog", "animal");
utilOutputTextView.setText(sortedAscending.get(0));
//Custom class (NetworkStateManager) with nested dependencies provided by constructor
networkStateTextView.setText(getString(R.string.network_text, networkStateManager.isConnectedOrConnecting()));
twitter.tweet("Hello world!");
}
}