Android: tags in drawer

This commit is contained in:
jendib 2014-11-23 19:55:08 +01:00
parent 1773998ca0
commit 6a9a166670
12 changed files with 219 additions and 8 deletions

View File

@ -16,14 +16,16 @@ import org.json.JSONObject;
public class MainApplication extends Application { public class MainApplication extends Application {
@Override @Override
public void onCreate() { public void onCreate() {
// Fetching /user/info from cache // Fetching GET /user from cache
JSONObject json = PreferenceUtil.getCachedJson(getApplicationContext(), PreferenceUtil.PREF_CACHED_USER_INFO_JSON); JSONObject json = PreferenceUtil.getCachedJson(getApplicationContext(), PreferenceUtil.PREF_CACHED_USER_INFO_JSON);
ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json); ApplicationContext.getInstance().setUserInfo(getApplicationContext(), json);
// TODO Fullscreen preview // TODO Tags in drawer & search on select
// TODO Sharing // TODO Sharing
// TODO Error feedback // TODO Error feedback
// TODO Printing // TODO Printing
// TODO Fullscreen preview
// TODO Caching preferences
super.onCreate(); super.onCreate();
} }

View File

@ -19,6 +19,7 @@ import android.widget.TextView;
import com.loopj.android.http.JsonHttpResponseHandler; import com.loopj.android.http.JsonHttpResponseHandler;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.adapter.FilePagerAdapter; import com.sismics.docs.adapter.FilePagerAdapter;
import com.sismics.docs.event.DocumentFullscreenEvent;
import com.sismics.docs.model.application.ApplicationContext; import com.sismics.docs.model.application.ApplicationContext;
import com.sismics.docs.resource.FileResource; import com.sismics.docs.resource.FileResource;
import com.sismics.docs.util.PreferenceUtil; import com.sismics.docs.util.PreferenceUtil;
@ -31,6 +32,8 @@ import org.json.JSONObject;
import java.util.Date; import java.util.Date;
import de.greenrobot.event.EventBus;
/** /**
* Document activity. * Document activity.
* *
@ -92,6 +95,8 @@ public class DocumentActivity extends ActionBarActivity {
// Grab the document // Grab the document
refreshDocument(document); refreshDocument(document);
EventBus.getDefault().register(this);
} }
/** /**
@ -223,4 +228,14 @@ public class DocumentActivity extends ActionBarActivity {
request.setDescription(description); request.setDescription(description);
downloadManager.enqueue(request); downloadManager.enqueue(request);
} }
public void onEvent(DocumentFullscreenEvent event) {
findViewById(R.id.detailLayout).setVisibility(event.isFullscreen() ? View.GONE : View.VISIBLE);
}
@Override
protected void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
} }

View File

@ -13,13 +13,20 @@ import android.support.v7.widget.SearchView;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.adapter.TagListAdapter;
import com.sismics.docs.event.SearchEvent; import com.sismics.docs.event.SearchEvent;
import com.sismics.docs.model.application.ApplicationContext; import com.sismics.docs.model.application.ApplicationContext;
import com.sismics.docs.provider.RecentSuggestionsProvider; import com.sismics.docs.provider.RecentSuggestionsProvider;
import com.sismics.docs.resource.TagResource;
import org.apache.http.Header;
import org.json.JSONObject; import org.json.JSONObject;
import de.greenrobot.event.EventBus; import de.greenrobot.event.EventBus;
@ -49,7 +56,7 @@ public class MainActivity extends ActionBarActivity {
setContentView(R.layout.main_activity); setContentView(R.layout.main_activity);
// Enable ActionBar app icon to behave as action to toggle nav drawer // Enable ActionBar app icon to behave as action to toggle nav drawer
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
if (getSupportActionBar() != null) { if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
@ -68,6 +75,31 @@ public class MainActivity extends ActionBarActivity {
TextView emailTextView = (TextView) findViewById(R.id.emailTextView); TextView emailTextView = (TextView) findViewById(R.id.emailTextView);
emailTextView.setText(userInfo.optString("email")); emailTextView.setText(userInfo.optString("email"));
// Get tag list to fill the drawer
final ListView tagListView = (ListView) findViewById(R.id.tagListView);
TagResource.stats(this, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
tagListView.setAdapter(new TagListAdapter(response.optJSONArray("stats")));
}
});
// Click on a tag
tagListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TagListAdapter adapter = (TagListAdapter) tagListView.getAdapter();
if (adapter == null) return;
JSONObject tag = adapter.getItem(position);
if (tag == null) return;
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQuery("tag:" + tag.optString("name"), true);
searchView.setIconified(false);
searchView.clearFocus();
drawerLayout.closeDrawers();
}
});
handleIntent(getIntent()); handleIntent(getIntent());
} }

View File

@ -0,0 +1,77 @@
package com.sismics.docs.adapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.sismics.docs.R;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Tag list adapter.
*
* @author bgamard.
*/
public class TagListAdapter extends BaseAdapter {
/**
* Tags.
*/
private JSONArray tags;
/**
* Tag list adapter.
*
* @param tags Tags
*/
public TagListAdapter(JSONArray tags) {
this.tags = tags;
}
@Override
public int getCount() {
return tags.length();
}
@Override
public JSONObject getItem(int position) {
return tags.optJSONObject(position);
}
@Override
public long getItemId(int position) {
return getItem(position).optString("id").hashCode();
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater vi = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.tag_list_item, parent, false);
}
// Fill the view
JSONObject tag = getItem(position);
TextView tagTextView = (TextView) view.findViewById(R.id.tagTextView);
tagTextView.setText(tag.optString("name"));
TextView tagCountTextView = (TextView) view.findViewById(R.id.tagCountTextView);
tagCountTextView.setText(tag.optString("count"));
// Label color filtering
ImageView labelImageView = (ImageView) view.findViewById(R.id.labelImageView);
Drawable labelDrawable = labelImageView.getDrawable().mutate();
labelDrawable.setColorFilter(Color.parseColor(tag.optString("color")), PorterDuff.Mode.MULTIPLY);
labelImageView.setImageDrawable(labelDrawable);
labelImageView.invalidate();
return view;
}
}

View File

@ -0,0 +1,17 @@
package com.sismics.docs.event;
/**
* @author bgamard.
*/
public class DocumentFullscreenEvent {
private boolean fullscreen;
public DocumentFullscreenEvent(boolean fullscreen) {
this.fullscreen = fullscreen;
}
public boolean isFullscreen() {
return fullscreen;
}
}

View File

@ -0,0 +1,24 @@
package com.sismics.docs.resource;
import android.content.Context;
import com.loopj.android.http.JsonHttpResponseHandler;
/**
* Access to /tag API.
*
* @author bgamard
*/
public class TagResource extends BaseResource {
/**
* GET /tag/stats.
*
* @param context Context
* @param responseHandler Callback
*/
public static void stats(Context context, JsonHttpResponseHandler responseHandler) {
init(context);
client.get(getApiUrl(context) + "/tag/stats", responseHandler);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 374 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 479 B

View File

@ -3,7 +3,6 @@
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="12dp" android:padding="12dp"
android:clickable="true" android:clickable="true"
android:focusable="true" android:focusable="true"

View File

@ -6,6 +6,7 @@
android:layout_height="match_parent"> android:layout_height="match_parent">
<RelativeLayout <RelativeLayout
android:id="@+id/detailLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="12dp" android:padding="12dp"

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:id="@+id/labelImageView"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="12dp"
android:layout_marginEnd="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_label_white_24dp"/>
<TextView
android:id="@+id/tagTextView"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/labelImageView"
android:layout_toEndOf="@+id/labelImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textColor="#212121"
android:text="Appartement"
android:textSize="14sp"/>
<TextView
android:id="@+id/tagCountTextView"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textColor="#888"
android:text="5"
android:textSize="14sp"/>
</RelativeLayout>