-
-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from herau/fix/102-login-issue
fix Login issue #102
- Loading branch information
Showing
8 changed files
with
174 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 0 additions & 82 deletions
82
app/src/main/java/openfoodfacts/github/scrachx/openfood/fragments/UserFragment.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
app/src/main/java/openfoodfacts/github/scrachx/openfood/views/LoginActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package openfoodfacts.github.scrachx.openfood.views; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.text.TextUtils; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.loopj.android.http.AsyncHttpResponseHandler; | ||
import com.loopj.android.http.RequestParams; | ||
|
||
import net.steamcrafted.loadtoast.LoadToast; | ||
|
||
import butterknife.Bind; | ||
import butterknife.OnClick; | ||
import openfoodfacts.github.scrachx.openfood.R; | ||
import openfoodfacts.github.scrachx.openfood.network.FoodUserClient; | ||
import openfoodfacts.github.scrachx.openfood.utils.Utils; | ||
|
||
/** | ||
* A login screen that offers login via login/password. | ||
*/ | ||
public class LoginActivity extends BaseActivity { | ||
|
||
@Bind(R.id.editTextLogin) EditText loginView; | ||
@Bind(R.id.editTextPass) EditText passwordView; | ||
@Bind(R.id.textInfoLogin) TextView infoLogin; | ||
@Bind(R.id.buttonSave) Button save; | ||
@Bind(R.id.buttonCreateAccount) Button signup; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_login); | ||
} | ||
|
||
@OnClick(R.id.buttonCreateAccount) | ||
protected void onCreateUser() { | ||
Intent browser = new Intent(Intent.ACTION_VIEW); | ||
browser.setData(Uri.parse(Utils.getUriByCurrentLanguage() + "cgi/user.pl")); | ||
startActivity(browser); | ||
} | ||
|
||
@OnClick(R.id.buttonSave) | ||
protected void attemptLogin() { | ||
String login = this.loginView.getText().toString(); | ||
String password = passwordView.getText().toString(); | ||
|
||
if (!(password.length() > 6)) { | ||
passwordView.setError(getString(R.string.error_invalid_password)); | ||
passwordView.requestFocus(); | ||
return; | ||
} | ||
|
||
if (TextUtils.isEmpty(login)) { | ||
loginView.setError(getString(R.string.error_field_required)); | ||
loginView.requestFocus(); | ||
return; | ||
} | ||
|
||
RequestParams requestParams = new RequestParams(); | ||
requestParams.put("user_id", login); | ||
requestParams.put("password", password); | ||
requestParams.put(".submit", "Sign-in"); | ||
|
||
final Activity context = this; | ||
|
||
FoodUserClient.post("/cgi/session.pl", requestParams, new AsyncHttpResponseHandler() { | ||
|
||
LoadToast lt = new LoadToast(context); | ||
|
||
@Override | ||
public void onStart() { | ||
super.onStart(); | ||
save.setClickable(false); | ||
lt.setText(context.getString(R.string.toast_retrieving)); | ||
lt.setBackgroundColor(context.getResources().getColor(R.color.indigo_600)); | ||
lt.setTextColor(context.getResources().getColor(R.color.white)); | ||
lt.show(); | ||
} | ||
|
||
@Override | ||
public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) { | ||
SharedPreferences settings = context.getSharedPreferences("login", 0); | ||
SharedPreferences.Editor editor = settings.edit(); | ||
String htmlNoParsed = new String(responseBody); | ||
|
||
if (htmlNoParsed.contains("Incorrect user name or password.") || htmlNoParsed.contains("See you soon!")) { | ||
lt.error(); | ||
Toast.makeText(context, context.getString(R.string.errorLogin), Toast.LENGTH_LONG).show(); | ||
loginView.setText(""); | ||
passwordView.setText(""); | ||
editor.putString("user", ""); | ||
editor.putString("pass", ""); | ||
editor.apply(); | ||
infoLogin.setText(R.string.txtInfoLoginNo); | ||
} else { | ||
lt.success(); | ||
Toast.makeText(context, context.getResources().getText(R.string.txtToastSaved), Toast.LENGTH_LONG).show(); | ||
editor.putString("user", loginView.getText().toString()); | ||
editor.putString("pass", passwordView.getText().toString()); | ||
editor.apply(); | ||
infoLogin.setText(R.string.txtInfoLoginOk); | ||
|
||
setResult(RESULT_OK, new Intent()); | ||
finish(); | ||
} | ||
Utils.hideKeyboard(context); | ||
} | ||
|
||
@Override | ||
public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody, Throwable error) { | ||
Toast.makeText(context, context.getString(R.string.errorWeb), Toast.LENGTH_LONG).show(); | ||
lt.error(); | ||
Utils.hideKeyboard(context); | ||
} | ||
|
||
@Override | ||
public void onFinish() { | ||
super.onFinish(); | ||
save.setClickable(true); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.