Anthony Restaino
8 years ago
4 changed files with 309 additions and 231 deletions
@ -0,0 +1,138 @@
@@ -0,0 +1,138 @@
|
||||
package acr.browser.lightning.dialog; |
||||
|
||||
import android.app.Activity; |
||||
import android.content.DialogInterface; |
||||
import android.support.annotation.NonNull; |
||||
import android.support.annotation.Nullable; |
||||
import android.support.annotation.StringRes; |
||||
import android.support.v7.app.AlertDialog; |
||||
import android.text.TextUtils; |
||||
import android.util.Log; |
||||
import android.view.View; |
||||
import android.widget.ArrayAdapter; |
||||
import android.widget.EditText; |
||||
import android.widget.LinearLayout; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
import acr.browser.lightning.R; |
||||
import acr.browser.lightning.bus.BookmarkEvents; |
||||
import acr.browser.lightning.constant.Constants; |
||||
import acr.browser.lightning.database.HistoryItem; |
||||
import acr.browser.lightning.utils.Utils; |
||||
|
||||
/** |
||||
* Copyright 7/31/2016 Anthony Restaino |
||||
* <p/> |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* <p/> |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p/> |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
public class BrowserDialog { |
||||
|
||||
public interface Listener { |
||||
void onClick(); |
||||
} |
||||
|
||||
public interface EditorListener { |
||||
void onClick(String text); |
||||
} |
||||
|
||||
public static abstract class Item { |
||||
|
||||
private int mTitle; |
||||
|
||||
public Item(@StringRes int title) { |
||||
mTitle = title; |
||||
} |
||||
|
||||
@StringRes |
||||
private int getTitle() { |
||||
return mTitle; |
||||
} |
||||
|
||||
public abstract void onClick(); |
||||
} |
||||
|
||||
|
||||
public static void show(@NonNull Activity activity, @NonNull Item item, @Nullable Item... items) { |
||||
show(activity, null, item, items); |
||||
} |
||||
|
||||
public static void show(@NonNull Activity activity, @StringRes int title, @NonNull Item item, @Nullable Item... items) { |
||||
show(activity, activity.getString(title), item, items); |
||||
} |
||||
|
||||
public static void show(@NonNull Activity activity, @Nullable String title, @NonNull Item item, @Nullable Item... items) { |
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity); |
||||
ArrayAdapter<String> adapter = new ArrayAdapter<>(activity, |
||||
android.R.layout.simple_list_item_1); |
||||
|
||||
final List<Item> itemList = new ArrayList<>(1); |
||||
itemList.add(item); |
||||
if (items != null) { |
||||
itemList.addAll(Arrays.asList(items)); |
||||
} |
||||
|
||||
for (Item it : itemList) { |
||||
adapter.add(activity.getString(it.getTitle())); |
||||
} |
||||
|
||||
if (!TextUtils.isEmpty(title)) { |
||||
builder.setTitle(title); |
||||
} |
||||
builder.setAdapter(adapter, new DialogInterface.OnClickListener() { |
||||
|
||||
@Override |
||||
public void onClick(DialogInterface dialog, int which) { |
||||
itemList.get(which).onClick(); |
||||
} |
||||
}); |
||||
builder.show(); |
||||
} |
||||
|
||||
public static void showEditText(@NonNull Activity activity, @StringRes int title, |
||||
@StringRes int hint, @StringRes int action, |
||||
@NonNull final EditorListener listener) { |
||||
showEditText(activity, title, hint, null, action, listener); |
||||
} |
||||
|
||||
public static void showEditText(@NonNull Activity activity, @StringRes int title, |
||||
@StringRes int hint, @Nullable String currentText, |
||||
@StringRes int action, @NonNull final EditorListener listener) { |
||||
final AlertDialog.Builder editorDialog = new AlertDialog.Builder(activity); |
||||
editorDialog.setTitle(title); |
||||
final EditText editText = new EditText(activity); |
||||
editText.setHint(hint); |
||||
if (currentText != null) { |
||||
editText.setText(currentText); |
||||
} |
||||
editText.setSingleLine(); |
||||
LinearLayout layout = new LinearLayout(activity); |
||||
layout.setOrientation(LinearLayout.VERTICAL); |
||||
int padding = Utils.dpToPx(10); |
||||
layout.setPadding(padding, padding, padding, padding); |
||||
layout.addView(editText); |
||||
editorDialog.setView(layout); |
||||
editorDialog.setPositiveButton(action, |
||||
new DialogInterface.OnClickListener() { |
||||
|
||||
@Override |
||||
public void onClick(DialogInterface dialog, int which) { |
||||
listener.onClick(editText.getText().toString()); |
||||
} |
||||
}); |
||||
editorDialog.show(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue