22
33import android .os .Bundle ;
44import android .support .v7 .app .AppCompatActivity ;
5+ import android .support .v7 .widget .LinearLayoutManager ;
6+ import android .support .v7 .widget .RecyclerView ;
57import android .support .v7 .widget .Toolbar ;
68import android .view .KeyEvent ;
79import android .view .Menu ;
1113import android .widget .TextView ;
1214import android .widget .Toast ;
1315
16+ import java .util .ArrayList ;
1417import java .util .List ;
1518
1619import io .reactivex .observers .DisposableSingleObserver ;
@@ -19,11 +22,12 @@ public class MainActivity extends AppCompatActivity implements CredentialsDialog
1922
2023 EditText ownerEditText ;
2124 EditText repositoryEditText ;
22- TextView issuesTextView ;
25+ RecyclerView list ;
26+ ArrayList <Issue > issues ;
2327 private String password = "" ;
2428 private String username = "" ;
2529 private CommunicationController communicationController ;
26-
30+ DisposableSingleObserver < List < Issue >> issuesObserver ;
2731 @ Override
2832 protected void onCreate (Bundle savedInstanceState ) {
2933 super .onCreate (savedInstanceState );
@@ -33,6 +37,11 @@ protected void onCreate(Bundle savedInstanceState) {
3337
3438 ownerEditText = (EditText ) findViewById (R .id .owner_edittext );
3539 repositoryEditText = (EditText ) findViewById (R .id .repository_edittext );
40+ if (BuildConfig .DEBUG ){
41+ ownerEditText .setText (BuildConfig .GITHUB_REPO_OWNER );
42+ repositoryEditText .setText (BuildConfig .GITHUB_REPO );
43+ }
44+
3645 repositoryEditText .setOnEditorActionListener (new TextView .OnEditorActionListener () {
3746 @ Override
3847 public boolean onEditorAction (TextView v , int actionId , KeyEvent event ) {
@@ -43,7 +52,10 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
4352 return false ;
4453 }
4554 });
46- issuesTextView = (TextView ) findViewById (R .id .issues_text );
55+ list = (RecyclerView ) findViewById (R .id .list );
56+ list .setLayoutManager (new LinearLayoutManager (this ));
57+ issues = new ArrayList <>();
58+ list .setAdapter (new IssueAdapter (issues ));
4759
4860 communicationController = new CommunicationController ();
4961 }
@@ -71,9 +83,10 @@ private DisposableSingleObserver<List<Issue>> getIssuesObserver() {
7183 return new DisposableSingleObserver <List <Issue >>() {
7284 @ Override
7385 public void onSuccess (List <Issue > issues ) {
74- for (Issue issue : issues ) {
75- issuesTextView .append (issue .toString () + "\n " );
76- }
86+ IssueAdapter listAdapter = (IssueAdapter ) list .getAdapter ();
87+ MainActivity .this .issues .clear ();
88+ MainActivity .this .issues .addAll (new ArrayList <Issue >(issues ));
89+ list .getAdapter ().notifyDataSetChanged ();
7790 }
7891
7992 @ Override
@@ -86,6 +99,10 @@ public void onError(Throwable e) {
8699 private void showCredentialsDialog () {
87100 CredentialsDialog dialog = new CredentialsDialog ();
88101 Bundle arguments = new Bundle ();
102+ if (BuildConfig .DEBUG && BuildConfig .GITHUB_USER .length () > 0 ){
103+ username = BuildConfig .GITHUB_USER ;
104+ password = BuildConfig .GITHUB_PW ;
105+ }
89106 arguments .putString ("username" , username );
90107 arguments .putString ("password" , password );
91108 dialog .setArguments (arguments );
@@ -100,13 +117,23 @@ public void onDialogPositiveClick(String username, String password) {
100117 }
101118
102119 private void queryForIssues () {
103- issuesTextView .setText ("" );
104120 String owner = ownerEditText .getText ().toString ();
105121 String repository = repositoryEditText .getText ().toString ();
106-
122+
107123 if (username .isEmpty () || password .isEmpty ()) {
108124 Toast .makeText (MainActivity .this , "Please provide your credentials" , Toast .LENGTH_SHORT ).show ();
109125 return ;
126+ if (!username .isEmpty () && !password .isEmpty () && !owner .isEmpty () && !repository .isEmpty ()) {
127+ issuesObserver = getIssuesObserver ();
128+ communicationController .loadIssues (username , password , issuesObserver , owner , repository );
129+ }
130+ }
131+
132+ @ Override
133+ protected void onStop () {
134+ super .onStop ();
135+ if (issuesObserver !=null && !issuesObserver .isDisposed ()) {
136+ issuesObserver .dispose ();
110137 }
111138
112139 if (owner .isEmpty () || repository .isEmpty ()) {
0 commit comments