|
| 1 | +package com.vogella.android.github.issuetracker; |
| 2 | + |
| 3 | +import android.support.v7.app.AppCompatActivity; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.v7.widget.Toolbar; |
| 6 | +import android.view.KeyEvent; |
| 7 | +import android.view.Menu; |
| 8 | +import android.view.MenuItem; |
| 9 | +import android.view.View; |
| 10 | +import android.view.inputmethod.EditorInfo; |
| 11 | +import android.widget.EditText; |
| 12 | +import android.widget.TextView; |
| 13 | +import android.widget.Toast; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import io.reactivex.observers.DisposableSingleObserver; |
| 18 | + |
| 19 | +public class MainActivity extends AppCompatActivity implements CredentialsDialog.ICredentialsDialogListener { |
| 20 | + |
| 21 | + EditText ownerEditText; |
| 22 | + EditText repositoryEditText; |
| 23 | + TextView issuesTextView; |
| 24 | + private String password = ""; |
| 25 | + private String username= ""; |
| 26 | + private CommunicationController communicationController; |
| 27 | + |
| 28 | + @Override |
| 29 | + protected void onCreate(Bundle savedInstanceState) { |
| 30 | + super.onCreate(savedInstanceState); |
| 31 | + setContentView(R.layout.activity_main); |
| 32 | + Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); |
| 33 | + setSupportActionBar(toolbar); |
| 34 | + |
| 35 | + ownerEditText = (EditText) findViewById(R.id.owner_edittext); |
| 36 | + repositoryEditText = (EditText) findViewById(R.id.repository_edittext); |
| 37 | + repositoryEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { |
| 38 | + @Override |
| 39 | + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 40 | + if(actionId == EditorInfo.IME_ACTION_DONE){ |
| 41 | + queryForIssues(); |
| 42 | + return true; |
| 43 | + } |
| 44 | + return false; |
| 45 | + } |
| 46 | + }); |
| 47 | + issuesTextView = (TextView) findViewById(R.id.issues_text); |
| 48 | + |
| 49 | + communicationController = new CommunicationController(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 54 | + getMenuInflater().inflate(R.menu.menu_main, menu); |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 60 | + switch (item.getItemId()) { |
| 61 | + case R.id.menu_credentials: |
| 62 | + showCredentialsDialog(); |
| 63 | + return true; |
| 64 | + case R.id.menu_refresh: |
| 65 | + queryForIssues(); |
| 66 | + return true; |
| 67 | + } |
| 68 | + return super.onOptionsItemSelected(item); |
| 69 | + } |
| 70 | + |
| 71 | + private DisposableSingleObserver<List<Issue>> getIssuesObserver() { |
| 72 | + return new DisposableSingleObserver<List<Issue>>() { |
| 73 | + @Override |
| 74 | + public void onSuccess(List<Issue> value) { |
| 75 | + for (Issue issues : value) { |
| 76 | + issuesTextView.append(issues.toString() + "\n"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onError(Throwable e) { |
| 82 | + Toast.makeText(MainActivity.this, "Cannot load issues.", Toast.LENGTH_SHORT).show(); |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + private void showCredentialsDialog() { |
| 88 | + CredentialsDialog dialog = new CredentialsDialog(); |
| 89 | + Bundle arguments = new Bundle(); |
| 90 | + arguments.putString("username", username); |
| 91 | + arguments.putString("password", password); |
| 92 | + dialog.setArguments(arguments); |
| 93 | + |
| 94 | + dialog.show(getSupportFragmentManager(), "credentialsDialog"); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onDialogPositiveClick(String username, String password) { |
| 99 | + this.username = username; |
| 100 | + this.password = password; |
| 101 | + } |
| 102 | + |
| 103 | + private void queryForIssues() { |
| 104 | + issuesTextView.setText(""); |
| 105 | + String owner = ownerEditText.getText().toString(); |
| 106 | + String repository = repositoryEditText.getText().toString(); |
| 107 | + if(!username.isEmpty() && !password.isEmpty() && !owner.isEmpty() && !repository.isEmpty()) { |
| 108 | + communicationController.loadIssues(username, password, getIssuesObserver(), owner, repository); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments