Last active
April 28, 2017 11:25
-
-
Save vogella/c6ff9733e18714197096cea96551b92e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vogella.android.github.issuetracker; | |
import java.util.List; | |
import io.reactivex.android.schedulers.AndroidSchedulers; | |
import io.reactivex.observers.DisposableSingleObserver; | |
import io.reactivex.schedulers.Schedulers; | |
import okhttp3.Credentials; | |
import okhttp3.OkHttpClient; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
import retrofit2.Retrofit; | |
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
public class CommunicationController { | |
private GithubApi githubApi; | |
public CommunicationController() { | |
initGithubApi(); | |
} | |
private void initGithubApi() { | |
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); | |
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | |
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build(); | |
Retrofit retrofit = new Retrofit.Builder() | |
.client(okHttpClient) | |
.baseUrl(GithubApi.BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | |
.build(); | |
githubApi = retrofit.create(GithubApi.class); | |
} | |
public void loadIssues(String username, String password, DisposableSingleObserver<List<Issue>> observer, String owner, String repository) { | |
githubApi.getIssues(Credentials.basic(username, password), owner, repository) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(observer); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vogella.android.github.issuetracker; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.KeyEvent; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.inputmethod.EditorInfo; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity implements CredentialsDialog.ICredentialsDialogListener { | |
EditText ownerEditText; | |
EditText repositoryEditText; | |
TextView issuesTextView; | |
private String password = ""; | |
private String username = ""; | |
private CommunicationController communicationController; | |
DisposableSingleObserver<List<Issue>> issuesObserver; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); | |
setSupportActionBar(toolbar); | |
ownerEditText = (EditText) findViewById(R.id.owner_edittext); | |
repositoryEditText = (EditText) findViewById(R.id.repository_edittext); | |
repositoryEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { | |
@Override | |
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { | |
if (actionId == EditorInfo.IME_ACTION_DONE) { | |
queryForIssues(); | |
return true; | |
} | |
return false; | |
} | |
}); | |
issuesTextView = (TextView) findViewById(R.id.issues_text); | |
communicationController = new CommunicationController(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.menu_credentials: | |
showCredentialsDialog(); | |
return true; | |
case R.id.menu_refresh: | |
queryForIssues(); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
private DisposableSingleObserver<List<Issue>> getIssuesObserver() { | |
return new DisposableSingleObserver<List<Issue>>() { | |
@Override | |
public void onSuccess(List<Issue> issues) { | |
for (Issue issue : issues) { | |
issuesTextView.append(issue.toString() + "\n"); | |
} | |
} | |
@Override | |
public void onError(Throwable e) { | |
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); | |
} | |
}; | |
} | |
private void showCredentialsDialog() { | |
CredentialsDialog dialog = new CredentialsDialog(); | |
Bundle arguments = new Bundle(); | |
arguments.putString("username", username); | |
arguments.putString("password", password); | |
dialog.setArguments(arguments); | |
dialog.show(getSupportFragmentManager(), "credentialsDialog"); | |
} | |
@Override | |
public void onDialogPositiveClick(String username, String password) { | |
this.username = username; | |
this.password = password; | |
} | |
private void queryForIssues() { | |
issuesTextView.setText(""); | |
String owner = ownerEditText.getText().toString(); | |
String repository = repositoryEditText.getText().toString(); | |
if (username.isEmpty() || password.isEmpty()) { | |
Toast.makeText(MainActivity.this, "Please provide your credentials", Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
if (owner.isEmpty() || repository.isEmpty()) { | |
Toast.makeText(MainActivity.this, "Please provider repository owner and/or name", Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
issuesObserver = getIssuesObserver(); | |
communicationController.loadIssues(username, password, issuesObserver , owner, repository); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
if (issuesObserver!=null && !issuesObserver.isDisposed()) { | |
issuesObserver.dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment