Browse Source

Release 0.3.4

Aidan Follestad 10 years ago
parent
commit
3347cac380

+ 8 - 3
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -197,7 +197,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
         }
 
         // Title is set after it's determined whether to use first title or custom view title
-        if (builder.title == null || builder.title.toString().trim().isEmpty()) {
+        if (builder.title == null || builder.title.toString().trim().length() == 0) {
             titleFrame.setVisibility(View.GONE);
             if (customView == null)
                 view.findViewById(R.id.titleFrameCustomView).setVisibility(View.GONE);
@@ -590,7 +590,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
     }
 
     @Override
-    public void onMeasureScroll(ListView view) {
+    public void onMeasureList(ListView view) {
         invalidateCustomViewAssociations();
     }
 
@@ -1062,8 +1062,13 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
     }
 
     public final void setItems(CharSequence[] items) {
+        if (adapter instanceof MaterialDialogAdapter) {
+            adapter = new MaterialDialogAdapter(mContext, ListType.getLayoutForType(listType), R.id.title, items);
+        } else
+            throw new IllegalStateException("When using a custom adapter, setItems() cannot be used. Set items through the adapter instead.");
         this.items = items;
-        invalidateList();
+        listView.setAdapter(adapter);
+        invalidateCustomViewAssociations();
     }
 
     /**

+ 3 - 3
library/src/main/java/com/afollestad/materialdialogs/views/MeasureCallbackListView.java

@@ -22,7 +22,7 @@ public class MeasureCallbackListView extends ListView {
     }
 
     public static interface Callback {
-        void onMeasureScroll(ListView view);
+        void onMeasureList(ListView view);
     }
 
     private Callback mCallback;
@@ -35,6 +35,6 @@ public class MeasureCallbackListView extends ListView {
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         if (mCallback != null)
-            mCallback.onMeasureScroll(this);
+            mCallback.onMeasureList(this);
     }
-}
+}

+ 3 - 3
sample/build.gradle

@@ -6,10 +6,10 @@ android {
 
     defaultConfig {
         applicationId "com.afollestad.materialdialogssample"
-        minSdkVersion 8
+        minSdkVersion 10
         targetSdkVersion 21
-        versionCode 39
-        versionName "0.3.3"
+        versionCode 40
+        versionName "0.3.4"
     }
     buildTypes {
 //        release {

+ 7 - 2
sample/src/main/AndroidManifest.xml

@@ -1,8 +1,13 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.afollestad.materialdialogssample">
 
-    <application android:allowBackup="true" android:label="@string/app_name"
-        android:icon="@drawable/ic_launcher" android:theme="@style/AppTheme">
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+
+    <application
+        android:allowBackup="true"
+        android:label="@string/app_name"
+        android:icon="@drawable/ic_launcher"
+        android:theme="@style/AppTheme">
 
         <activity
             android:name=".MainActivity"

+ 105 - 0
sample/src/main/java/com/afollestad/materialdialogssample/FolderSelectorDialog.java

@@ -0,0 +1,105 @@
+package com.afollestad.materialdialogssample;
+
+import android.app.Activity;
+import android.app.Dialog;
+import android.app.DialogFragment;
+import android.os.Bundle;
+import android.os.Environment;
+import android.view.View;
+
+import com.afollestad.materialdialogs.MaterialDialog;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * @author Aidan Follestad (afollestad)
+ */
+public class FolderSelectorDialog extends DialogFragment implements MaterialDialog.ListCallback, MaterialDialog.Callback {
+
+    File parentFolder;
+    File[] parentContents;
+    boolean canGoUp = true;
+    Callback mCallback;
+
+    public static interface Callback {
+        void onFolderSelection(File folder);
+    }
+
+    public FolderSelectorDialog() {
+        parentFolder = Environment.getExternalStorageDirectory();
+        parentContents = listFiles();
+    }
+
+    public String[] getContentsArray() {
+        String[] results = new String[parentContents.length + (canGoUp ? 1 : 0)];
+        if (canGoUp) results[0] = "...";
+        for (int i = 0; i < parentContents.length; i++)
+            results[canGoUp ? i + 1 : i] = parentContents[i].getName();
+        return results;
+    }
+
+    public File[] listFiles() {
+        File[] contents = parentFolder.listFiles();
+        List<File> results = new ArrayList<File>();
+        for (File fi : contents) {
+            if (fi.isDirectory()) results.add(fi);
+        }
+        Collections.sort(results, new FolderSorter());
+        return results.toArray(new File[results.size()]);
+    }
+
+    @Override
+    public Dialog onCreateDialog(Bundle savedInstanceState) {
+        return new MaterialDialog.Builder(getActivity())
+                .title(parentFolder.getAbsolutePath())
+                .items(getContentsArray())
+                .itemsCallback(this)
+                .callback(this)
+                .autoDismiss(false)
+                .positiveText(R.string.choose)
+                .negativeText(android.R.string.cancel)
+                .build();
+    }
+
+    @Override
+    public void onSelection(MaterialDialog materialDialog, View view, int i, CharSequence s) {
+        if (canGoUp && i == 0) {
+            parentFolder = parentFolder.getParentFile();
+            canGoUp = parentFolder.getParent() != null;
+        } else {
+            parentFolder = parentContents[canGoUp ? i - 1 : i];
+            canGoUp = true;
+        }
+        parentContents = listFiles();
+        MaterialDialog dialog = (MaterialDialog) getDialog();
+        dialog.setTitle(parentFolder.getAbsolutePath());
+        dialog.setItems(getContentsArray());
+    }
+
+    @Override
+    public void onPositive(MaterialDialog materialDialog) {
+        materialDialog.dismiss();
+        mCallback.onFolderSelection(parentFolder);
+    }
+
+    public void show(Activity context, Callback callback) {
+        mCallback = callback;
+        show(context.getFragmentManager(), "FOLDER_SELECTOR");
+    }
+
+    @Override
+    public void onNegative(MaterialDialog materialDialog) {
+        materialDialog.dismiss();
+    }
+
+    public static class FolderSorter implements Comparator<File> {
+        @Override
+        public int compare(File lhs, File rhs) {
+            return lhs.getName().compareTo(rhs.getName());
+        }
+    }
+}

+ 15 - 1
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -22,10 +22,12 @@ import com.afollestad.materialdialogs.DialogAction;
 import com.afollestad.materialdialogs.MaterialDialog;
 import com.afollestad.materialdialogs.Theme;
 
+import java.io.File;
+
 /**
  * @author Aidan Follestad (afollestad)
  */
-public class MainActivity extends ActionBarActivity {
+public class MainActivity extends ActionBarActivity implements FolderSelectorDialog.Callback {
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -136,6 +138,13 @@ public class MainActivity extends ActionBarActivity {
                 showThemed();
             }
         });
+
+        findViewById(R.id.folder_chooser).setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                new FolderSelectorDialog().show(MainActivity.this, MainActivity.this);
+            }
+        });
     }
 
     private void showBasicNoTitle() {
@@ -415,4 +424,9 @@ public class MainActivity extends ActionBarActivity {
         }
         return super.onOptionsItemSelected(item);
     }
+
+    @Override
+    public void onFolderSelection(File folder) {
+        Toast.makeText(this, folder.getAbsolutePath(), Toast.LENGTH_SHORT).show();
+    }
 }

+ 7 - 0
sample/src/main/res/layout/activity_main.xml

@@ -114,6 +114,13 @@
             android:text="@string/themed"
             android:layout_marginTop="@dimen/sample_button_spacing" />
 
+        <Button
+            android:id="@+id/folder_chooser"
+            android:layout_width="match_parent"
+            android:layout_height="56dp"
+            android:text="@string/folder_chooser"
+            android:layout_marginTop="@dimen/sample_button_spacing" />
+
     </LinearLayout>
 
 </ScrollView>

+ 1 - 0
sample/src/main/res/values/strings.xml

@@ -50,6 +50,7 @@
     <string name="basic_longContent">Basic (Long Content)</string>
     <string name="basic_icon">Basic (Icon)</string>
     <string name="basic_list_notitle">Basic List (No Title)</string>
+    <string name="folder_chooser">Folder Chooser</string>
 
     <string-array name="socialNetworks">
         <item>Twitter</item>