New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lib): immutable object for config #60

Merged
merged 1 commit into from Aug 1, 2015
Jump to file or symbol
Failed to load files and symbols.
+178 −121
Diff settings

Always

Just for now

Copy path View file
@@ -116,11 +116,16 @@ You need to declare the `DirectoryChooserActivity` and request the
To choose a directory, start the activity from your app logic:
```java
final Intent chooserIntent = new Intent(this, DirectoryChooserActivity.class);
// Optional: Allow users to create a new directory with a fixed name.
chooserIntent.putExtra(DirectoryChooserActivity.EXTRA_NEW_DIR_NAME,
"DirChooserSample");
final DirectoryChooserConfig config = DirectoryChooserConfig.builder()
.newDirectoryName("DirChooserSample")
.allowReadOnlyDirectory(true)
.allowNewDirectoryNameModification(true)
.build();
chooserIntent.putExtra(DirectoryChooserActivity.EXTRA_CONFIG, config);
// REQUEST_DIRECTORY is a constant integer to identify the request, e.g. 0
startActivityForResult(chooserIntent, REQUEST_DIRECTORY);
@@ -162,7 +167,10 @@ public class DirChooserFragmentSample extends Activity implements
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
mDialog = DirectoryChooserFragment.newInstance("DialogSample", null);
final DirectoryChooserConfig config = DirectoryChooserConfig.builder()
.newDirectoryName("DialogSample")
.build();
mDialog = DirectoryChooserFragment.newInstance(config);
mDirectoryTextView = (TextView) findViewById(R.id.textDirectory);
@@ -176,7 +184,7 @@ public class DirChooserFragmentSample extends Activity implements
}
@Override
public void onSelectDirectory(@Nonnull String path) {
public void onSelectDirectory(@NonNull String path) {
mDirectoryTextView.setText(path);
mDialog.dismiss();
}
@@ -189,6 +197,44 @@ public class DirChooserFragmentSample extends Activity implements
```
### Configuration
The Directory Chooser is configured through a parcelable configuration object, which is great
because it means that you don't have to tear your hair out over finicky string extras. Instead,
you get auto-completion and a nice immutable data structure. Here's what you can configure:
#### `newDirectoryName` : String (required)
Name of the directory to create. User can change this name when he creates the
folder. To avoid this use `allowNewDirectoryNameModification` argument.
#### `initialDirectory` : String (default: "")
Optional argument to define the path of the directory that will be shown first.
If it is not sent or if path denotes a non readable/writable directory or it is not a directory,
it defaults to `android.os.Environment#getExternalStorageDirectory()`.
#### `allowReadOnlyDirectory` : Boolean (default: false)
Argument to define whether or not the directory chooser allows read-only paths to be chosen. If it
false only directories with read-write access can be chosen.
#### `allowNewDirectoryNameModification` : Boolean (default: false)
Argument to define whether or not the directory chooser allows modification of provided new
directory name.
#### Example
```java
final DirectoryChooserConfig config = DirectoryChooserConfig.builder()
.newDirectoryName("DialogSample")
.allowNewDirectoryNameModification(true)
.allowReadOnlyDirectory(true)
.initialDirectory("/sdcard")
.build();
```
## Apps using this
<table>
@@ -273,7 +319,7 @@ gradle build :library:uploadArchives
## License
```text
Copyright 2013-2014 Pascal Hartig
Copyright 2013-2015 Pascal Hartig
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Copy path View file
@@ -1,12 +1,14 @@
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
Copy path View file
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'checkstyle'
repositories {
@@ -10,6 +11,10 @@ dependencies {
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:support-annotations:22.2.1'
compile 'com.gu:option:1.3'
compile 'com.github.frankiesardo:auto-parcel:0.3'
compile 'com.google.auto.value:auto-value:1.1'
apt 'com.github.frankiesardo:auto-parcel-processor:0.3'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.9.5'
testCompile('com.squareup:fest-android:1.0.8') {
@@ -16,43 +16,26 @@
*/
public class DirectoryChooserActivity extends AppCompatActivity implements
DirectoryChooserFragment.OnFragmentInteractionListener {
public static final String EXTRA_NEW_DIR_NAME = "directory_name";
public static final String EXTRA_ALLOW_NEW_DIR_NAME_MODIFICIATION = "allow_directory_name_modification";
/**
* Extra to define the path of the directory that will be shown first.
* If it is not sent or if path denotes a non readable/writable directory
* or it is not a directory, it defaults to
* {@link android.os.Environment#getExternalStorageDirectory()}
*/
public static final String EXTRA_INITIAL_DIRECTORY = "initial_directory";
public static final String EXTRA_CONFIG = "config";
public static final String RESULT_SELECTED_DIR = "selected_dir";
public static final int RESULT_CODE_DIR_SELECTED = 1;
public static final String EXTRA_ALLOW_READ_ONLY_DIRECTORY = "allow_read_only_directory";
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
setContentView(R.layout.directory_chooser_activity);
final String newDirName = getIntent().getStringExtra(EXTRA_NEW_DIR_NAME);
final String initialDir = getIntent().getStringExtra(EXTRA_INITIAL_DIRECTORY);
final boolean allowReadOnlyDir = getIntent().getBooleanExtra(EXTRA_ALLOW_READ_ONLY_DIRECTORY, false);
final boolean allowNewDirNameModification = getIntent().getBooleanExtra(EXTRA_ALLOW_NEW_DIR_NAME_MODIFICIATION, true);
final DirectoryChooserConfig config = getIntent().getParcelableExtra(EXTRA_CONFIG);
if (newDirName == null) {
if (config == null) {
throw new IllegalArgumentException(
"You must provide EXTRA_NEW_DIR_NAME when starting the DirectoryChooserActivity.");
"You must provide EXTRA_CONFIG when starting the DirectoryChooserActivity.");
}
if (savedInstanceState == null) {
final FragmentManager fragmentManager = getFragmentManager();
final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance(newDirName, initialDir,
allowReadOnlyDir, allowNewDirNameModification);
final DirectoryChooserFragment fragment = DirectoryChooserFragment.newInstance(config);
fragmentManager.beginTransaction()
.add(R.id.main, fragment)
.commit();
@@ -0,0 +1,56 @@
package net.rdrei.android.dirchooser;
import android.os.Parcelable;
import auto.parcel.AutoParcel;
@AutoParcel
public abstract class DirectoryChooserConfig implements Parcelable {
/**
* @return Builder for a new DirectoryChooserConfig.
*/
public static Builder builder() {
return new AutoParcel_DirectoryChooserConfig.Builder()
.initialDirectory("")
.allowNewDirectoryNameModification(false)
.allowReadOnlyDirectory(false);
}
/**
* Name of the directory to create. User can change this name when he creates the
* folder. To avoid this use {@link #allowNewDirectoryNameModification} argument.
*/
abstract String newDirectoryName();
/**
* Optional argument to define the path of the directory
* that will be shown first.
* If it is not sent or if path denotes a non readable/writable directory
* or it is not a directory, it defaults to
* {@link android.os.Environment#getExternalStorageDirectory()}
*/
abstract String initialDirectory();
/**
* Argument to define whether or not the directory chooser
* allows read-only paths to be chosen. If it false only
* directories with read-write access can be chosen.
*/
abstract boolean allowReadOnlyDirectory();
/**
* Argument to define whether or not the directory chooser
* allows modification of provided new directory name.
*/
abstract boolean allowNewDirectoryNameModification();
@AutoParcel.Builder
public abstract static class Builder {
public abstract Builder newDirectoryName(String s);
public abstract Builder initialDirectory(String s);
public abstract Builder allowReadOnlyDirectory(boolean b);
public abstract Builder allowNewDirectoryNameModification(boolean b);
public abstract DirectoryChooserConfig build();
}
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.