|
| 1 | +package com.lenovo.instagram_clone; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.support.v7.app.AppCompatActivity; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.util.Log; |
| 7 | +import android.view.KeyEvent; |
| 8 | +import android.view.View; |
| 9 | +import android.view.inputmethod.InputMethodManager; |
| 10 | +import android.widget.Button; |
| 11 | +import android.widget.EditText; |
| 12 | +import android.widget.ImageView; |
| 13 | +import android.widget.RelativeLayout; |
| 14 | +import android.widget.TextView; |
| 15 | +import android.widget.Toast; |
| 16 | + |
| 17 | +import com.parse.LogInCallback; |
| 18 | +import com.parse.Parse; |
| 19 | +import com.parse.ParseAnalytics; |
| 20 | +import com.parse.ParseException; |
| 21 | +import com.parse.ParseUser; |
| 22 | +import com.parse.SignUpCallback; |
| 23 | + |
| 24 | +public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnKeyListener { |
| 25 | + |
| 26 | + Boolean signUpModeActive = true; |
| 27 | + int i; |
| 28 | + TextView changeSignupModeTextView; |
| 29 | + EditText passwordEditText; |
| 30 | + |
| 31 | + public void showUserList() { |
| 32 | + |
| 33 | + Intent intent = new Intent(getApplicationContext(), UserListActivity.class); |
| 34 | + startActivity(intent); |
| 35 | + |
| 36 | + } |
| 37 | + @Override |
| 38 | + public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 39 | + |
| 40 | + if(i==KeyEvent.KEYCODE_ENTER && event.getAction()==KeyEvent.ACTION_DOWN) |
| 41 | + { |
| 42 | + Signup(v); |
| 43 | + } |
| 44 | + return false; |
| 45 | + } |
| 46 | + @Override |
| 47 | + public void onClick(View view) { |
| 48 | + |
| 49 | + if (view.getId() == R.id.changeSignupModeTextView) { |
| 50 | + |
| 51 | + Button signupButton = (Button) findViewById(R.id.signupButton); |
| 52 | + |
| 53 | + if (signUpModeActive) { |
| 54 | + |
| 55 | + signUpModeActive = false; |
| 56 | + signupButton.setText("Login"); |
| 57 | + changeSignupModeTextView.setText("Or, Signup"); |
| 58 | + |
| 59 | + } else { |
| 60 | + |
| 61 | + signUpModeActive = true; |
| 62 | + signupButton.setText("Signup"); |
| 63 | + changeSignupModeTextView.setText("Or, Login"); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + }else if(view.getId() ==R.id.backgroundRelativeLayout || view.getId() ==R.id.LogoImageView ) |
| 68 | + { |
| 69 | + InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); |
| 70 | + inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0); |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + public void Signup(View view) { |
| 75 | + |
| 76 | + EditText usernameEditText = (EditText) findViewById(R.id.UsernameeditText); |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + if (usernameEditText.getText().toString().matches("") || passwordEditText.getText().toString().matches("")) { |
| 81 | + |
| 82 | + Toast.makeText(this, "A username and password are required.", Toast.LENGTH_SHORT).show(); |
| 83 | + |
| 84 | + } else { |
| 85 | + |
| 86 | + if (signUpModeActive) { |
| 87 | + |
| 88 | + ParseUser user = new ParseUser(); |
| 89 | + |
| 90 | + user.setUsername(usernameEditText.getText().toString()); |
| 91 | + user.setPassword(passwordEditText.getText().toString()); |
| 92 | + |
| 93 | + user.signUpInBackground(new SignUpCallback() { |
| 94 | + @Override |
| 95 | + public void done(ParseException e) { |
| 96 | + if (e == null) { |
| 97 | + |
| 98 | + Log.i("Signup", "Successful"); |
| 99 | + showUserList(); |
| 100 | + |
| 101 | + } else { |
| 102 | + |
| 103 | + Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); |
| 104 | + |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + } else { |
| 110 | + |
| 111 | + ParseUser.logInInBackground(usernameEditText.getText().toString(), passwordEditText.getText().toString(), new LogInCallback() { |
| 112 | + @Override |
| 113 | + public void done(ParseUser user, ParseException e) { |
| 114 | + |
| 115 | + if (user != null) { |
| 116 | + |
| 117 | + Log.i("Signup", "Login successful"); |
| 118 | + showUserList(); |
| 119 | + } else { |
| 120 | + |
| 121 | + Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + } |
| 135 | + @Override |
| 136 | + protected void onCreate(Bundle savedInstanceState) { |
| 137 | + super.onCreate(savedInstanceState); |
| 138 | + setContentView(R.layout.activity_main); |
| 139 | + |
| 140 | + changeSignupModeTextView = (TextView) findViewById(R.id.changeSignupModeTextView); |
| 141 | + changeSignupModeTextView.setOnClickListener(this); |
| 142 | + RelativeLayout backgroundRelativeLayout=(RelativeLayout)findViewById(R.id.backgroundRelativeLayout); |
| 143 | + ImageView logoImageView =(ImageView)findViewById(R.id.LogoImageView); |
| 144 | + backgroundRelativeLayout.setOnClickListener(this); |
| 145 | + logoImageView.setOnClickListener(this); |
| 146 | + passwordEditText = (EditText) findViewById(R.id.PasswordeditText); |
| 147 | + passwordEditText.setOnKeyListener(this); |
| 148 | + |
| 149 | + Parse.enableLocalDatastore(this); |
| 150 | + |
| 151 | + // Add your initialization code here |
| 152 | + Parse.initialize(new Parse.Configuration.Builder(getApplicationContext()) |
| 153 | + .applicationId("aa84cd91fd1c6e4f42d0e8cb27bd479dc9f13c1d") |
| 154 | + .clientKey("0a162ef04c19311d5a900f9ea20b8082b7974274") |
| 155 | + .server("http://18.224.23.225:80/parse/") |
| 156 | + .build() |
| 157 | + ); |
| 158 | + |
| 159 | + if (ParseUser.getCurrentUser() != null) { |
| 160 | + |
| 161 | + showUserList(); |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + ParseAnalytics.trackAppOpenedInBackground(getIntent()); |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | +} |
0 commit comments