Entries pages

This commit is contained in:
Przemek Grondek 2014-07-30 14:34:58 +02:00
parent 680b0eff3d
commit 5a12aec282
4 changed files with 95 additions and 34 deletions

View file

@ -15,8 +15,6 @@ public class EntriesActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entries);
Bundle bundle = getIntent().getExtras();
bundle.putInt(Static.PAGE_ARG, 0);
// String titleName = bundle.getString(Static.DATABASE_NAME_ARG)+"->"+bundle.getString(Static.TABLE_NAME_ARG);
String titleName = bundle.getString(Static.TABLE_NAME_ARG);
setTitle(titleName);
@ -29,21 +27,4 @@ public class EntriesActivity extends Activity {
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { // TODO Implement pages
switch (item.getItemId()){
case R.id.action_previous:
case R.id.action_next:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.entries_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}

View file

@ -1,13 +1,13 @@
package info.nerull7.mysqlbrowser;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
@ -25,7 +25,7 @@ import info.nerull7.mysqlbrowser.db.AsyncDatabaseConnector;
/**
* Created by nerull7 on 15.07.14.
*/
public class EntriesFragment extends Fragment implements AsyncDatabaseConnector.MatrixReturnListener, AsyncDatabaseConnector.ListReturnListener{
public class EntriesFragment extends Fragment implements AsyncDatabaseConnector.MatrixReturnListener, AsyncDatabaseConnector.ListReturnListener, AsyncDatabaseConnector.IntegerReturnListener {
private TableLayout entriesTable;
private CustomScrollView entriesScrollView;
private FrameLayout headerFrame;
@ -37,10 +37,14 @@ public class EntriesFragment extends Fragment implements AsyncDatabaseConnector.
private String tableName;
private int entriesLimit;
private int page;
private int pageCount;
private ProgressBar progressBar;
private ScrollView fakeScrollView;
private View dummyView;
private MenuInflater menuInflater;
private Menu menu;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -48,11 +52,12 @@ public class EntriesFragment extends Fragment implements AsyncDatabaseConnector.
initArguments();
initViewItems(rootView);
// setupActionBar();
Static.asyncDatabaseConnector.setIntegerReturnListener(this);
Static.asyncDatabaseConnector.setListReturnListener(this);
Static.asyncDatabaseConnector.setMatrixReturnListener(this);
Static.asyncDatabaseConnector.getFields(tableName);
Static.asyncDatabaseConnector.getEntriesCount(tableName);
return rootView;
}
@ -60,7 +65,7 @@ public class EntriesFragment extends Fragment implements AsyncDatabaseConnector.
private void initArguments(){
databaseName = getArguments().getString(Static.DATABASE_NAME_ARG);
tableName = getArguments().getString(Static.TABLE_NAME_ARG);
page = getArguments().getInt(Static.PAGE_ARG);
page = 1;
entriesLimit = PreferenceManager.getDefaultSharedPreferences(getActivity()).getInt(SettingsFragment.ENTRIES_PAGE_LIMIT, SettingsFragment.ENTRIES_PAGE_LIMIT_DEF);
}
@ -87,11 +92,55 @@ public class EntriesFragment extends Fragment implements AsyncDatabaseConnector.
entriesTable.setVisibility(View.INVISIBLE);
}
// TODO Handling ActionBar
// private void setupActionBar() {
// ActionBar actionBar = getActivity().getActionBar();
// actionBar.
// }
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.entries_activity_actions, menu);
menuInflater = inflater; // I think we need it later
menu.findItem(R.id.action_previous).setVisible(false); // hide previous
this.menu = menu;
// super.onCreateOptionsMenu(menu, inflater);
}
private void changeMenus(int page){
if(page==1){
menu.findItem(R.id.action_previous).setVisible(false);
} else if (page==2){
menu.findItem(R.id.action_previous).setVisible(true);
}
if (page==(pageCount-1)) {
menu.findItem(R.id.action_next).setVisible(true);
} else if (page==pageCount) {
menu.findItem(R.id.action_next).setVisible(false);
}
}
private void setLoading(boolean isLoading){
if(menu != null) {
menu.findItem(R.id.action_next).setEnabled(!isLoading);
menu.findItem(R.id.action_previous).setEnabled(!isLoading);
}
progressBar.setVisibility(isLoading ? View.VISIBLE : View.INVISIBLE);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_previous:
page--;
break;
case R.id.action_next:
page++;
break;
}
changeMenus(page);
entriesTable.removeAllViews(); // clean table
setLoading(true);
Static.asyncDatabaseConnector.getRows(tableName, entriesLimit, page); // get new entries
return super.onOptionsItemSelected(item);
}
@Override
public void onMatrixReturn(List<List<String>> rows) {
@ -120,7 +169,7 @@ public class EntriesFragment extends Fragment implements AsyncDatabaseConnector.
rootView.addView(errorMessage);
}
progressBar.setVisibility(View.INVISIBLE);
setLoading(false);
}
@Override
@ -140,6 +189,16 @@ public class EntriesFragment extends Fragment implements AsyncDatabaseConnector.
Static.asyncDatabaseConnector.getRows(tableName, entriesLimit, page);
}
@Override
public void onIntegerReturn(int result) {
pageCount = result/entriesLimit;
if( result%entriesLimit > 0)
pageCount++;
if(pageCount>1)
setHasOptionsMenu(true);
}
private void syncWidths(){ // TODO: Merge with adding columns maybe? Loops -= 3 should be quicker
TableRow headerRow = (TableRow) headerFrame.getChildAt(0);
int maxWidth[]= new int[headerRow.getChildCount()];

View file

@ -11,7 +11,6 @@ import info.nerull7.mysqlbrowser.db.AsyncDatabaseConnector;
public class Static {
public static final String DATABASE_NAME_ARG = "DatabaseName";
public static final String TABLE_NAME_ARG = "TableName";
public static final String PAGE_ARG = "Page";
public static AsyncDatabaseConnector asyncDatabaseConnector = null;

View file

@ -23,6 +23,7 @@ public class AsyncDatabaseConnector {
public static final String ACTION_DATABASE_LIST = "dblist";
public static final String ACTION_TABLE_LIST = "tablelist";
public static final String ACTION_FIELD_LIST = "fieldlist";
public static final String ACTION_ENTRIES_COUNT = "numrows";
public static final String ACTION_DATA_MATRIX = "getrows";
private String login;
@ -32,6 +33,7 @@ public class AsyncDatabaseConnector {
private String database;
private BooleanReturnListener booleanReturnListener;
private IntegerReturnListener integerReturnListener;
private StringReturnListener stringReturnListener;
private ListReturnListener listReturnListener;
private MatrixReturnListener matrixReturnListener;
@ -151,10 +153,22 @@ public class AsyncDatabaseConnector {
}
public void getRows(String table, int count, int page){
int limitStart = page * count;
int limitStart = (page-1) * count;
getMatrix(actionUrlBuilder(ACTION_DATA_MATRIX)+"&d="+database+"&t="+table+"&s="+limitStart+"&l="+count);
}
public void getEntriesCount(String table){
String urlQuery = actionUrlBuilder(ACTION_ENTRIES_COUNT)+"&d="+database+"&t="+table;
Downloader downloader = new Downloader(new Downloader.OnFinishedListener() {
@Override
public void onFinished(String data, String error) {
if(integerReturnListener!=null)
integerReturnListener.onIntegerReturn(Integer.parseInt(data));
}
});
downloader.execute(urlQuery);
}
public void setBooleanReturnListener(BooleanReturnListener booleanReturnListener){
this.booleanReturnListener = booleanReturnListener;
}
@ -163,6 +177,10 @@ public class AsyncDatabaseConnector {
this.stringReturnListener = stringReturnListener;
}
public void setIntegerReturnListener(IntegerReturnListener integerReturnListener){
this.integerReturnListener = integerReturnListener;
}
public void setListReturnListener(ListReturnListener listReturnListener) {
this.listReturnListener = listReturnListener;
}
@ -174,7 +192,11 @@ public class AsyncDatabaseConnector {
public static interface BooleanReturnListener{
public void onBooleanReturn(boolean result);
}
public static interface IntegerReturnListener{
public void onIntegerReturn(int result);
}
public static interface StringReturnListener{
public void onStringReturn(String data);
}