Add a subtitle to AddDirectoryActivity containing the currently displayed folder's path.

This commit is contained in:
Eder Bastos 2015-05-10 11:07:16 -04:00
parent ca4bec3539
commit abaf41baa7
3 changed files with 40 additions and 20 deletions

View File

@ -24,6 +24,7 @@ public class AddDirectoryActivity extends Activity implements FileAdapter.FileCl
public static final String KEY_CURRENT_PATH = BuildConfig.APPLICATION_ID + ".path"; public static final String KEY_CURRENT_PATH = BuildConfig.APPLICATION_ID + ".path";
private FileAdapter mAdapter; private FileAdapter mAdapter;
private Toolbar mToolbar;
@Override @Override
protected void onCreate(Bundle savedInstanceState) protected void onCreate(Bundle savedInstanceState)
@ -32,8 +33,8 @@ public class AddDirectoryActivity extends Activity implements FileAdapter.FileCl
setContentView(R.layout.activity_add_directory); setContentView(R.layout.activity_add_directory);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_folder_list); mToolbar = (Toolbar) findViewById(R.id.toolbar_folder_list);
setActionBar(toolbar); setActionBar(mToolbar);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list_files); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list_files);
@ -46,7 +47,8 @@ public class AddDirectoryActivity extends Activity implements FileAdapter.FileCl
if (savedInstanceState == null) if (savedInstanceState == null)
{ {
path = Environment.getExternalStorageDirectory().getPath(); path = Environment.getExternalStorageDirectory().getPath();
} else }
else
{ {
// Get the path we were looking at before we rotated. // Get the path we were looking at before we rotated.
path = savedInstanceState.getString(KEY_CURRENT_PATH); path = savedInstanceState.getString(KEY_CURRENT_PATH);
@ -71,7 +73,7 @@ public class AddDirectoryActivity extends Activity implements FileAdapter.FileCl
switch (item.getItemId()) switch (item.getItemId())
{ {
case R.id.menu_up_one_level: case R.id.menu_up_one_level:
mAdapter.setPath(mAdapter.getPath() + "/.."); mAdapter.upOneLevel();
break; break;
} }
@ -101,4 +103,10 @@ public class AddDirectoryActivity extends Activity implements FileAdapter.FileCl
finish(); finish();
} }
@Override
public void updateSubtitle(String path)
{
mToolbar.setSubtitle(path);
}
} }

View File

@ -69,6 +69,8 @@ public final class GameGridActivity extends Activity
public void onClick(View view) public void onClick(View view)
{ {
Intent fileChooser = new Intent(GameGridActivity.this, AddDirectoryActivity.class); Intent fileChooser = new Intent(GameGridActivity.this, AddDirectoryActivity.class);
// The second argument to this method is read below in onActivityResult().
startActivityForResult(fileChooser, REQUEST_ADD_DIRECTORY); startActivityForResult(fileChooser, REQUEST_ADD_DIRECTORY);
} }
}); });
@ -85,9 +87,9 @@ public final class GameGridActivity extends Activity
/** /**
* Callback from AddDirectoryActivity. Applies any changes necessary to the GameGridActivity. * Callback from AddDirectoryActivity. Applies any changes necessary to the GameGridActivity.
* *
* @param requestCode * @param requestCode An int describing whether the Activity that is returning did so successfully.
* @param resultCode * @param resultCode An int describing what Activity is giving us this callback.
* @param result * @param result The information the returning Activity is providing us.
*/ */
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent result) protected void onActivityResult(int requestCode, int resultCode, Intent result)
@ -99,6 +101,7 @@ public final class GameGridActivity extends Activity
// other activities might use this callback in the future (don't forget to change Javadoc!) // other activities might use this callback in the future (don't forget to change Javadoc!)
if (requestCode == REQUEST_ADD_DIRECTORY) if (requestCode == REQUEST_ADD_DIRECTORY)
{ {
// Get the path the user selected in AddDirectoryActivity.
String path = result.getStringExtra(AddDirectoryActivity.KEY_CURRENT_PATH); String path = result.getStringExtra(AddDirectoryActivity.KEY_CURRENT_PATH);
// Store this path as a preference. // Store this path as a preference.

View File

@ -26,12 +26,14 @@ public class FileAdapter extends RecyclerView.Adapter<FileViewHolder> implements
* Initializes the dataset to be displayed, and associates the Adapter with the * Initializes the dataset to be displayed, and associates the Adapter with the
* Activity as an event listener. * Activity as an event listener.
* *
* @param gameList * @param path A String containing the path to the directory to be shown by this Adapter.
* @param listener An Activity that can respond to callbacks from this Adapter.
*/ */
public FileAdapter(String path, FileClickListener listener) public FileAdapter(String path, FileClickListener listener)
{ {
mFileList = generateFileList(new File(path)); mFileList = generateFileList(new File(path));
mListener = listener; mListener = listener;
mListener.updateSubtitle(path);
} }
/** /**
@ -107,12 +109,12 @@ public class FileAdapter extends RecyclerView.Adapter<FileViewHolder> implements
* When a file is clicked, determine if it is a directory; if it is, show that new directory's * When a file is clicked, determine if it is a directory; if it is, show that new directory's
* contents. If it is not, end the activity successfully. * contents. If it is not, end the activity successfully.
* *
* @param view * @param view The View representing the file the user clicked on.
*/ */
@Override @Override
public void onClick(final View view) public void onClick(final View view)
{ {
String path = (String) view.getTag(); final String path = (String) view.getTag();
File clickedFile = new File(path); File clickedFile = new File(path);
@ -135,6 +137,7 @@ public class FileAdapter extends RecyclerView.Adapter<FileViewHolder> implements
{ {
mFileList = fileList; mFileList = fileList;
notifyDataSetChanged(); notifyDataSetChanged();
mListener.updateSubtitle(path);
} }
}, 200); }, 200);
} }
@ -148,7 +151,7 @@ public class FileAdapter extends RecyclerView.Adapter<FileViewHolder> implements
/** /**
* For a given directory, return a list of Files it contains. * For a given directory, return a list of Files it contains.
* *
* @param directory * @param directory A File representing the directory that should have its contents displayed.
* @return * @return
*/ */
private ArrayList<FileListItem> generateFileList(File directory) private ArrayList<FileListItem> generateFileList(File directory)
@ -176,26 +179,32 @@ public class FileAdapter extends RecyclerView.Adapter<FileViewHolder> implements
return mPath; return mPath;
} }
/**
* Mostly just allows the activity's menu option to kick us up a level in the directory
* structure.
*
* @param path
*/
public void setPath(String path) public void setPath(String path)
{ {
mPath = path; File directory = new File(path);
File parentDirectory = new File(path);
mFileList = generateFileList(directory);
notifyDataSetChanged();
mListener.updateSubtitle(path);
}
public void upOneLevel()
{
File currentDirectory = new File(mPath);
File parentDirectory = currentDirectory.getParentFile();
mFileList = generateFileList(parentDirectory); mFileList = generateFileList(parentDirectory);
notifyDataSetChanged(); notifyDataSetChanged();
mListener.updateSubtitle(mPath);
} }
/** /**
* Callback for when the user wants to add the visible directory to the library. * Callback to the containing Activity.
*/ */
public interface FileClickListener public interface FileClickListener
{ {
void finishSuccessfully(); void finishSuccessfully();
void updateSubtitle(String path);
} }
} }