Skip to content

Commit

Permalink
Improve design
Browse files Browse the repository at this point in the history
  • Loading branch information
Nilhcem committed May 5, 2013
1 parent 0ef3c93 commit 026ede5
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 32 deletions.
Binary file added assets/fonts/Roboto-Italic.ttf
Binary file not shown.
Binary file removed res/drawable-hdpi/ic_action_refresh.png
Binary file not shown.
Binary file removed res/drawable-mdpi/ic_action_refresh.png
Binary file not shown.
Binary file removed res/drawable-xhdpi/ic_action_refresh.png
Binary file not shown.
27 changes: 17 additions & 10 deletions res/layout/checkable_host_item.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
xmlns:app="http://schemas.android.com/apk/res/com.nilhcem.hostseditor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingTop="4dp" >

<!-- Left part (IP) -->
<TextView
<com.nilhcem.hostseditor.widget.TypefacedTextView
android:id="@+id/hostItemIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:focusable="false"
android:paddingLeft="8dp"
android:paddingRight="18dp"
android:textIsSelectable="false"
android:textSize="18sp" />
android:textSize="16sp"
app:typeface="fonts/Roboto-Regular.ttf" />

<!-- Middle part (hostname/comment) -->
<LinearLayout
Expand All @@ -26,23 +31,25 @@
android:orientation="vertical" >

<!-- Hostname -->
<TextView
<com.nilhcem.hostseditor.widget.TypefacedTextView
android:id="@+id/hostItemHostname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:textIsSelectable="false"
android:textSize="18sp" />
android:textSize="16sp"
app:typeface="fonts/Roboto-Regular.ttf" />

<!-- Comment -->
<TextView
<com.nilhcem.hostseditor.widget.TypefacedTextView
android:id="@+id/hostItemComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:textColor="#ffadadad"
android:textStyle="italic"
android:visibility="gone" />
android:textColor="@color/list_hosts_comment"
android:textSize="12sp"
android:visibility="gone"
app:typeface="fonts/Roboto-Italic.ttf" />
</LinearLayout>

<!-- Right part (checkbox) -->
Expand Down
23 changes: 23 additions & 0 deletions res/layout/list_hosts_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<!-- Hosts list -->
<fragment
android:id="@+id/listHostsFragment"
android:name="com.nilhcem.hostseditor.list.ListHostsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<!-- Loading -->
<ProgressBar
android:id="@+id/listLoading"
style="?android:attr/progressBarStyleLarge"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
1 change: 0 additions & 1 deletion res/menu/list_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<!-- Reload -->
<item
android:id="@+id/action_reload_hosts"
android:icon="@drawable/ic_action_refresh"
android:showAsAction="never|withText"
android:title="@string/action_reload"/>

Expand Down
8 changes: 8 additions & 0 deletions res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- List host (contextual menu) -->
<color name="list_hosts_entry">#fff3f3f3</color>
<color name="list_hosts_comment">#ff909090</color>

</resources>
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
<string name="list_menu_edit">Edit</string>
<string name="list_menu_delete">Delete</string>
<string name="list_menu_toggle">Toggle</string>

</resources>
13 changes: 13 additions & 0 deletions src/com/nilhcem/hostseditor/bus/event/LoadingEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.nilhcem.hostseditor.bus.event;

public class LoadingEvent {
private boolean mIsLoading;

public LoadingEvent(boolean isLoading) {
mIsLoading = isLoading;
}

public boolean isLoading() {
return mIsLoading;
}
}
52 changes: 45 additions & 7 deletions src/com/nilhcem/hostseditor/list/ListHostsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ProgressBar;
import butterknife.InjectView;
import butterknife.Views;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.nilhcem.hostseditor.R;
import com.nilhcem.hostseditor.addedit.AddEditHostActivity;
import com.nilhcem.hostseditor.bus.event.LoadingEvent;
import com.nilhcem.hostseditor.bus.event.StartAddEditActivityEvent;
import com.nilhcem.hostseditor.core.BaseActivity;
import com.nilhcem.hostseditor.core.HostsManager;
Expand All @@ -19,21 +24,24 @@

public class ListHostsActivity extends BaseActivity {
private static final int REQUESTCODE_ADDHOST_ACTIVITY = 1;
private static final String INSTANCE_STATE_LOADING = "loading";

@Inject HostsManager mHostsManager;
@InjectView(R.id.listLoading) ProgressBar mProgressBar;
private ListHostsFragment mFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_hosts_layout);
Views.inject(this);

FragmentManager fragmentMngr = getSupportFragmentManager();
mFragment = (ListHostsFragment) fragmentMngr.findFragmentByTag(ListHostsFragment.TAG);
if (mFragment == null) {
mFragment = new ListHostsFragment();
FragmentTransaction ft = fragmentMngr.beginTransaction();
ft.add(android.R.id.content, mFragment, ListHostsFragment.TAG);
ft.commit();
mFragment = (ListHostsFragment) fragmentMngr.findFragmentById(R.id.listHostsFragment);
mFragment.computeViewWidths();

if (savedInstanceState == null) {
onLoadingEvent(new LoadingEvent(true));
}
}

Expand Down Expand Up @@ -66,17 +74,47 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == REQUESTCODE_ADDHOST_ACTIVITY) {
if (resultCode == RESULT_OK) {
onLoadingEvent(new LoadingEvent(true));
Host modified = data.getParcelableExtra(AddEditHostActivity.EXTRA_HOST_MODIFIED);
Host original = data.getParcelableExtra(AddEditHostActivity.EXTRA_HOST_ORIGINAL);
mFragment.addHost(new Host[] { modified, original });
}
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(INSTANCE_STATE_LOADING, mProgressBar.getVisibility() == View.VISIBLE);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean isLoading = savedInstanceState.getBoolean(INSTANCE_STATE_LOADING, true);
if (isLoading) {
onLoadingEvent(new LoadingEvent(true));
}
}

@Subscribe
public void onEditHostEvent(StartAddEditActivityEvent event) {
public void onStartAddEditActivityEvent(StartAddEditActivityEvent event) {
Intent intent = new Intent(this, AddEditHostActivity.class);
intent.putExtra(AddEditHostActivity.EXTRA_HOST_ORIGINAL, event.getHost());
startActivityForResult(intent, REQUESTCODE_ADDHOST_ACTIVITY);
}

@Subscribe
public void onLoadingEvent(LoadingEvent event) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

if (event.isLoading()) {
mProgressBar.setVisibility(View.VISIBLE);
ft.hide(mFragment);
} else {
mProgressBar.setVisibility(View.GONE);
ft.show(mFragment);
}
ft.commit();
}
}
41 changes: 40 additions & 1 deletion src/com/nilhcem/hostseditor/list/ListHostsAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;

import com.nilhcem.hostseditor.model.Host;
import com.nilhcem.hostseditor.util.Compatibility;
import com.nilhcem.hostseditor.util.ThreadPreconditions;
import com.nilhcem.hostseditor.widget.CheckableHostItem;

public class ListHostsAdapter extends BaseAdapter {
private static final String TAG = "ListHostsAdapter";

private List<Host> mHosts = Collections.emptyList();
private Context mAppContext;

private int mIpMinWidth;
private int mIpMaxWidth;

public ListHostsAdapter(Context context) {
mAppContext = context;
}
Expand All @@ -27,6 +34,38 @@ public void updateHosts(List<Host> hosts) {
notifyDataSetChanged();
}

public void computeViewWidths(Context context) {
// The IP column can be very large, especially when it holds ipv6 with 39 chars.
// Its size has to be generated programmatically, as it should fit with the device's width.
// A tablet can afford having a large column but not a phone.

int screenWidth = Compatibility.getScreenDimensions(context).x;
Log.d(TAG, "Screen width: " + screenWidth);

// Step 1: Compute minimum width.
// Min width must be between [100dp, 160dp]. If possible, 30% of screen width.
int minWidth = screenWidth * 30 / 100;
int minRange = Math.round(Compatibility.convertDpToPixel(100f, context));
int maxRange = Math.round(Compatibility.convertDpToPixel(160f, context));

if (minWidth < minRange) {
minWidth = minRange;
}
if (minWidth > maxRange) {
minWidth = maxRange;
}

// Step 2: Compute maximum width, usually 35% of screen width.
int maxWidth = screenWidth * 35 / 100;
if (maxWidth < minWidth) {
maxWidth = minWidth;
}

Log.d(TAG, "Min width: " + minWidth + " - Max width: " + maxWidth);
mIpMinWidth = minWidth;
mIpMaxWidth = maxWidth;
}

@Override
public int getCount() {
return mHosts.size();
Expand All @@ -50,7 +89,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
}

Host host = getItem(position);
((CheckableHostItem) convertView).init(host);
((CheckableHostItem) convertView).init(host, mIpMinWidth, mIpMaxWidth);
return convertView;
}
}
14 changes: 7 additions & 7 deletions src/com/nilhcem/hostseditor/list/ListHostsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.nilhcem.hostseditor.R;
import com.nilhcem.hostseditor.bus.event.LoadingEvent;
import com.nilhcem.hostseditor.bus.event.RefreshHostsEvent;
import com.nilhcem.hostseditor.bus.event.StartAddEditActivityEvent;
import com.nilhcem.hostseditor.bus.event.TaskCompletedEvent;
Expand All @@ -36,7 +37,7 @@
import com.squareup.otto.Subscribe;

public class ListHostsFragment extends BaseFragment implements OnItemClickListener, OnItemLongClickListener {
static final String TAG = "ListHostsFragment";
private static final String TAG = "ListHostsFragment";

@Inject HostsManager mHostsManager;
private ListHostsAdapter mAdapter;
Expand Down Expand Up @@ -74,11 +75,6 @@ public void onPause() {
super.onPause();
}

@Override
public void onResume() {
super.onResume();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int nbCheckedElements = 0;
Expand Down Expand Up @@ -118,14 +114,14 @@ public void onHostsRefreshed(RefreshHostsEvent hosts) {
finishActionMode();
mAdapter.updateHosts(hosts.get());
mListView.setAdapter(mAdapter);
mBus.post(new LoadingEvent(false));
}

public void addHost(Host[] hosts) {
runGenericTask(AddEditHostAsync.class, hosts);
}

public void refreshHosts(boolean forceRefresh) {
mAdapter.updateHosts(new ArrayList<Host>());
mApp.getObjectGraph().get(ListHostsAsync.class).execute(forceRefresh);
}

Expand All @@ -140,6 +136,10 @@ public void selectAll() {
}
}

public void computeViewWidths() {
mAdapter.computeViewWidths(mActivity);
}

private void displayActionMode(int nbCheckedElements) {
if (nbCheckedElements > 0) {
if (mMode == null) {
Expand Down
7 changes: 7 additions & 0 deletions src/com/nilhcem/hostseditor/task/GenericTaskAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.AsyncTask;
import android.util.Log;

import com.nilhcem.hostseditor.bus.event.LoadingEvent;
import com.nilhcem.hostseditor.bus.event.TaskCompletedEvent;
import com.nilhcem.hostseditor.core.HostsManager;
import com.nilhcem.hostseditor.model.Host;
Expand All @@ -20,6 +21,12 @@ public abstract class GenericTaskAsync extends AsyncTask<Host, Void, Void> {

private Context mAppContext;

@Override
protected void onPreExecute() {
super.onPreExecute();
mBus.post(new LoadingEvent(true));
}

@Override
protected Void doInBackground(Host... params) {
process(params);
Expand Down
7 changes: 7 additions & 0 deletions src/com/nilhcem/hostseditor/task/ListHostsAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import android.os.AsyncTask;

import com.nilhcem.hostseditor.bus.event.LoadingEvent;
import com.nilhcem.hostseditor.bus.event.RefreshHostsEvent;
import com.nilhcem.hostseditor.core.HostsManager;
import com.nilhcem.hostseditor.model.Host;
Expand All @@ -19,6 +20,12 @@ public class ListHostsAsync extends AsyncTask<Boolean, Void, List<Host>> {
@Inject Bus mBus;
@Inject HostsManager mHostsManager;

@Override
protected void onPreExecute() {
super.onPreExecute();
mBus.post(new LoadingEvent(true));
}

@Override
protected List<Host> doInBackground(Boolean... params) {
Boolean forceRefresh = params[0];
Expand Down
Loading

0 comments on commit 026ede5

Please sign in to comment.