Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
Y
Your Cashier
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ahmad Abi Mulya
Your Cashier
Commits
e4777055
Commit
e4777055
authored
Sep 01, 2020
by
Wahyu Wibowo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
create login with api
parent
1ec17828
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
218 additions
and
37 deletions
+218
-37
LoginActivity.java
...com/example/yourcashiertest/activities/LoginActivity.java
+30
-11
RegisterActivity.java
.../example/yourcashiertest/activities/RegisterActivity.java
+16
-19
Data.java
...rc/main/java/com/example/yourcashiertest/models/Data.java
+83
-0
Login.java
...c/main/java/com/example/yourcashiertest/models/Login.java
+28
-0
ResponseLogin.java
...ava/com/example/yourcashiertest/models/ResponseLogin.java
+39
-0
UserService.java
...ava/com/example/yourcashiertest/services/UserService.java
+6
-2
UserViewModel.java
...com/example/yourcashiertest/viewmodels/UserViewModel.java
+16
-5
No files found.
app/src/main/java/com/example/yourcashiertest/activities/LoginActivity.java
View file @
e4777055
...
@@ -11,12 +11,19 @@ import android.view.View;
...
@@ -11,12 +11,19 @@ import android.view.View;
import
android.widget.Toast
;
import
android.widget.Toast
;
import
com.example.yourcashiertest.R
;
import
com.example.yourcashiertest.R
;
import
com.example.yourcashiertest.clients.ApiClient
;
import
com.example.yourcashiertest.databinding.ActivityLoginBinding
;
import
com.example.yourcashiertest.databinding.ActivityLoginBinding
;
import
com.example.yourcashiertest.models.Login
;
import
com.example.yourcashiertest.models.ResponseLogin
;
import
com.example.yourcashiertest.services.UserService
;
import
com.example.yourcashiertest.viewmodels.UserViewModel
;
import
com.example.yourcashiertest.viewmodels.UserViewModel
;
import
retrofit2.Call
;
import
retrofit2.Callback
;
import
retrofit2.Response
;
public
class
LoginActivity
extends
AppCompatActivity
{
public
class
LoginActivity
extends
AppCompatActivity
{
ActivityLoginBinding
binding
;
ActivityLoginBinding
binding
;
public
static
final
String
DATA_LOGIN
=
"DATA_LOGIN"
;
private
PrefManager
prefManager
;
private
PrefManager
prefManager
;
UserViewModel
viewModel
;
UserViewModel
viewModel
;
...
@@ -44,7 +51,6 @@ public class LoginActivity extends AppCompatActivity {
...
@@ -44,7 +51,6 @@ public class LoginActivity extends AppCompatActivity {
binding
.
setLoading
(
true
);
binding
.
setLoading
(
true
);
viewModel
.
setListUser
();
viewModel
.
setListUser
();
launchMain
();
launchMain
();
finish
();
}
}
});
});
}
}
...
@@ -55,28 +61,41 @@ public class LoginActivity extends AppCompatActivity {
...
@@ -55,28 +61,41 @@ public class LoginActivity extends AppCompatActivity {
startActivity
(
new
Intent
(
LoginActivity
.
this
,
RegisterActivity
.
class
));
startActivity
(
new
Intent
(
LoginActivity
.
this
,
RegisterActivity
.
class
));
}
}
public
void
launchMain
()
{
public
void
launchMain
()
{
Login
login
=
new
Login
();
login
.
setEmail
(
binding
.
etUsername
.
getText
().
toString
());
login
.
setPassword
(
binding
.
etPassword
.
getText
().
toString
());
requestLogin
(
login
);
}
public
void
requestLogin
(
Login
login
){
try
{
try
{
viewModel
.
getListUser
().
observe
(
this
,
users
->
{
ApiClient
.
client
(
UserService
.
class
,
UserViewModel
.
BASE_URL
).
loginUser
(
login
).
enqueue
(
new
Callback
<
ResponseLogin
>()
{
for
(
int
i
=
0
;
i
<
users
.
size
();
i
++)
{
@Override
if
(
binding
.
etUsername
.
getText
().
toString
().
equals
(
users
.
get
(
i
).
getEmail
())
public
void
onResponse
(
Call
<
ResponseLogin
>
call
,
Response
<
ResponseLogin
>
response
)
{
&&
binding
.
etPassword
.
getText
().
toString
().
equals
(
users
.
get
(
i
).
getPassword
()))
{
if
(
response
.
code
()
>=
200
&
response
.
code
()
<=
299
){
Log
.
d
(
"body"
,
String
.
valueOf
(
response
.
body
().
getData
().
getFullName
()));
prefManager
.
setFirstLogin
(
false
);
prefManager
.
setFirstLogin
(
false
);
binding
.
setLoading
(
false
);
binding
.
setLoading
(
false
);
startActivity
(
new
Intent
(
LoginActivity
.
this
,
MainActivity
.
class
)
startActivity
(
new
Intent
(
LoginActivity
.
this
,
MainActivity
.
class
)
.
addFlags
(
Intent
.
FLAG_ACTIVITY_CLEAR_TASK
).
addFlags
(
Intent
.
FLAG_ACTIVITY_CLEAR_TOP
));
.
addFlags
(
Intent
.
FLAG_ACTIVITY_CLEAR_TASK
).
addFlags
(
Intent
.
FLAG_ACTIVITY_CLEAR_TOP
));
prefManager
.
setDataUser
(
users
.
get
(
i
).
getFullName
());
prefManager
.
setDataUser
(
response
.
body
().
getData
(
).
getFullName
());
finish
();
finish
();
return
;
}
else
{
Toast
.
makeText
(
getApplicationContext
(),
"Email or password wrong"
,
Toast
.
LENGTH_LONG
).
show
();
binding
.
setLoading
(
false
);
}
}
}
}
Toast
.
makeText
(
LoginActivity
.
this
,
"User Not Found!"
,
Toast
.
LENGTH_LONG
).
show
();
binding
.
setLoading
(
false
);
@Override
return
;
public
void
onFailure
(
Call
<
ResponseLogin
>
call
,
Throwable
t
)
{
t
.
printStackTrace
();
}
});
});
}
catch
(
Exception
e
){
}
catch
(
Exception
e
){
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
}
}
public
void
checkSession
(){
public
void
checkSession
(){
prefManager
.
setFirstLogin
(
false
);
prefManager
.
setFirstLogin
(
false
);
startActivity
(
new
Intent
(
LoginActivity
.
this
,
MainActivity
.
class
).
addFlags
(
Intent
.
FLAG_ACTIVITY_CLEAR_TASK
).
addFlags
(
Intent
.
FLAG_ACTIVITY_CLEAR_TOP
));
startActivity
(
new
Intent
(
LoginActivity
.
this
,
MainActivity
.
class
).
addFlags
(
Intent
.
FLAG_ACTIVITY_CLEAR_TASK
).
addFlags
(
Intent
.
FLAG_ACTIVITY_CLEAR_TOP
));
...
...
app/src/main/java/com/example/yourcashiertest/activities/RegisterActivity.java
View file @
e4777055
...
@@ -32,9 +32,7 @@ public class RegisterActivity extends AppCompatActivity {
...
@@ -32,9 +32,7 @@ public class RegisterActivity extends AppCompatActivity {
viewModel
=
new
ViewModelProvider
(
this
,
new
ViewModelProvider
.
NewInstanceFactory
()).
get
(
UserViewModel
.
class
);
viewModel
=
new
ViewModelProvider
(
this
,
new
ViewModelProvider
.
NewInstanceFactory
()).
get
(
UserViewModel
.
class
);
binding
.
btnRegister
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
binding
.
btnRegister
.
setOnClickListener
(
v
->
{
@Override
public
void
onClick
(
View
v
)
{
if
(
binding
.
etUsername
.
getText
().
toString
().
length
()
==
0
||
binding
.
etPassword
.
getText
().
toString
().
length
()
==
0
if
(
binding
.
etUsername
.
getText
().
toString
().
length
()
==
0
||
binding
.
etPassword
.
getText
().
toString
().
length
()
==
0
||
binding
.
etEmail
.
getText
().
toString
().
length
()
==
0
||
binding
.
etPhoneNumber
.
getText
().
toString
().
length
()
==
0
){
||
binding
.
etEmail
.
getText
().
toString
().
length
()
==
0
||
binding
.
etPhoneNumber
.
getText
().
toString
().
length
()
==
0
){
Toast
.
makeText
(
getApplicationContext
(),
"Please input All Required Data"
,
Toast
.
LENGTH_SHORT
).
show
();
Toast
.
makeText
(
getApplicationContext
(),
"Please input All Required Data"
,
Toast
.
LENGTH_SHORT
).
show
();
...
@@ -51,7 +49,6 @@ public class RegisterActivity extends AppCompatActivity {
...
@@ -51,7 +49,6 @@ public class RegisterActivity extends AppCompatActivity {
Toast
.
makeText
(
getApplicationContext
(),
"Register Was Successful"
,
Toast
.
LENGTH_LONG
).
show
();
Toast
.
makeText
(
getApplicationContext
(),
"Register Was Successful"
,
Toast
.
LENGTH_LONG
).
show
();
startActivity
(
new
Intent
(
getApplicationContext
(),
LoginActivity
.
class
));
startActivity
(
new
Intent
(
getApplicationContext
(),
LoginActivity
.
class
));
}
}
}
});
});
}
}
...
...
app/src/main/java/com/example/yourcashiertest/models/Data.java
0 → 100644
View file @
e4777055
package
com
.
example
.
yourcashiertest
.
models
;
import
com.google.gson.annotations.SerializedName
;
public
class
Data
{
@SerializedName
(
"password"
)
private
String
password
;
@SerializedName
(
"full_name"
)
private
String
fullName
;
@SerializedName
(
"updated_at"
)
private
String
updatedAt
;
@SerializedName
(
"created_at"
)
private
String
createdAt
;
@SerializedName
(
"phone_number"
)
private
String
phoneNumber
;
@SerializedName
(
"id"
)
private
int
id
;
@SerializedName
(
"email"
)
private
String
email
;
public
void
setPassword
(
String
password
){
this
.
password
=
password
;
}
public
String
getPassword
(){
return
password
;
}
public
void
setFullName
(
String
fullName
){
this
.
fullName
=
fullName
;
}
public
String
getFullName
(){
return
fullName
;
}
public
void
setUpdatedAt
(
String
updatedAt
){
this
.
updatedAt
=
updatedAt
;
}
public
String
getUpdatedAt
(){
return
updatedAt
;
}
public
void
setCreatedAt
(
String
createdAt
){
this
.
createdAt
=
createdAt
;
}
public
String
getCreatedAt
(){
return
createdAt
;
}
public
void
setPhoneNumber
(
String
phoneNumber
){
this
.
phoneNumber
=
phoneNumber
;
}
public
String
getPhoneNumber
(){
return
phoneNumber
;
}
public
void
setId
(
int
id
){
this
.
id
=
id
;
}
public
int
getId
(){
return
id
;
}
public
void
setEmail
(
String
email
){
this
.
email
=
email
;
}
public
String
getEmail
(){
return
email
;
}
}
\ No newline at end of file
app/src/main/java/com/example/yourcashiertest/models/Login.java
0 → 100644
View file @
e4777055
package
com
.
example
.
yourcashiertest
.
models
;
import
com.google.gson.annotations.SerializedName
;
public
class
Login
{
@SerializedName
(
"email"
)
private
String
email
;
@SerializedName
(
"password"
)
private
String
password
;
public
String
getEmail
(){
return
email
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
String
getPassword
(){
return
password
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
}
\ No newline at end of file
app/src/main/java/com/example/yourcashiertest/models/ResponseLogin.java
0 → 100644
View file @
e4777055
package
com
.
example
.
yourcashiertest
.
models
;
import
com.google.gson.annotations.SerializedName
;
public
class
ResponseLogin
{
@SerializedName
(
"data"
)
private
Data
data
;
@SerializedName
(
"messages"
)
private
String
messages
;
@SerializedName
(
"status"
)
private
boolean
status
;
public
void
setData
(
Data
data
){
this
.
data
=
data
;
}
public
Data
getData
(){
return
data
;
}
public
void
setMessages
(
String
messages
){
this
.
messages
=
messages
;
}
public
String
getMessages
(){
return
messages
;
}
public
void
setStatus
(
boolean
status
){
this
.
status
=
status
;
}
public
boolean
isStatus
(){
return
status
;
}
}
\ No newline at end of file
app/src/main/java/com/example/yourcashiertest/services/UserService.java
View file @
e4777055
package
com
.
example
.
yourcashiertest
.
services
;
package
com
.
example
.
yourcashiertest
.
services
;
import
com.example.yourcashiertest.models.Login
;
import
com.example.yourcashiertest.models.Password
;
import
com.example.yourcashiertest.models.Password
;
import
com.example.yourcashiertest.models.ResponseLogin
;
import
com.example.yourcashiertest.models.ResponseUser
;
import
com.example.yourcashiertest.models.ResponseUser
;
import
com.example.yourcashiertest.models.User
;
import
com.example.yourcashiertest.models.User
;
import
java.util.List
;
import
retrofit2.Call
;
import
retrofit2.Call
;
import
retrofit2.http.Body
;
import
retrofit2.http.Body
;
import
retrofit2.http.GET
;
import
retrofit2.http.GET
;
...
@@ -23,6 +23,10 @@ public interface UserService {
...
@@ -23,6 +23,10 @@ public interface UserService {
@POST
(
"v1/customer"
)
@POST
(
"v1/customer"
)
Call
<
User
>
registerUser
(
@Body
User
user
);
Call
<
User
>
registerUser
(
@Body
User
user
);
@Headers
(
"Content-Type: application/json"
)
@POST
(
"v1/login"
)
Call
<
ResponseLogin
>
loginUser
(
@Body
Login
login
);
@Headers
(
"Content-Type: application/json"
)
@Headers
(
"Content-Type: application/json"
)
@PUT
(
"v1/change_password/{id}"
)
@PUT
(
"v1/change_password/{id}"
)
Call
<
Password
>
editPassword
(
@Path
(
"id"
)
int
id
,
@Body
Password
password
);
Call
<
Password
>
editPassword
(
@Path
(
"id"
)
int
id
,
@Body
Password
password
);
...
...
app/src/main/java/com/example/yourcashiertest/viewmodels/UserViewModel.java
View file @
e4777055
...
@@ -6,11 +6,11 @@ import androidx.lifecycle.MutableLiveData;
...
@@ -6,11 +6,11 @@ import androidx.lifecycle.MutableLiveData;
import
androidx.lifecycle.ViewModel
;
import
androidx.lifecycle.ViewModel
;
import
com.example.yourcashiertest.clients.ApiClient
;
import
com.example.yourcashiertest.clients.ApiClient
;
import
com.example.yourcashiertest.models.Login
;
import
com.example.yourcashiertest.models.Password
;
import
com.example.yourcashiertest.models.Password
;
import
com.example.yourcashiertest.models.ResponseUser
;
import
com.example.yourcashiertest.models.ResponseUser
;
import
com.example.yourcashiertest.models.User
;
import
com.example.yourcashiertest.models.User
;
import
com.example.yourcashiertest.services.UserService
;
import
com.example.yourcashiertest.services.UserService
;
import
com.google.gson.Gson
;
import
java.util.List
;
import
java.util.List
;
...
@@ -21,7 +21,7 @@ import retrofit2.Response;
...
@@ -21,7 +21,7 @@ import retrofit2.Response;
public
class
UserViewModel
extends
ViewModel
{
public
class
UserViewModel
extends
ViewModel
{
MutableLiveData
<
List
<
User
>>
listUser
=
new
MutableLiveData
<>();
MutableLiveData
<
List
<
User
>>
listUser
=
new
MutableLiveData
<>();
public
final
String
BASE_URL
=
"https://your-cashier.herokuapp.com/api/"
;
public
static
final
String
BASE_URL
=
"https://your-cashier.herokuapp.com/api/"
;
public
MutableLiveData
<
List
<
User
>>
getListUser
(){
public
MutableLiveData
<
List
<
User
>>
getListUser
(){
...
@@ -74,8 +74,19 @@ public class UserViewModel extends ViewModel {
...
@@ -74,8 +74,19 @@ public class UserViewModel extends ViewModel {
});
});
}
}
public
String
toStringPassword
(
String
password
){
// public void login(Login login){
return
"{ new_password:"
+
password
+
"}"
;
// ApiClient.client(UserService.class, BASE_URL)
}
// .loginUser(login).enqueue(new Callback<Login>() {
// @Override
// public void onResponse(Call<Login> call, Response<Login> response) {
//
// }
//
// @Override
// public void onFailure(Call<Login> call, Throwable t) {
//
// }
// });
// }
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment