Commit f0b97423 by Paktalin

Moved SignInActivity to Kotlin

parent 65462142
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
...@@ -22,6 +22,7 @@ dependencies { ...@@ -22,6 +22,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc02' implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation "org.jetbrains.kotlin:kotlin-reflect:1.2.30"
implementation 'com.google.firebase:firebase-core:16.0.3' implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-database:16.0.1' implementation 'com.google.firebase:firebase-database:16.0.1'
...@@ -34,3 +35,4 @@ dependencies { ...@@ -34,3 +35,4 @@ dependencies {
} }
apply plugin: 'com.google.gms.google-services' apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
\ No newline at end of file
...@@ -10,14 +10,14 @@ ...@@ -10,14 +10,14 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
android:name=".VocabularyApplication"> android:name=".VocabularyApplication">
<activity android:name=".SignInActivity" > <activity android:name=".UserActivity"/>
<activity android:name=".SignInActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".UserActivity"/>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
package com.paktalin.vocabularynotebook;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class SignInActivity extends AppCompatActivity {
private static final String TAG = "VN/" + SignInActivity.class.getSimpleName();
private EditText mEmailEt, mPasswordEt;
private Button mSignInBtn, mSignUpBtn;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
initializeViews();
mAuth = FirebaseAuth.getInstance();
mSignInBtn.setOnClickListener(signInClickListener);
mSignUpBtn.setOnClickListener(signUpClickListener);
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) startUserActivity();
}
private void initializeViews() {
mEmailEt = findViewById(R.id.email);
mPasswordEt = findViewById(R.id.password);
mSignInBtn = findViewById(R.id.btn_sign_in);
mSignUpBtn = findViewById(R.id.btn_sign_up);
}
private View.OnClickListener signInClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
};
private View.OnClickListener signUpClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
signUp();
}
};
private void signIn() {
String email = mEmailEt.getText().toString();
String password = mPasswordEt.getText().toString();
if(fieldsNotEmpty(email, password)) {
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithEmail:success");
startUserActivity();
} else {
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void signUp() {
String email = mEmailEt.getText().toString();
String password = mPasswordEt.getText().toString();
if(fieldsNotEmpty(email, password)) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "createUserWithEmail:success");
startUserActivity();
} else {
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void startUserActivity() {
Intent userActivityIntent = new Intent(SignInActivity.this, UserActivity.class);
startActivity(userActivityIntent);
}
private boolean fieldsNotEmpty(String email, String password) {
if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(SignInActivity.this, "Please, enter email and password", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
}
package com.paktalin.vocabularynotebook
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.google.android.gms.signin.SignIn
import com.google.firebase.auth.FirebaseAuth
class SignInActivity : AppCompatActivity() {
private var mEmailEt: EditText? = null
private var mPasswordEt: EditText? = null
private var mSignInBtn: Button? = null
private var mSignUpBtn: Button? = null
private var mAuth: FirebaseAuth? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sign_in)
initializeViews()
mAuth = FirebaseAuth.getInstance()
mSignInBtn!!.setOnClickListener({ signIn() })
mSignUpBtn!!.setOnClickListener({ signUp() })
}
override fun onStart() {
super.onStart()
val currentUser = mAuth!!.currentUser
if (currentUser != null) startUserActivity()
}
private fun initializeViews() {
mEmailEt = findViewById(R.id.email)
mPasswordEt = findViewById(R.id.password)
mSignInBtn = findViewById(R.id.btn_sign_in)
mSignUpBtn = findViewById(R.id.btn_sign_up)
}
private fun signIn() {
val email = mEmailEt!!.text.toString()
val password = mPasswordEt!!.text.toString()
if (fieldsNotEmpty(email, password)) {
mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) startUserActivity()
else {
Log.w(TAG, "signInWithEmail:failure", task.exception)
Toast.makeText(this@SignInActivity, "Authentication failed.",
Toast.LENGTH_SHORT).show()
}
}
}
}
private fun signUp() {
val email = mEmailEt!!.text.toString()
val password = mPasswordEt!!.text.toString()
if (fieldsNotEmpty(email, password)) {
mAuth!!.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) startUserActivity()
else {
Log.w(TAG, "createUserWithEmail:failure", task.exception)
Toast.makeText(this@SignInActivity, "Authentication failed.",
Toast.LENGTH_SHORT).show()
}
}
}
}
private fun startUserActivity() {
Log.d(TAG, "Signed in successfully");
val userActivityIntent = Intent(this@SignInActivity, UserActivity::class.java)
startActivity(userActivityIntent)
}
private fun fieldsNotEmpty(email: String, password: String): Boolean {
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(this@SignInActivity, "Please, enter email and password", Toast.LENGTH_SHORT).show()
return false
}
return true
}
companion object {
private val TAG = "VN/" + SignIn::class.simpleName
}
}
\ No newline at end of file
...@@ -7,7 +7,7 @@ import android.support.v7.app.AppCompatActivity; ...@@ -7,7 +7,7 @@ import android.support.v7.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseAuth;
public class UserActivity extends AppCompatActivity { public class UserActivity extends AppCompatActivity {
private static final String TAG = "VN/" + SignInActivity.class.getSimpleName(); private static final String TAG = "VN/" + UserActivity.class.getSimpleName();
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
......
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript { buildscript {
ext.kotlin_version = '1.2.30'
repositories { repositories {
google() google()
jcenter() jcenter()
mavenCentral()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.1.4' classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.google.gms:google-services:4.1.0' classpath 'com.google.gms:google-services:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment