Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
* Cleaned and reorganization of code for a functional refresh
Browse files Browse the repository at this point in the history
* Better handling of drawer and title status
* Fixed issue with empty/blank fragment
  • Loading branch information
lgallard committed Oct 16, 2014
1 parent 88dbef6 commit 9beb34a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 127 deletions.
4 changes: 2 additions & 2 deletions qBittorrentClient/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lgallardo.qbittorrentclient"
android:versionCode="220"
android:versionName="2.2.0" >
android:versionCode="232"
android:versionName="2.3.2" >

<uses-sdk
android:minSdkVersion="14"
Expand Down
184 changes: 59 additions & 125 deletions qBittorrentClient/src/com/lgallardo/qbittorrentclient/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
public class MainActivity extends FragmentActivity {

// Params to get JSON Array
private static String[] params = new String[4];
private static String[] params = new String[2];

// JSON Node Names
protected static final String TAG_USER = "user";
Expand Down Expand Up @@ -197,10 +197,6 @@ protected void onCreate(Bundle savedInstanceState) {

drawerList = (ListView) findViewById(R.id.left_drawer);

drawerList.setItemChecked(0, true);
// drawerList.setSelection(0);
setTitle(navigationDrawerItemTitles[0]);

// Drawer item list objects
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[8];

Expand All @@ -217,6 +213,12 @@ protected void onCreate(Bundle savedInstanceState) {
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
drawerList.setAdapter(adapter);

// Set All checked
drawerList.setItemChecked(0, true);

// Set title to All
setTitle(navigationDrawerItemTitles[0]);

// Set the item click listener
drawerList.setOnItemClickListener(new DrawerItemClickListener());

Expand All @@ -231,7 +233,7 @@ protected void onCreate(Bundle savedInstanceState) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(title);
// getActionBar().setTitle(title);
}

/** Called when a drawer has settled in a completely open state. */
Expand Down Expand Up @@ -460,18 +462,12 @@ public void onBackPressed() {

private void refresh() {

refresh("all", false);
refresh("all");

}

private void refresh(String state) {

refresh(state, false);

}

private void refresh(String state, boolean clear) {

if (oldVersion == true) {
params[0] = "json/events";
} else {
Expand All @@ -480,14 +476,6 @@ private void refresh(String state, boolean clear) {

params[1] = state;

if (clear) {
params[2] = "clear";
} else {
params[2] = "";
}

params[3] = "/json/propertiesGeneral/";

ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

Expand Down Expand Up @@ -1161,7 +1149,7 @@ public void refreshWithDelay(final String state, int seconds) {
@Override
public void run() {
// Do something after 5s = 5000ms
refresh(state, true);
refresh(state);
}
}, seconds);
}
Expand Down Expand Up @@ -1540,115 +1528,60 @@ protected void onPostExecute(Torrent[] result) {
MainActivity.lines[i] = torrent;
}

if (findViewById(R.id.one_frame) != null) {
getFragmentManager().popBackStack();
}
// if (findViewById(R.id.one_frame) != null) {
//
// getFragmentManager().popBackStack();
//
// }

firstFragment.setListAdapter(new myAdapter(MainActivity.this, names, lines));
// firstFragment = new ItemstFragment();

// Log.i("Refresh >", "About to set Adapter");
myAdapter myadapter = new myAdapter(MainActivity.this, names, lines);
firstFragment.setListAdapter(myadapter);

// myadapter.notifyDataSetChanged();

// Create the about fragment
AboutFragment aboutFragment = new AboutFragment();

// Add the fragment to the 'list_frame' FrameLayout
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

// Got some results
if (torrentsFiltered.size() > 0) {

// Add the fragment to the 'list_frame' FrameLayout
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

// Set the second fragments container
// Assign the first and second fragment, and
// set the second fragment container
if (findViewById(R.id.fragment_container) != null) {
firstFragment.setSecondFragmentContainer(R.id.content_frame);

if (getFragmentManager().findFragmentByTag("firstFragment") == null) {

fragmentTransaction.replace(R.id.one_frame, firstFragment, "firstFragment");
}

} else {
firstFragment.setSecondFragmentContainer(R.id.one_frame);
fragmentTransaction.replace(R.id.one_frame, firstFragment);
}

fragmentTransaction.commit();

ListView lv = firstFragment.getListView();

lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

// Also update the second fragment (if it comes from the
// drawer)
if (params[2].equals("clear") && lv.getCount() > 0) {

// Scroll to the first position
lv.smoothScrollToPosition(0);

// Notify there isn't any item selected
// firstFragment.setSelection(-1);

if (findViewById(R.id.fragment_container) != null) {

// Reset the BackStack (Back button)
fragmentManager = getFragmentManager();

for (int i = 0; i < getFragmentManager().getBackStackEntryCount(); ++i) {
fragmentManager.popBackStack("secondFragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

// Replace with the about fragment
fragmentManager.beginTransaction().replace(R.id.content_frame, aboutFragment).commit();

} else {
// Set where is the second container
firstFragment.setSecondFragmentContainer(R.id.content_frame);

// Just one fragment
// Reset the BackStack (Back button)
fragmentManager = getFragmentManager();
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
fragmentManager.popBackStack();
}
// Set first fragment
fragmentTransaction.replace(R.id.list_frame, firstFragment);

// Replace with the about fragment
if (fragmentManager.findFragmentByTag("firstFragment") == null) {
// Replace with the about fragment
fragmentManager.beginTransaction().replace(R.id.one_frame, firstFragment, "firstFragment").commit();
}
// Reset back button stack
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
fragmentManager.popBackStack("secondFragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

}

if (params[2].equals("") && lv.getCount() > 0) {

// Scroll listView to the first position
lv.smoothScrollToPosition(0);

if (aboutFragment != null) {

if (findViewById(R.id.fragment_container) != null) {

// Reset the BackStack (Back button)
fragmentManager = getFragmentManager();

for (int i = 0; i < getFragmentManager().getBackStackEntryCount(); ++i) {
getFragmentManager().popBackStack("secondFragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, aboutFragment).commit();

}

else {
// Set second fragment with About fragment
fragmentTransaction.replace(R.id.content_frame, aboutFragment);

fragmentManager = getFragmentManager();
} else {

if (fragmentManager.findFragmentByTag("firstFragment") == null) {
// Set where is the second container
firstFragment.setSecondFragmentContainer(R.id.one_frame);

fragmentManager.beginTransaction().replace(R.id.one_frame, firstFragment, "firstFragment").commit();
}
// Set first and only fragment
fragmentTransaction.replace(R.id.one_frame, firstFragment, "firstFragment");

}
// Reset back button stack
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
fragmentManager.popBackStack();
}

}

} else {
Expand All @@ -1657,26 +1590,27 @@ protected void onPostExecute(Torrent[] result) {
String[] emptyList = new String[] { getString(R.string.no_results) };
firstFragment.setListAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.no_items_found, R.id.no_results, emptyList));

// Add the fragment to the 'list_frame' FrameLayout
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

// Set the second fragments container
if (findViewById(R.id.fragment_container) != null) {
firstFragment.setSecondFragmentContainer(R.id.content_frame);
fragmentTransaction.replace(R.id.list_frame, firstFragment);
fragmentTransaction.replace(R.id.content_frame, aboutFragment);
fragmentTransaction.commit();

} else {
firstFragment.setSecondFragmentContainer(R.id.one_frame);
if (getFragmentManager().findFragmentByTag("firstFragment") == null) {
fragmentTransaction.replace(R.id.one_frame, firstFragment, "firstFragment");
fragmentTransaction.replace(R.id.one_frame, firstFragment, "firstFragment");

// Reset back button stack
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
fragmentManager.popBackStack();
}
fragmentTransaction.commit();
}

}

// Commit
fragmentTransaction.commit();

}
// catch(IllegalStateException le){
//
Expand Down Expand Up @@ -1872,22 +1806,22 @@ private void selectItem(int position) {

switch (position) {
case 0:
refresh("all", true);
refresh("all");
break;
case 1:
refresh("downloading", true);
refresh("downloading");
break;
case 2:
refresh("completed", true);
refresh("completed");
break;
case 3:
refresh("paused", true);
refresh("paused");
break;
case 4:
refresh("active", true);
refresh("active");
break;
case 5:
refresh("inactive", true);
refresh("inactive");
break;
case 6:
// Options - Execute the task in background
Expand Down

0 comments on commit 9beb34a

Please sign in to comment.