package movie.bw.com.firstweek;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.gson.Gson;
import movie.bw.com.firstweek.activity.RegisterActivity;
import movie.bw.com.firstweek.activity.ShowActivity;
import movie.bw.com.firstweek.bean.UserInFoBean;
import movie.bw.com.firstweek.mvp.model.MainModellml;
import movie.bw.com.firstweek.mvp.presenter.MainPresenterlml;
import movie.bw.com.firstweek.mvp.view.MainView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, MainView {
private EditText username;
private EditText password;
private Button login;
private Button register;
private SharedPreferences sp;
private MainPresenterlml mainPresenterlml;
private String name;
private String pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
initview();
mainPresenterlml = new MainPresenterlml(new MainModellml(), this);
sp = getSharedPreferences(“user”, MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
String user_name = sp.getString("user_name", null);
String user_pass = sp.getString("user_pass", null);
if (!TextUtils.isEmpty(user_name)){
username.setText(user_name);
}
if (!TextUtils.isEmpty(user_pass)){
password.setText(user_pass);
}
}
private void initview() {
username = findViewById(R.id.edit_username);
password = findViewById(R.id.edit_password);
login = findViewById(R.id.btn_login);
register = findViewById(R.id.btn_register);
login.setOnClickListener(this);
register.setOnClickListener(this);
}
// 登录注册点击
@Override
public void onClick(View v) {
switch (v.getId()){
// 登录
case R.id.btn_login:
dologin();
break;
// 注册
case R.id.btn_register:
startActivity(new Intent(MainActivity.this,RegisterActivity.class));
break;
}
}
// 登录
private void dologin() {
name = username.getText().toString().trim();
pass = password.getText().toString().trim();
if (TextUtils.isEmpty(name)){
Toast.makeText(this,“用户名不能为空”,Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(pass)){
Toast.makeText(this,“密码不能为空”,Toast.LENGTH_LONG).show();
return;
}
// 走登录
mainPresenterlml.doLogin(name, pass);
}
// 成功
@Override
public void success(String data) {
UserInFoBean bean = new Gson().fromJson(data, UserInFoBean.class);
sp.edit().putString(“user_name”,bean.getResult().getPhone())
.putString(“user_nickName”,bean.getResult().getNickName())
.putString(“user_pictrue”,bean.getResult().getHeadPic())
.commit();
// 跳到用户展示
startActivity(new Intent(MainActivity.this,ShowActivity.class));
}
// 失败
@Override
public void fail(String error) {
Toast.makeText(this,error,Toast.LENGTH_LONG).show();
}
}