mirror of https://github.com/kurisufriend/Clover
parent
d2eb9f6e51
commit
404720292a
@ -0,0 +1,153 @@ |
||||
package org.floens.chan.ui.controller; |
||||
|
||||
import android.app.AlertDialog; |
||||
import android.content.Context; |
||||
import android.text.Html; |
||||
import android.text.method.LinkMovementMethod; |
||||
import android.view.View; |
||||
import android.webkit.WebSettings; |
||||
import android.webkit.WebView; |
||||
import android.widget.Button; |
||||
import android.widget.EditText; |
||||
import android.widget.LinearLayout; |
||||
import android.widget.TextView; |
||||
|
||||
import org.floens.chan.ChanApplication; |
||||
import org.floens.chan.R; |
||||
import org.floens.chan.controller.Controller; |
||||
import org.floens.chan.core.manager.ReplyManager; |
||||
import org.floens.chan.core.model.Pass; |
||||
import org.floens.chan.core.settings.ChanSettings; |
||||
import org.floens.chan.ui.view.CrossfadeView; |
||||
import org.floens.chan.utils.AndroidUtils; |
||||
import org.floens.chan.utils.AnimationUtils; |
||||
|
||||
public class PassSettingsController extends Controller implements View.OnClickListener { |
||||
private LinearLayout container; |
||||
private CrossfadeView crossfadeView; |
||||
private TextView errors; |
||||
private Button button; |
||||
private TextView bottomDescription; |
||||
private EditText inputToken; |
||||
private EditText inputPin; |
||||
private TextView authenticated; |
||||
|
||||
public PassSettingsController(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
@Override |
||||
public void onCreate() { |
||||
super.onCreate(); |
||||
|
||||
navigationItem.title = string(R.string.settings_screen_pass); |
||||
|
||||
view = inflateRes(R.layout.settings_pass); |
||||
container = (LinearLayout) view.findViewById(R.id.container); |
||||
crossfadeView = (CrossfadeView) view.findViewById(R.id.crossfade); |
||||
errors = (TextView) view.findViewById(R.id.errors); |
||||
button = (Button) view.findViewById(R.id.button); |
||||
bottomDescription = (TextView) view.findViewById(R.id.bottom_description); |
||||
inputToken = (EditText) view.findViewById(R.id.input_token); |
||||
inputPin = (EditText) view.findViewById(R.id.input_pin); |
||||
authenticated = (TextView) view.findViewById(R.id.authenticated); |
||||
|
||||
AnimationUtils.setHeight(errors, false, false); |
||||
|
||||
final boolean loggedIn = loggedIn(); |
||||
button.setText(loggedIn ? R.string.setting_pass_logout : R.string.setting_pass_login); |
||||
button.setOnClickListener(this); |
||||
|
||||
bottomDescription.setText(Html.fromHtml(string(R.string.setting_pass_bottom_description))); |
||||
bottomDescription.setMovementMethod(LinkMovementMethod.getInstance()); |
||||
|
||||
inputToken.setText(ChanSettings.passToken.get()); |
||||
inputPin.setText(ChanSettings.passPin.get()); |
||||
|
||||
AndroidUtils.waitForLayout(view, new AndroidUtils.OnMeasuredCallback() { |
||||
@Override |
||||
public void onMeasured(View view) { |
||||
crossfadeView.getLayoutParams().height = crossfadeView.getHeight(); |
||||
crossfadeView.requestLayout(); |
||||
crossfadeView.toggle(!loggedIn, false); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
@Override |
||||
public void onClick(View v) { |
||||
if (v == button) { |
||||
if (loggedIn()) { |
||||
ChanSettings.passId.set(""); |
||||
crossfadeView.toggle(true, true); |
||||
button.setText(R.string.setting_pass_login); |
||||
hideError(); |
||||
((PassSettingControllerListener) navigationController.getPreviousSibling(PassSettingsController.this)).onPassEnabledChanged(false); |
||||
} else { |
||||
auth(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void auth() { |
||||
AndroidUtils.hideKeyboard(view); |
||||
inputToken.setEnabled(false); |
||||
inputPin.setEnabled(false); |
||||
button.setEnabled(false); |
||||
button.setText(R.string.setting_pass_logging_in); |
||||
hideError(); |
||||
|
||||
ChanSettings.passToken.set(inputToken.getText().toString()); |
||||
ChanSettings.passPin.set(inputPin.getText().toString()); |
||||
|
||||
ChanApplication.getReplyManager().sendPass(new Pass(ChanSettings.passToken.get(), ChanSettings.passPin.get()), new ReplyManager.PassListener() { |
||||
@Override |
||||
public void onResponse(ReplyManager.PassResponse response) { |
||||
if (response.isError) { |
||||
if (response.unknownError) { |
||||
WebView webView = new WebView(context); |
||||
WebSettings settings = webView.getSettings(); |
||||
settings.setSupportZoom(true); |
||||
webView.loadData(response.responseData, "text/html", null); |
||||
|
||||
new AlertDialog.Builder(context) |
||||
.setView(webView) |
||||
.setNeutralButton(R.string.ok, null) |
||||
.show(); |
||||
} else { |
||||
showError(response.message); |
||||
} |
||||
button.setText(R.string.setting_pass_login); |
||||
} else { |
||||
crossfadeView.toggle(false, true); |
||||
button.setText(R.string.setting_pass_logout); |
||||
ChanSettings.passId.set(response.passId); |
||||
authenticated.setText(response.message); |
||||
((PassSettingControllerListener) navigationController.getPreviousSibling(PassSettingsController.this)).onPassEnabledChanged(true); |
||||
} |
||||
|
||||
button.setEnabled(true); |
||||
inputToken.setEnabled(true); |
||||
inputPin.setEnabled(true); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
private void showError(String error) { |
||||
errors.setText(error); |
||||
AnimationUtils.setHeight(errors, true, true, container.getWidth()); |
||||
} |
||||
|
||||
private void hideError() { |
||||
errors.setText(null); |
||||
AnimationUtils.setHeight(errors, false, true, container.getHeight()); |
||||
} |
||||
|
||||
private boolean loggedIn() { |
||||
return ChanSettings.passId.get().length() > 0; |
||||
} |
||||
|
||||
public interface PassSettingControllerListener { |
||||
public void onPassEnabledChanged(boolean enabled); |
||||
} |
||||
} |
@ -0,0 +1,87 @@ |
||||
package org.floens.chan.ui.view; |
||||
|
||||
import android.animation.Animator; |
||||
import android.animation.AnimatorListenerAdapter; |
||||
import android.content.Context; |
||||
import android.util.AttributeSet; |
||||
import android.view.View; |
||||
import android.widget.FrameLayout; |
||||
|
||||
public class CrossfadeView extends FrameLayout { |
||||
private int fadeDuration = 200; |
||||
|
||||
private View viewOne; |
||||
private View viewTwo; |
||||
private boolean inited = false; |
||||
private boolean viewOneSelected = true; |
||||
|
||||
public CrossfadeView(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
public CrossfadeView(Context context, AttributeSet attrs) { |
||||
super(context, attrs); |
||||
} |
||||
|
||||
public CrossfadeView(Context context, AttributeSet attrs, int defStyleAttr) { |
||||
super(context, attrs, defStyleAttr); |
||||
} |
||||
|
||||
@Override |
||||
protected void onFinishInflate() { |
||||
super.onFinishInflate(); |
||||
viewOne = getChildAt(0); |
||||
viewTwo = getChildAt(1); |
||||
} |
||||
|
||||
public void toggle(boolean viewOneSelected, boolean animated) { |
||||
if (!inited || this.viewOneSelected != viewOneSelected) { |
||||
this.viewOneSelected = viewOneSelected; |
||||
doToggle(animated); |
||||
} |
||||
} |
||||
|
||||
public void toggle(boolean animated) { |
||||
viewOneSelected = !viewOneSelected; |
||||
doToggle(animated); |
||||
} |
||||
|
||||
private void doToggle(boolean animated) { |
||||
inited = true; |
||||
if (animated) { |
||||
if (viewOneSelected) { |
||||
viewOne.setVisibility(View.VISIBLE); |
||||
viewOne.animate().alpha(1f).setDuration(fadeDuration).setListener(null); |
||||
viewTwo.animate().alpha(0f).setDuration(fadeDuration).setListener(new AnimatorListenerAdapter() { |
||||
@Override |
||||
public void onAnimationEnd(Animator animation) { |
||||
viewOne.setVisibility(View.VISIBLE); |
||||
viewTwo.setVisibility(View.GONE); |
||||
} |
||||
}); |
||||
} else { |
||||
viewTwo.setVisibility(View.VISIBLE); |
||||
viewTwo.animate().alpha(1f).setDuration(fadeDuration).setListener(null); |
||||
viewOne.animate().alpha(0f).setDuration(fadeDuration).setListener(new AnimatorListenerAdapter() { |
||||
@Override |
||||
public void onAnimationEnd(Animator animation) { |
||||
viewOne.setVisibility(View.GONE); |
||||
viewTwo.setVisibility(View.VISIBLE); |
||||
} |
||||
}); |
||||
} |
||||
} else { |
||||
if (viewOneSelected) { |
||||
viewOne.setVisibility(View.VISIBLE); |
||||
viewOne.setAlpha(1f); |
||||
viewTwo.setVisibility(View.GONE); |
||||
viewTwo.setAlpha(0f); |
||||
} else { |
||||
viewOne.setVisibility(View.GONE); |
||||
viewOne.setAlpha(0f); |
||||
viewTwo.setVisibility(View.VISIBLE); |
||||
viewTwo.setAlpha(1f); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,84 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:background="#ffffffff"> |
||||
|
||||
<LinearLayout |
||||
android:id="@+id/container" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:paddingTop="32dp" |
||||
android:paddingLeft="32dp" |
||||
android:paddingRight="32dp" |
||||
android:paddingBottom="32dp" |
||||
android:orientation="vertical"> |
||||
|
||||
<TextView |
||||
android:id="@+id/errors" |
||||
android:textColor="#fff44336" |
||||
android:textSize="16sp" |
||||
android:paddingTop="8dp" |
||||
android:paddingLeft="4dp" |
||||
android:paddingRight="4dp" |
||||
android:paddingBottom="8dp" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
<org.floens.chan.ui.view.CrossfadeView |
||||
android:id="@+id/crossfade" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content"> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="vertical"> |
||||
|
||||
<EditText |
||||
android:id="@+id/input_token" |
||||
android:inputType="text" |
||||
android:imeOptions="actionNext" |
||||
android:hint="@string/setting_pass_token" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
<EditText |
||||
android:id="@+id/input_pin" |
||||
android:inputType="numberPassword" |
||||
android:imeOptions="actionDone" |
||||
android:hint="@string/setting_pass_pin" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
</LinearLayout> |
||||
|
||||
<TextView |
||||
android:id="@+id/authenticated" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_gravity="center_horizontal" |
||||
android:textColor="#dd000000" |
||||
android:textSize="20sp" |
||||
android:text="@string/setting_pass_authenticated" /> |
||||
|
||||
</org.floens.chan.ui.view.CrossfadeView> |
||||
|
||||
<Button |
||||
android:id="@+id/button" |
||||
android:layout_width="130dp" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/bottom_description" |
||||
android:textColor="#dd000000" |
||||
android:textSize="14sp" |
||||
android:paddingTop="16dp" |
||||
android:paddingLeft="4dp" |
||||
android:paddingRight="4dp" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
</LinearLayout> |
||||
|
||||
</ScrollView> |
@ -0,0 +1,24 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<org.floens.chan.ui.view.CrossfadeView xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:id="@+id/crossfade" |
||||
android:background="#ffe5e5e5"> |
||||
|
||||
<include layout="@layout/settings_layout" /> |
||||
|
||||
<FrameLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:padding="8dp"> |
||||
|
||||
<TextView |
||||
style="?android:attr/textAppearanceMedium" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_gravity="center" |
||||
android:gravity="center" |
||||
android:text="@string/setting_watch_info" /> |
||||
</FrameLayout> |
||||
|
||||
</org.floens.chan.ui.view.CrossfadeView> |
Loading…
Reference in new issue