Skip to content

Commit

Permalink
Working app )).
Browse files Browse the repository at this point in the history
  • Loading branch information
mokiyenk authored and mokiyenk committed Sep 7, 2014
1 parent c2d0ee8 commit 305d5f9
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

/**
* Created by s.mokiyenko on 9/6/14.
Expand All @@ -11,12 +12,17 @@ public class BaseBurokratActivity extends Activity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// BurokratApplication.getEventBus().register(this);
BurokratApplication.getEventBus().register(this);
}

@Override
protected void onPause() {
super.onPause();
// BurokratApplication.getEventBus().unregister(this);
BurokratApplication.getEventBus().unregister(this);
}

@SuppressWarnings("unused")
public void onEventMainThread(final RestError error){
Toast.makeText(this, error.getErrorMessage(), Toast.LENGTH_LONG).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
public class BurokratApplication extends Application {

private static EventBus eventBus;
private static ResClient client;

@Override
public void onCreate() {
super.onCreate();
eventBus = new EventBus();
client = new ResClient(eventBus);
}

public static EventBus getEventBus() {
return eventBus;
}

public static ResClient getClient() {
return client;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.smokiyenko.burokrat.app;

/**
* Created by s.mokiyenko on 9/7/14.
*/
public class DocumentStepsResponse {

private final String[] steps;

public DocumentStepsResponse(String[] steps) {
this.steps = steps;
}

public String[] getSteps() {
return steps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.smokiyenko.burokrat.app;

/**
* Created by s.mokiyenko on 9/7/14.
*/
public class DocumentsListResponse {

private final String[] documentsList;


public DocumentsListResponse(String[] documentsList) {
this.documentsList = documentsList;
}

public String[] getDocumentsList() {
return documentsList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import de.greenrobot.event.EventBus;


public class LoginActivity extends BaseBurokratActivity {
Expand All @@ -26,14 +29,19 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

// @SuppressWarnings("Unused")
// //TODO Fix callback when login is ready
// public void onEventOnMainThread(){
//
// }
@SuppressWarnings("Unused")
public void onEventMainThread(final SessionResponse session){
BurokratApplication.getEventBus().unregister(this);
BurokratApplication.getEventBus().postSticky(session);
startActivity(new Intent(this, MainActivity.class));
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}

public void onLoginPressed(final View view) {
startActivity(new Intent(this, MainActivity.class));
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
// startActivity(new Intent(this, MainActivity.class));
// overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
TextView tvPassportId = (TextView) findViewById(R.id.login_passport_id);
TextView tvPassportSeries = (TextView) findViewById(R.id.login_passport_series);
BurokratApplication.getClient().login(tvPassportSeries.getText().toString() , tvPassportId.getText().toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.smokiyenko.burokrat.app;

import android.app.Activity;
import android.net.Uri;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -23,21 +22,28 @@ public class MainActivity extends Activity implements DocumentFragment.OnFragmen
"Документ 8",
"Документ 9",
"Документ 10"};
private SessionResponse session;

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
session = BurokratApplication.getEventBus().getStickyEvent(SessionResponse.class);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null){
DocumentFragment fragment = DocumentFragment.newInstance();
final ArrayList<String> documents = new ArrayList<String>();
documents.addAll(Arrays.asList(documentNames));
fragment.setDocuments(documents);
getFragmentManager().beginTransaction().add(R.id.fragment_container,fragment).commit();
getFragmentManager().beginTransaction().add(R.id.fragment_container,fragment,"doc").commit();
}
}

@SuppressWarnings("Unused")
public void onEventMainThread(final DocumentsListResponse documentsListResponse){
getFragmentManager().getFragment(null,"doc");
}


@Override
public boolean onCreateOptionsMenu(final Menu menu) {
Expand Down Expand Up @@ -67,6 +73,8 @@ public boolean onNavigateUp() {
}




@Override
public void onFragmentInteraction(final String name) {
getActionBar().setTitle(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import android.util.Log;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.gson.Gson;

import de.greenrobot.event.EventBus;
Expand All @@ -14,9 +19,7 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;

/**
* Created by s.mokiyenko on 9/6/14.
Expand All @@ -25,23 +28,23 @@ public class ResClient implements Closeable {

private final EventBus eventBus;

private final ThreadPoolExecutor requestRunner = new ThreadPoolExecutor(1, 1, 5000, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));
private final ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));

public enum RequestUrl {
LOGIN(""),
SEARCH(""),
LOGIN("getUser"),
SEARCH("getDocks"),
QUEUE("");

private final String requestUrl;
//TODO add Server url
private final static String SERVER_URL = "";
private final static String SERVER_URL = "http://gs-rest-service2.cfapps.io/";

RequestUrl(final String requestUrl) {
this.requestUrl = requestUrl;
}

public URL getRequestURL() throws MalformedURLException {
return new URL(SERVER_URL.concat(requestUrl));
public URL getRequestURL(final String body) throws MalformedURLException {
return new URL(SERVER_URL.concat(requestUrl).concat(body));
}

}
Expand All @@ -55,7 +58,7 @@ private abstract class InternalHttpCall implements Runnable {
private final RESTRequest request;
private final RequestUrl requestUrl;

private InternalHttpCall(final RESTRequest restRequest, final RequestUrl requestUrl){
private InternalHttpCall(final RESTRequest restRequest, final RequestUrl requestUrl) {
this.request = restRequest;
this.requestUrl = requestUrl;
}
Expand All @@ -65,12 +68,11 @@ private InternalHttpCall(final RESTRequest restRequest, final RequestUrl request
public void run() {
final HttpURLConnection urlConnection;
try {
urlConnection = (HttpURLConnection) requestUrl.getRequestURL().openConnection();
urlConnection = (HttpURLConnection) requestUrl.getRequestURL(request.getBody()).openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod(request.getRequestType().toString());
if (request.getBody() != null) {
final OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(request.getBody().getBytes("UTF-8"));
}

final int responseCode = HttpUrlConnectionSupport.retrieveResponseCode(urlConnection);

final String result;
Expand All @@ -83,7 +85,8 @@ public void run() {
}
parseResponse(result);
} catch (final Exception ex) {
Log.e("ResClient", "InternalHttpCall exception", ex );
Log.e("ResClient", "InternalHttpCall exception", ex);
throw new RuntimeException(ex);
}
}

Expand Down Expand Up @@ -114,24 +117,59 @@ protected String urlConnectionToString(final HttpURLConnection connection, boole
}


public void login(final String passportSeries, final String passportId){
public void login(final String passportSeries, final String passportId) {

final String body = String.format("?passportSeries=%s&passportId=%s", passportSeries, passportId);
final RESTRequest request = new RESTRequest.Builder().setBody(body).setType(RESTRequest.RequestType.GET).build();

final InternalHttpCall httpCall = new InternalHttpCall(request, RequestUrl.LOGIN) {
@Override
void parseResponse(String json) {
Gson gson = new Gson();
final SessionResponse session = gson.fromJson(json, SessionResponse.class);
eventBus.postSticky(session);
}
};
addCall(httpCall);

}

final String body = String.format("passportSeries=%s,passportSeries=%s",passportId,passportSeries);
final RESTRequest request = new RESTRequest.Builder().setBody(body).setType(RESTRequest.RequestType.POST).build();
public void getDocksList(final SessionResponse sessionResponse) {
final String body = String.format("?jsessionid=%s", sessionResponse.getJsessionid());
final RESTRequest request = new RESTRequest.Builder().setBody(body).setType(RESTRequest.RequestType.GET).build();

final InternalHttpCall httpCall = new InternalHttpCall(request,RequestUrl.LOGIN) {
final InternalHttpCall httpCall = new InternalHttpCall(request, RequestUrl.LOGIN) {
@Override
void parseResponse(String json) {
Gson gson = new Gson();
final DocumentsListResponse documentsListResponse = gson.fromJson(json, DocumentsListResponse.class);
eventBus.post(documentsListResponse);
}
};
addCall(httpCall);
}

requestRunner.submit(httpCall);
private void addCall(final InternalHttpCall call){
final ListenableFuture future = service.submit(call);

Futures.addCallback(future, new FutureCallback() {
@Override
public void onSuccess(final Object result) {
Log.d("RestClient", "onSuccess");
//We have allredy posted result to event bus
}

@Override
public void onFailure(final Throwable t) {
Log.e("RestClient", "onFailure", t);
eventBus.post(new RestError(t.getMessage()));
}
});
}


@Override
public void close() throws IOException {
requestRunner.shutdownNow();
service.shutdownNow();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.smokiyenko.burokrat.app;

/**
* Created by s.mokiyenko on 9/7/14.
*/
public class RestError {

private final String errorMessage;

public RestError(String errorMessage) {
this.errorMessage = errorMessage;
}

public String getErrorMessage() {
return errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.smokiyenko.burokrat.app;

import com.google.common.base.Objects;

/**
* Created by s.mokiyenko on 9/7/14.
*/
public class SessionResponse {

private final String jsessionid;
private final String firstName;
private final String secondName;

public SessionResponse(final String id, final String name, final String sername) {
this.jsessionid = id;
this.firstName = name;
this.secondName = sername;
}

public String getSecondName() {
return secondName;
}

public String getFirstName() {
return firstName;
}

public String getJsessionid() {
return jsessionid;
}

@Override
public String toString() {
return Objects.toStringHelper(SessionResponse.class).add("jsessionid", jsessionid).add("firstName", firstName).add("secondName", secondName).toString();
}
}

0 comments on commit 305d5f9

Please sign in to comment.