Skip to content
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

feature: add feedback alert dialog #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.gradle
/.idea
/build
*/build
*.iml
local.properties
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.1'
classpath 'com.android.tools.build:gradle:1.0.0'
}
}

ext {
compileSdkVersion = 21
buildToolsVersion = "19.1.0"
buildToolsVersion = "21.1.2"
}

allprojects {
Expand Down
118 changes: 118 additions & 0 deletions library/src/main/java/cn/pedant/SweetAlert/ContextOptionDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package cn.pedant.SweetAlert;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;



/**
* Created by pengwei on 15/4/15.
*/
public class ContextOptionDialog extends Dialog{

private TextView titleTextView;
private ListView optionListView;
private Context context;

private String[] options;
private ArrayAdapter<String> optionAdapter;
private String title;

private AdapterView.OnItemClickListener listener;

public ContextOptionDialog(Context context) {
super(context, R.style.alert_dialog);

this.context = context;
setCancelable(true);
setCanceledOnTouchOutside(false);

}

public void setTitle(String title){
if (title == null){
return;
}

this.title = title;
if (titleTextView != null){
titleTextView.setText(title);
}
}

public void setOptions(String[] options){
if (options == null || options.length < 1){
return;
}

this.options = options;
if (optionListView != null){
optionAdapter = new ArrayAdapter<String>(context, R.layout.option_item, options);
optionListView.setAdapter(optionAdapter);
optionAdapter.notifyDataSetChanged();
}
}

public void setOnOptionItemClickListner(AdapterView.OnItemClickListener listener){
this.listener = listener;
}


protected ContextOptionDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}

/**
* Similar to {@link Activity#onCreate}, you should initialize your dialog
* in this method, including calling {@link #setContentView}.
*
* @param savedInstanceState If this dialog is being reinitalized after a
* the hosting activity was previously shut down, holds the result from
* the most recent call to {@link #onSaveInstanceState}, or null if this
* is the first time.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.context_option_dialog);
titleTextView = (TextView) findViewById(R.id.titleTextView);
optionListView = (ListView) findViewById(R.id.optionListView);

if (title != null){
titleTextView.setText(title);
}

if (options != null && options.length > 0){
optionAdapter = new ArrayAdapter<String>(context, R.layout.option_item, options);
optionListView.setAdapter(optionAdapter);
optionListView.setOnItemClickListener(new OnOptionClickListener());
optionAdapter.notifyDataSetChanged();
}

}

private class OnOptionClickListener implements AdapterView.OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (listener != null){
listener.onItemClick(parent, view, position, id);
}

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ContextOptionDialog.this.dismiss();
}
}, 300);
}
}

}
68 changes: 64 additions & 4 deletions library/src/main/java/cn/pedant/SweetAlert/SweetAlertDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Transformation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.*;

import com.pnikosis.materialishprogress.ProgressWheel;

Expand Down Expand Up @@ -52,16 +51,20 @@ public class SweetAlertDialog extends Dialog implements View.OnClickListener {
private Button mCancelButton;
private ProgressHelper mProgressHelper;
private FrameLayout mWarningFrame;
private EditText mFeedbackEdit;
private OnSweetClickListener mCancelClickListener;
private OnSweetClickListener mConfirmClickListener;
private FeedbackTextWatch mFeedbackTextWatch;
private boolean mCloseFromCancel;
private boolean mForceUserInputFeedback;

public static final int NORMAL_TYPE = 0;
public static final int ERROR_TYPE = 1;
public static final int SUCCESS_TYPE = 2;
public static final int WARNING_TYPE = 3;
public static final int CUSTOM_IMAGE_TYPE = 4;
public static final int PROGRESS_TYPE = 5;
public static final int FEEDBACK_TYPE = 6;

public static interface OnSweetClickListener {
public void onClick (SweetAlertDialog sweetAlertDialog);
Expand Down Expand Up @@ -153,10 +156,17 @@ protected void onCreate(Bundle savedInstanceState) {
mWarningFrame = (FrameLayout)findViewById(R.id.warning_frame);
mConfirmButton = (Button)findViewById(R.id.confirm_button);
mCancelButton = (Button)findViewById(R.id.cancel_button);
mFeedbackEdit = (EditText) findViewById(R.id.feed_back_edit);
mProgressHelper.setProgressWheel((ProgressWheel)findViewById(R.id.progressWheel));
mConfirmButton.setOnClickListener(this);
mCancelButton.setOnClickListener(this);

if (mAlertType == FEEDBACK_TYPE && mForceUserInputFeedback){
mConfirmButton.setClickable(false);
mConfirmButton.setBackgroundResource(R.drawable.gray_button_background);
mFeedbackEdit.addTextChangedListener(mFeedbackTextWatch);
}

setTitleText(mTitleText);
setContentText(mContentText);
setCancelText(mCancelText);
Expand All @@ -172,6 +182,7 @@ private void restore () {
mWarningFrame.setVisibility(View.GONE);
mProgressFrame.setVisibility(View.GONE);
mConfirmButton.setVisibility(View.VISIBLE);
mFeedbackEdit.setVisibility(View.GONE);

mConfirmButton.setBackgroundResource(R.drawable.blue_button_background);
mErrorFrame.clearAnimation();
Expand Down Expand Up @@ -220,13 +231,33 @@ private void changeAlertType(int alertType, boolean fromCreate) {
mProgressFrame.setVisibility(View.VISIBLE);
mConfirmButton.setVisibility(View.GONE);
break;
case FEEDBACK_TYPE:
mFeedbackEdit.setVisibility(View.VISIBLE);
mConfirmButton.setVisibility(View.VISIBLE);
break;
}
if (!fromCreate) {
playAnimation();
}
}
}

public void setForceUserInputFeedback(boolean forceUserInputFeedback){
mForceUserInputFeedback = forceUserInputFeedback;
if ((mAlertType == FEEDBACK_TYPE) && forceUserInputFeedback){
if (mFeedbackTextWatch == null){
mFeedbackTextWatch = new FeedbackTextWatch();
}

if (mConfirmButton != null && mFeedbackEdit != null){
mConfirmButton.setClickable(false);
mConfirmButton.setBackgroundResource(R.drawable.gray_button_background);
mFeedbackEdit.addTextChangedListener(mFeedbackTextWatch);
}
}

}

public int getAlerType () {
return mAlertType;
}
Expand All @@ -240,6 +271,10 @@ public String getTitleText () {
return mTitleText;
}

public String getFeedbackText(){
return mFeedbackEdit.getText().toString();
}

public SweetAlertDialog setTitleText (String text) {
mTitleText = text;
if (mTitleTextView != null && mTitleText != null) {
Expand Down Expand Up @@ -379,4 +414,29 @@ public void onClick(View v) {
public ProgressHelper getProgressHelper () {
return mProgressHelper;
}

private class FeedbackTextWatch implements TextWatcher {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
if (s != null && s.length() > 0 ){
mConfirmButton.setClickable(true);
mConfirmButton.setBackgroundResource(R.drawable.blue_button_background);
}else {
mConfirmButton.setClickable(false);
mConfirmButton.setBackgroundResource(R.drawable.gray_button_background);

}
}
}
}
1 change: 1 addition & 0 deletions library/src/main/java/cn/pedant/SweetAlert/tmp

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions library/src/main/res/drawable/feedback_edit_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<stroke
android:color="@color/blue_btn_bg_color"
android:width="1dp" />
<corners android:radius="5dp" />
</shape>
5 changes: 5 additions & 0 deletions library/src/main/res/drawable/option_item_bg_selector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/option_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@color/option_item_bg" />
</selector>
10 changes: 10 additions & 0 deletions library/src/main/res/layout/alert_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@
android:textColor="#797979"
android:visibility="gone" />

<EditText
android:id="@+id/feed_back_edit"
android:visibility="gone"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/feedback_edit_text_padding"
android:background="@drawable/feedback_edit_background"
android:hint="feedback..."/>

<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
Expand Down
35 changes: 35 additions & 0 deletions library/src/main/res/layout/context_option_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/alert_width"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical"
android:background="@drawable/dialog_background"
android:padding="5dp"
>

<TextView
android:id="@+id/titleTextView"
android:text="title"
android:textSize="19sp"
android:textColor="#575757"
android:height="50dp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/success_stroke_color"/>

<ListView
android:scrollbars="none"
android:divider="@color/option_divider_color"
android:dividerHeight="1dp"
android:id="@+id/optionListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>
12 changes: 12 additions & 0 deletions library/src/main/res/layout/option_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/optionItemTextView"
android:text="hello"
android:textSize="19sp"
android:textColor="@color/text_color"
android:layout_width="match_parent"
android:layout_height="44dp"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:background="@drawable/option_item_bg_selector"
/>
3 changes: 3 additions & 0 deletions library/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
<color name="material_blue_grey_95">#ff21272b</color>
<color name="material_deep_teal_20">#ff80cbc4</color>
<color name="material_deep_teal_50">#ff009688</color>
<color name="option_item_bg_pressed">#f0f0f0</color>
<color name="option_item_bg">#ffffff</color>
<color name="option_divider_color">#80575757</color>
</resources>
1 change: 1 addition & 0 deletions library/src/main/res/values/dimen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<dimen name="alert_width">290dp</dimen>
<dimen name="common_circle_width">3dp</dimen>
<dimen name="progress_circle_radius">34dp</dimen>
<dimen name="feedback_edit_text_padding">5dp</dimen>
</resources>
Loading