mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 14:07:55 +01:00
Android: event listeners on main thread, files UI polish
This commit is contained in:
parent
6fa0b8494e
commit
a0356845b1
@ -21,8 +21,6 @@ public class MainApplication extends Application {
|
||||
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
|
||||
|
||||
// TODO Fullscreen preview
|
||||
// TODO Event on file uploaded to refresh file list
|
||||
// TODO Files deleting
|
||||
|
||||
super.onCreate();
|
||||
}
|
||||
|
@ -30,6 +30,7 @@ import com.sismics.docs.adapter.FilePagerAdapter;
|
||||
import com.sismics.docs.event.DocumentDeleteEvent;
|
||||
import com.sismics.docs.event.DocumentEditEvent;
|
||||
import com.sismics.docs.event.DocumentFullscreenEvent;
|
||||
import com.sismics.docs.event.FileAddEvent;
|
||||
import com.sismics.docs.event.FileDeleteEvent;
|
||||
import com.sismics.docs.fragment.DocShareFragment;
|
||||
import com.sismics.docs.listener.JsonHttpResponseHandler;
|
||||
@ -132,7 +133,6 @@ public class DocumentViewActivity extends ActionBarActivity {
|
||||
private void refreshDocument(JSONObject document) {
|
||||
this.document = 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");
|
||||
@ -178,29 +178,7 @@ public class DocumentViewActivity extends ActionBarActivity {
|
||||
sharedImageView.setVisibility(shared ? View.VISIBLE : View.GONE);
|
||||
|
||||
// Grab the attached files
|
||||
final View progressBar = findViewById(R.id.progressBar);
|
||||
final TextView filesEmptyView = (TextView) findViewById(R.id.filesEmptyView);
|
||||
fileViewPager = (ViewPager) findViewById(R.id.fileViewPager);
|
||||
fileViewPager.setOffscreenPageLimit(1);
|
||||
|
||||
FileResource.list(this, id, new JsonHttpResponseHandler() {
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
JSONArray files = response.optJSONArray("files");
|
||||
filePagerAdapter = new FilePagerAdapter(DocumentViewActivity.this, files);
|
||||
fileViewPager.setAdapter(filePagerAdapter);
|
||||
|
||||
progressBar.setVisibility(View.GONE);
|
||||
if (files.length() == 0) filesEmptyView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAllFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
|
||||
filesEmptyView.setText(R.string.error_loading_files);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
filesEmptyView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
});
|
||||
updateFiles();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -425,7 +403,7 @@ public class DocumentViewActivity extends ActionBarActivity {
|
||||
*
|
||||
* @param event Document fullscreen event
|
||||
*/
|
||||
public void onEvent(DocumentFullscreenEvent event) {
|
||||
public void onEventMainThread(DocumentFullscreenEvent event) {
|
||||
findViewById(R.id.detailLayout).setVisibility(event.isFullscreen() ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
|
||||
@ -434,7 +412,7 @@ public class DocumentViewActivity extends ActionBarActivity {
|
||||
*
|
||||
* @param event Document edit event
|
||||
*/
|
||||
public void onEvent(DocumentEditEvent event) {
|
||||
public void onEventMainThread(DocumentEditEvent event) {
|
||||
if (document == null) return;
|
||||
if (event.getDocument().optString("id").equals(document.optString("id"))) {
|
||||
// The current document has been modified, refresh it
|
||||
@ -447,7 +425,7 @@ public class DocumentViewActivity extends ActionBarActivity {
|
||||
*
|
||||
* @param event Document delete event
|
||||
*/
|
||||
public void onEvent(DocumentDeleteEvent event) {
|
||||
public void onEventMainThread(DocumentDeleteEvent event) {
|
||||
if (document == null) return;
|
||||
if (event.getDocumentId().equals(document.optString("id"))) {
|
||||
// The current document has been deleted, close this activity
|
||||
@ -460,9 +438,23 @@ public class DocumentViewActivity extends ActionBarActivity {
|
||||
*
|
||||
* @param event File delete event
|
||||
*/
|
||||
public void onEvent(FileDeleteEvent event) {
|
||||
public void onEventMainThread(FileDeleteEvent event) {
|
||||
if (filePagerAdapter == null) return;
|
||||
filePagerAdapter.remove(event.getFileId());
|
||||
final TextView filesEmptyView = (TextView) findViewById(R.id.filesEmptyView);
|
||||
if (filePagerAdapter.getCount() == 0) filesEmptyView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* A file add event has been fired.
|
||||
*
|
||||
* @param event File add event
|
||||
*/
|
||||
public void onEventMainThread(FileAddEvent event) {
|
||||
if (document == null) return;
|
||||
if (document.optString("id").equals(event.getDocumentId())) {
|
||||
updateFiles();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -499,6 +491,40 @@ public class DocumentViewActivity extends ActionBarActivity {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh files list.
|
||||
*/
|
||||
private void updateFiles() {
|
||||
if (document == null) return;
|
||||
|
||||
final View progressBar = findViewById(R.id.progressBar);
|
||||
final TextView filesEmptyView = (TextView) findViewById(R.id.filesEmptyView);
|
||||
fileViewPager = (ViewPager) findViewById(R.id.fileViewPager);
|
||||
fileViewPager.setOffscreenPageLimit(1);
|
||||
fileViewPager.setAdapter(null);
|
||||
progressBar.setVisibility(View.VISIBLE);
|
||||
filesEmptyView.setVisibility(View.GONE);
|
||||
|
||||
FileResource.list(this, document.optString("id"), new JsonHttpResponseHandler() {
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
JSONArray files = response.optJSONArray("files");
|
||||
filePagerAdapter = new FilePagerAdapter(DocumentViewActivity.this, files);
|
||||
fileViewPager.setAdapter(filePagerAdapter);
|
||||
|
||||
progressBar.setVisibility(View.GONE);
|
||||
if (files.length() == 0) filesEmptyView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAllFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
|
||||
filesEmptyView.setText(R.string.error_loading_files);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
filesEmptyView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
EventBus.getDefault().unregister(this);
|
||||
|
@ -15,6 +15,9 @@ import com.sismics.docs.util.PreferenceUtil;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
|
||||
import it.sephiroth.android.library.imagezoom.ImageViewTouchBase;
|
||||
|
||||
@ -25,7 +28,7 @@ public class FilePagerAdapter extends PagerAdapter {
|
||||
/**
|
||||
* Files list.
|
||||
*/
|
||||
private JSONArray files;
|
||||
private List<JSONObject> files;
|
||||
|
||||
/**
|
||||
* AQuery.
|
||||
@ -42,8 +45,11 @@ public class FilePagerAdapter extends PagerAdapter {
|
||||
*/
|
||||
private String authToken;
|
||||
|
||||
public FilePagerAdapter(Context context, JSONArray files) {
|
||||
this.files = files;
|
||||
public FilePagerAdapter(Context context, JSONArray filesArray) {
|
||||
this.files = new ArrayList<>();
|
||||
for (int i = 0; i < filesArray.length(); i++) {
|
||||
files.add(filesArray.optJSONObject(i));
|
||||
}
|
||||
this.context = context;
|
||||
this.authToken = PreferenceUtil.getAuthToken(context);
|
||||
aq = new AQuery(context);
|
||||
@ -55,7 +61,7 @@ public class FilePagerAdapter extends PagerAdapter {
|
||||
|
||||
ImageViewTouch fileImageView = (ImageViewTouch) view.findViewById(R.id.fileImageView);
|
||||
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.fileProgressBar);
|
||||
JSONObject file = files.optJSONObject(position);
|
||||
JSONObject file = files.get(position);
|
||||
String fileUrl = PreferenceUtil.getServerUrl(context) + "/api/file/" + file.optString("id") + "/data?size=web";
|
||||
aq.id(fileImageView)
|
||||
.image(new BitmapAjaxCallback()
|
||||
@ -82,7 +88,7 @@ public class FilePagerAdapter extends PagerAdapter {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return files.length();
|
||||
return files.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -101,7 +107,7 @@ public class FilePagerAdapter extends PagerAdapter {
|
||||
return null;
|
||||
}
|
||||
|
||||
return files.optJSONObject(position);
|
||||
return files.get(position);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,13 +118,17 @@ public class FilePagerAdapter extends PagerAdapter {
|
||||
public void remove(String fileId) {
|
||||
if (files == null || fileId == null) return;
|
||||
|
||||
for (int i = 0; i < files.length(); i++) {
|
||||
JSONObject file = files.optJSONObject(i);
|
||||
for (JSONObject file : files) {
|
||||
if (fileId.equals(file.optString("id"))) {
|
||||
files.remove(i);
|
||||
files.remove(file);
|
||||
notifyDataSetChanged();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemPosition(Object object) {
|
||||
return POSITION_NONE;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,45 @@
|
||||
package com.sismics.docs.event;
|
||||
|
||||
/**
|
||||
* File add event.
|
||||
*
|
||||
* @author bgamard.
|
||||
*/
|
||||
public class FileAddEvent {
|
||||
/**
|
||||
* Document ID.
|
||||
*/
|
||||
private String documentId;
|
||||
/**
|
||||
* File ID.
|
||||
*/
|
||||
private String fileId;
|
||||
|
||||
/**
|
||||
* Create a file add event.
|
||||
*
|
||||
* @param fileId File ID
|
||||
*/
|
||||
public FileAddEvent(String documentId, String fileId) {
|
||||
this.documentId = documentId;
|
||||
this.fileId = fileId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of fileId.
|
||||
*
|
||||
* @return fileId
|
||||
*/
|
||||
public String getFileId() {
|
||||
return fileId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of documentId.
|
||||
*
|
||||
* @return documentId
|
||||
*/
|
||||
public String getDocumentId() {
|
||||
return documentId;
|
||||
}
|
||||
}
|
@ -150,7 +150,7 @@ public class DocListFragment extends Fragment {
|
||||
*
|
||||
* @param event Search event
|
||||
*/
|
||||
public void onEvent(SearchEvent event) {
|
||||
public void onEventMainThread(SearchEvent event) {
|
||||
query = event.getQuery();
|
||||
loadDocuments(getView(), true);
|
||||
}
|
||||
@ -160,7 +160,7 @@ public class DocListFragment extends Fragment {
|
||||
*
|
||||
* @param event Document edit event
|
||||
*/
|
||||
public void onEvent(DocumentEditEvent event) {
|
||||
public void onEventMainThread(DocumentEditEvent event) {
|
||||
adapter.updateDocument(event.getDocument());
|
||||
}
|
||||
|
||||
@ -169,7 +169,7 @@ public class DocListFragment extends Fragment {
|
||||
*
|
||||
* @param event Document delete event
|
||||
*/
|
||||
public void onEvent(DocumentDeleteEvent event) {
|
||||
public void onEventMainThread(DocumentDeleteEvent event) {
|
||||
adapter.deleteDocument(event.getDocumentId());
|
||||
}
|
||||
|
||||
@ -178,7 +178,7 @@ public class DocListFragment extends Fragment {
|
||||
*
|
||||
* @param event Document add event
|
||||
*/
|
||||
public void onEvent(DocumentAddEvent event) {
|
||||
public void onEventMainThread(DocumentAddEvent event) {
|
||||
// Refresh the list, maybe the new document fit in it
|
||||
loadDocuments(getView(), true);
|
||||
|
||||
|
@ -139,7 +139,7 @@ public class DocShareFragment extends DialogFragment {
|
||||
});
|
||||
}
|
||||
|
||||
public void onEvent(ShareDeleteEvent event) {
|
||||
public void onEventMainThread(ShareDeleteEvent event) {
|
||||
ShareResource.delete(getActivity(), event.getId(), new JsonHttpResponseHandler() {
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
@ -153,7 +153,7 @@ public class DocShareFragment extends DialogFragment {
|
||||
});
|
||||
}
|
||||
|
||||
public void onEvent(ShareSendEvent event) {
|
||||
public void onEventMainThread(ShareSendEvent event) {
|
||||
if (document == null) return;
|
||||
|
||||
// Build the share link
|
||||
|
@ -11,6 +11,7 @@ import android.support.v4.app.NotificationCompat.Builder;
|
||||
import android.util.Log;
|
||||
|
||||
import com.sismics.docs.R;
|
||||
import com.sismics.docs.event.FileAddEvent;
|
||||
import com.sismics.docs.listener.JsonHttpResponseHandler;
|
||||
import com.sismics.docs.resource.FileResource;
|
||||
|
||||
@ -20,6 +21,8 @@ import org.json.JSONObject;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
||||
/**
|
||||
* Service to upload a file to a document in the background.
|
||||
*
|
||||
@ -81,6 +84,7 @@ public class FileUploadService extends IntentService {
|
||||
FileResource.addSync(this, documentId, is, new JsonHttpResponseHandler() {
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
EventBus.getDefault().post(new FileAddEvent(documentId, response.optString("id")));
|
||||
FileUploadService.this.onComplete();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user