mirror of
https://github.com/sismics/docs.git
synced 2025-12-30 18:11:44 +00:00
Android: document details
This commit is contained in:
@@ -20,9 +20,19 @@ public class MainApplication extends Application {
|
||||
JSONObject json = PreferenceUtil.getCachedJson(getApplicationContext(), PreferenceUtil.PREF_CACHED_USER_INFO_JSON);
|
||||
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
|
||||
|
||||
// TODO Fullscreen preview
|
||||
// TODO Downloading
|
||||
// TODO Sharing
|
||||
// TODO Shared status
|
||||
// TODO Tags on document
|
||||
// TODO Error feedback
|
||||
// TODO Infinite scrolling on documents
|
||||
// TODO Searching
|
||||
// TODO Printing
|
||||
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onLowMemory() {
|
||||
BitmapAjaxCallback.clearCache();
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.sismics.docs.activity;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.support.v7.app.ActionBarActivity;
|
||||
import android.text.format.DateFormat;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.loopj.android.http.JsonHttpResponseHandler;
|
||||
import com.sismics.docs.R;
|
||||
import com.sismics.docs.adapter.FilePagerAdapter;
|
||||
import com.sismics.docs.model.application.ApplicationContext;
|
||||
import com.sismics.docs.resource.FileResource;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Document activity.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class DocumentActivity extends ActionBarActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(final Bundle args) {
|
||||
super.onCreate(args);
|
||||
|
||||
// Check if logged in
|
||||
if (!ApplicationContext.getInstance().isLoggedIn()) {
|
||||
startActivity(new Intent(this, LoginActivity.class));
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle activity context
|
||||
if (getIntent() == null) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse input document
|
||||
String documentJson = getIntent().getStringExtra("document");
|
||||
if (documentJson == null) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
JSONObject document;
|
||||
try {
|
||||
document = new JSONObject(documentJson);
|
||||
} catch (JSONException e) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
// Setup the activity
|
||||
setContentView(R.layout.document_activity);
|
||||
if (getSupportActionBar() != null) {
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
getSupportActionBar().setHomeButtonEnabled(true);
|
||||
}
|
||||
|
||||
// Grab the document
|
||||
refreshDocument(document);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the displayed document.
|
||||
*
|
||||
* @param document Document in JSON format
|
||||
*/
|
||||
private void refreshDocument(JSONObject document) {
|
||||
String id = document.optString("id");
|
||||
String title = document.optString("title");
|
||||
String date = DateFormat.getDateFormat(this).format(new Date(document.optLong("create_date")));
|
||||
String description = document.optString("description");
|
||||
|
||||
// Fill the layout
|
||||
setTitle(title);
|
||||
TextView createdDateTextView = (TextView) findViewById(R.id.createdDateTextView);
|
||||
createdDateTextView.setText(date);
|
||||
TextView languageTextView = (TextView) findViewById(R.id.languageTextView);
|
||||
languageTextView.setText(document.optString("language"));
|
||||
TextView descriptionTextView = (TextView) findViewById(R.id.descriptionTextView);
|
||||
if (description == null || description.isEmpty()) {
|
||||
descriptionTextView.setVisibility(View.GONE);
|
||||
} else {
|
||||
descriptionTextView.setText(description);
|
||||
}
|
||||
|
||||
// Grab the attached files
|
||||
FileResource.list(this, id, new JsonHttpResponseHandler() {
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
ViewPager fileViewPager = (ViewPager) findViewById(R.id.fileViewPager);
|
||||
fileViewPager.setOffscreenPageLimit(1);
|
||||
fileViewPager.setAdapter(new FilePagerAdapter(DocumentActivity.this, response.optJSONArray("files")));
|
||||
findViewById(R.id.progressBar).setVisibility(View.GONE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
finish();
|
||||
return true;
|
||||
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,7 +88,21 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the displayed documents
|
||||
* Return an item at a given position.
|
||||
*
|
||||
* @param position Item position
|
||||
* @return Item
|
||||
*/
|
||||
public JSONObject getItemAt(int position) {
|
||||
if (documents == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return documents.optJSONObject(position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the displayed documents.
|
||||
* @param documents Documents
|
||||
*/
|
||||
public void setDocuments(JSONArray documents) {
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.sismics.docs.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v4.view.PagerAdapter;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ProgressBar;
|
||||
|
||||
import com.androidquery.AQuery;
|
||||
import com.androidquery.callback.BitmapAjaxCallback;
|
||||
import com.sismics.docs.R;
|
||||
import com.sismics.docs.util.PreferenceUtil;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
|
||||
import it.sephiroth.android.library.imagezoom.ImageViewTouchBase;
|
||||
|
||||
/**
|
||||
* @author bgamard.
|
||||
*/
|
||||
public class FilePagerAdapter extends PagerAdapter {
|
||||
/**
|
||||
* Files list.
|
||||
*/
|
||||
private JSONArray files;
|
||||
|
||||
/**
|
||||
* AQuery.
|
||||
*/
|
||||
private AQuery aq;
|
||||
|
||||
/**
|
||||
* Context.
|
||||
*/
|
||||
private Context context;
|
||||
|
||||
/**
|
||||
* Auth token used to download files.
|
||||
*/
|
||||
private String authToken;
|
||||
|
||||
public FilePagerAdapter(Context context, JSONArray files) {
|
||||
this.files = files;
|
||||
this.context = context;
|
||||
this.authToken = PreferenceUtil.getAuthToken(context);
|
||||
aq = new AQuery(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object instantiateItem(ViewGroup container, int position) {
|
||||
View view = LayoutInflater.from(container.getContext()).inflate(R.layout.file_viewpager_item, container, false);
|
||||
|
||||
ImageViewTouch fileImageView = (ImageViewTouch) view.findViewById(R.id.fileImageView);
|
||||
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.fileProgressBar);
|
||||
JSONObject file = files.optJSONObject(position);
|
||||
String fileUrl = PreferenceUtil.getServerUrl(context) + "/api/file/" + file.optString("id") + "/data?size=web";
|
||||
aq.id(fileImageView)
|
||||
.image(new BitmapAjaxCallback()
|
||||
.url(fileUrl)
|
||||
.progress(progressBar)
|
||||
.animation(AQuery.FADE_IN_NETWORK)
|
||||
.cookie("auth_token", authToken));
|
||||
|
||||
fileImageView.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
|
||||
|
||||
container.addView(view, 0);
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyItem(ViewGroup container, int position, Object object) {
|
||||
container.removeView((View) object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
if (files == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return files.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isViewFromObject(View view, Object object) {
|
||||
return view == object;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.sismics.docs.fragment;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
@@ -7,11 +8,11 @@ import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.loopj.android.http.JsonHttpResponseHandler;
|
||||
import com.sismics.docs.DividerItemDecoration;
|
||||
import com.sismics.docs.R;
|
||||
import com.sismics.docs.activity.DocumentActivity;
|
||||
import com.sismics.docs.adapter.DocListAdapter;
|
||||
import com.sismics.docs.listener.RecyclerItemClickListener;
|
||||
import com.sismics.docs.resource.DocumentResource;
|
||||
@@ -30,8 +31,8 @@ public class DocListFragment extends Fragment {
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
// Initialize the view
|
||||
View view = inflater.inflate(R.layout.doc_list_fragment, container, false);
|
||||
|
||||
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.docList);
|
||||
|
||||
recyclerView.setHasFixedSize(true);
|
||||
recyclerView.setLongClickable(true);
|
||||
|
||||
@@ -46,7 +47,12 @@ public class DocListFragment extends Fragment {
|
||||
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(View view, int position) {
|
||||
Toast.makeText(getActivity(), position + " clicked", Toast.LENGTH_SHORT).show();
|
||||
JSONObject document = adapter.getItemAt(position);
|
||||
if (document != null) {
|
||||
Intent intent = new Intent(getActivity(), DocumentActivity.class);
|
||||
intent.putExtra("document", document.toString());
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -64,6 +70,10 @@ public class DocListFragment extends Fragment {
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
adapter.setDocuments(response.optJSONArray("documents"));
|
||||
|
||||
if (getView() != null) {
|
||||
getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import com.loopj.android.http.RequestParams;
|
||||
* @author bgamard
|
||||
*/
|
||||
public class DocumentResource extends BaseResource {
|
||||
|
||||
/**
|
||||
* GET /document/list.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.sismics.docs.resource;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.loopj.android.http.JsonHttpResponseHandler;
|
||||
|
||||
/**
|
||||
* Access to /file API.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class FileResource extends BaseResource {
|
||||
/**
|
||||
* GET /file/list.
|
||||
*
|
||||
* @param context Context
|
||||
* @param documentId Document ID
|
||||
* @param responseHandler Callback
|
||||
*/
|
||||
public static void list(Context context, String documentId, JsonHttpResponseHandler responseHandler) {
|
||||
init(context);
|
||||
|
||||
client.get(getApiUrl(context) + "/file/list?id=" + documentId, responseHandler);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.sismics.docs.ui.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
|
||||
|
||||
/**
|
||||
* ViewPager for files.
|
||||
*
|
||||
* @author bgamard.
|
||||
*/
|
||||
public class FileViewPager extends ViewPager {
|
||||
|
||||
public FileViewPager(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public FileViewPager(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
|
||||
if (v instanceof ImageViewTouch) {
|
||||
return ((ImageViewTouch) v).canScroll(dx);
|
||||
} else {
|
||||
return super.canScroll(v, checkV, dx, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user