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
9e2c6217
Commit
9e2c6217
authored
Sep 02, 2020
by
Ahmad Abi Mulya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix login bug get response from API)
parent
d09b3daf
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
80 additions
and
49 deletions
+80
-49
build.gradle
app/build.gradle
+2
-3
CartActivity.java
.../com/example/yourcashiertest/activities/CartActivity.java
+1
-1
LoginActivity.java
...com/example/yourcashiertest/activities/LoginActivity.java
+41
-35
ApiClient.java
...n/java/com/example/yourcashiertest/clients/ApiClient.java
+29
-6
ResponseLogin.java
...ava/com/example/yourcashiertest/models/ResponseLogin.java
+4
-3
UserService.java
...ava/com/example/yourcashiertest/services/UserService.java
+3
-1
No files found.
app/build.gradle
View file @
9e2c6217
...
@@ -57,10 +57,9 @@ dependencies {
...
@@ -57,10 +57,9 @@ dependencies {
implementation
'com.squareup.retrofit2:converter-gson:2.9.0'
implementation
'com.squareup.retrofit2:converter-gson:2.9.0'
implementation
'com.google.code.gson:gson:2.8.6'
implementation
'com.google.code.gson:gson:2.8.6'
implementation
'com.facebook.stetho:stetho:1.5.1'
implementation
'com.facebook.stetho:stetho:1.5.1'
implementation
'com.squareup.okhttp3:logging-interceptor:4.1.1'
testImplementation
'junit:junit:4.13'
testImplementation
'junit:junit:4.13'
implementation
'com.squareup.retrofit2:converter-scalars:2.3.0'
androidTestImplementation
'androidx.test.ext:junit:1.1.1'
androidTestImplementation
'androidx.test.ext:junit:1.1.1'
androidTestImplementation
'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation
'androidx.test.espresso:espresso-core:3.2.0'
}
}
\ No newline at end of file
app/src/main/java/com/example/yourcashiertest/activities/CartActivity.java
View file @
9e2c6217
...
@@ -69,7 +69,7 @@ public class CartActivity extends AppCompatActivity {
...
@@ -69,7 +69,7 @@ public class CartActivity extends AppCompatActivity {
public
void
onIncrease
(
Cart
cart
)
{
public
void
onIncrease
(
Cart
cart
)
{
Log
.
d
(
"stock1"
,
""
+
product
.
getQuantity
());
Log
.
d
(
"stock1"
,
""
+
product
.
getQuantity
());
Log
.
d
(
"qty1"
,
""
+
cart
.
getQty
());
Log
.
d
(
"qty1"
,
""
+
cart
.
getQty
());
if
(
product
.
getQuantity
()
<
cart
.
getQty
()){
if
(
product
.
getQuantity
()
>
cart
.
getQty
()){
Log
.
d
(
"stock2"
,
""
+
product
.
getQuantity
());
Log
.
d
(
"stock2"
,
""
+
product
.
getQuantity
());
Log
.
d
(
"qty2"
,
""
+
cart
.
getQty
());
Log
.
d
(
"qty2"
,
""
+
cart
.
getQty
());
cart
.
setQty
(
cart
.
getQty
()
+
1
);
cart
.
setQty
(
cart
.
getQty
()
+
1
);
...
...
app/src/main/java/com/example/yourcashiertest/activities/LoginActivity.java
View file @
9e2c6217
...
@@ -13,10 +13,14 @@ import android.widget.Toast;
...
@@ -13,10 +13,14 @@ import android.widget.Toast;
import
com.example.yourcashiertest.R
;
import
com.example.yourcashiertest.R
;
import
com.example.yourcashiertest.clients.ApiClient
;
import
com.example.yourcashiertest.clients.ApiClient
;
import
com.example.yourcashiertest.databinding.ActivityLoginBinding
;
import
com.example.yourcashiertest.databinding.ActivityLoginBinding
;
import
com.example.yourcashiertest.models.Data
;
import
com.example.yourcashiertest.models.Login
;
import
com.example.yourcashiertest.models.Login
;
import
com.example.yourcashiertest.models.ResponseLogin
;
import
com.example.yourcashiertest.models.ResponseLogin
;
import
com.example.yourcashiertest.services.UserService
;
import
com.example.yourcashiertest.services.UserService
;
import
com.example.yourcashiertest.viewmodels.UserViewModel
;
import
com.example.yourcashiertest.viewmodels.UserViewModel
;
import
com.google.gson.Gson
;
import
java.io.IOException
;
import
retrofit2.Call
;
import
retrofit2.Call
;
import
retrofit2.Callback
;
import
retrofit2.Callback
;
...
@@ -32,29 +36,31 @@ public class LoginActivity extends AppCompatActivity {
...
@@ -32,29 +36,31 @@ public class LoginActivity extends AppCompatActivity {
super
.
onCreate
(
savedInstanceState
);
super
.
onCreate
(
savedInstanceState
);
binding
=
DataBindingUtil
.
setContentView
(
this
,
R
.
layout
.
activity_login
);
binding
=
DataBindingUtil
.
setContentView
(
this
,
R
.
layout
.
activity_login
);
prefManager
=
new
PrefManager
(
this
);
prefManager
=
new
PrefManager
(
this
);
Log
.
d
(
"login1"
,
""
+
prefManager
.
isFirstLogin
());
Log
.
d
(
"login1"
,
""
+
prefManager
.
isFirstLogin
());
if
(!
prefManager
.
isFirstLogin
())
{
if
(!
prefManager
.
isFirstLogin
())
{
Log
.
d
(
"login2"
,
""
+
prefManager
.
isFirstLogin
());
Log
.
d
(
"login2"
,
""
+
prefManager
.
isFirstLogin
());
checkSession
();
checkSession
();
finish
();
finish
();
}
}
viewModel
=
new
ViewModelProvider
(
this
,
new
ViewModelProvider
.
NewInstanceFactory
()).
get
(
UserViewModel
.
class
);
viewModel
=
new
ViewModelProvider
(
this
,
new
ViewModelProvider
.
NewInstanceFactory
()).
get
(
UserViewModel
.
class
);
binding
.
btnLogin
.
setOnClickListener
(
view
->
{
binding
.
btnLogin
.
setOnClickListener
(
view
->
{
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
)
{
Toast
.
makeText
(
getApplicationContext
(),
"Please input Username and Password"
,
Toast
.
LENGTH_SHORT
).
show
();
Toast
.
makeText
(
getApplicationContext
(),
"Please input Username and Password"
,
Toast
.
LENGTH_SHORT
).
show
();
}
else
{
}
else
{
binding
.
setLoading
(
true
);
binding
.
setLoading
(
true
);
viewModel
.
setListUser
();
launchMain
();
launchMain
();
}
}
});
});
}
}
public
void
tvForgetPassword
(
View
view
)
{
public
void
tvForgetPassword
(
View
view
)
{
startActivity
(
new
Intent
(
LoginActivity
.
this
,
ForgetPassword
.
class
));
startActivity
(
new
Intent
(
LoginActivity
.
this
,
ForgetPassword
.
class
));
}
}
public
void
tvSignUpHere
(
View
view
)
{
public
void
tvSignUpHere
(
View
view
)
{
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
login
=
new
Login
();
login
.
setEmail
(
binding
.
etUsername
.
getText
().
toString
());
login
.
setEmail
(
binding
.
etUsername
.
getText
().
toString
());
...
@@ -62,40 +68,40 @@ public class LoginActivity extends AppCompatActivity {
...
@@ -62,40 +68,40 @@ public class LoginActivity extends AppCompatActivity {
requestLogin
(
login
);
requestLogin
(
login
);
}
}
public
void
requestLogin
(
Login
login
){
public
void
requestLogin
(
Login
login
)
{
try
{
ApiClient
.
client
(
UserService
.
class
,
UserViewModel
.
BASE_URL
).
loginUser
(
login
).
enqueue
(
new
Callback
<
ResponseLogin
>()
{
ApiClient
.
client
(
UserService
.
class
,
UserViewModel
.
BASE_URL
).
loginUser
(
login
).
enqueue
(
new
Callback
<
ResponseLogin
>()
{
@Override
@Override
public
void
onResponse
(
Call
<
ResponseLogin
>
call
,
Response
<
ResponseLogin
>
response
)
{
public
void
onResponse
(
Call
<
ResponseLogin
>
call
,
Response
<
ResponseLogin
>
response
)
{
if
(
response
.
body
()!=
null
)
{
if
(
response
.
code
()
<
400
)
{
Log
.
d
(
"body"
,
String
.
valueOf
(
response
.
body
().
getData
().
getFullName
()
));
// Log.d("body", "" + response.errorBody().toString(
));
prefManager
.
setFirstLogin
(
false
);
prefManager
.
setFirstLogin
(
false
);
binding
.
setLoading
(
false
);
prefManager
.
setDataUser
(
response
.
body
().
getData
().
getFullName
()
);
Toast
.
makeText
(
getApplicationContext
(),
response
.
body
().
getMessages
(),
Toast
.
LENGTH_LONG
).
show
();
Toast
.
makeText
(
getApplicationContext
(),
response
.
body
().
getMessages
(),
Toast
.
LENGTH_LONG
).
show
();
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
(
response
.
body
().
getData
().
getFullName
());
finish
();
finish
();
}
else
if
(
response
.
code
()
==
302
){
}
else
{
Toast
.
makeText
(
getApplicationContext
(),
"Please verify your account!"
,
Toast
.
LENGTH_LONG
).
show
();
String
errorJson
=
null
;
binding
.
setLoading
(
false
);
try
{
}
else
{
errorJson
=
response
.
errorBody
().
string
();
Toast
.
makeText
(
getApplicationContext
(),
response
.
body
().
getMessages
(),
Toast
.
LENGTH_LONG
).
show
();
Log
.
d
(
"response"
,
""
+
errorJson
);
binding
.
setLoading
(
false
);
ResponseLogin
responseLogin
=
new
Gson
().
fromJson
(
errorJson
,
ResponseLogin
.
class
);
Toast
.
makeText
(
getApplicationContext
(),
responseLogin
.
getMessages
(),
Toast
.
LENGTH_LONG
).
show
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
binding
.
setLoading
(
false
);
}
@Override
@Override
public
void
onFailure
(
Call
<
ResponseLogin
>
call
,
Throwable
t
)
{
public
void
onFailure
(
Call
<
ResponseLogin
>
call
,
Throwable
t
)
{
t
.
printStackTrace
();
t
.
printStackTrace
();
}
}
});
});
}
catch
(
Exception
e
){
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
));
finish
();
finish
();
...
...
app/src/main/java/com/example/yourcashiertest/clients/ApiClient.java
View file @
9e2c6217
package
com
.
example
.
yourcashiertest
.
clients
;
package
com
.
example
.
yourcashiertest
.
clients
;
import
android.util.Log
;
import
com.example.yourcashiertest.BuildConfig
;
import
com.google.gson.GsonBuilder
;
import
com.google.gson.GsonBuilder
;
import
java.util.concurrent.TimeUnit
;
import
okhttp3.OkHttpClient
;
import
okhttp3.Request
;
import
okhttp3.logging.HttpLoggingInterceptor
;
import
retrofit2.Retrofit
;
import
retrofit2.Retrofit
;
import
retrofit2.converter.gson.GsonConverterFactory
;
import
retrofit2.converter.gson.GsonConverterFactory
;
import
retrofit2.converter.scalars.ScalarsConverterFactory
;
public
class
ApiClient
{
public
class
ApiClient
{
public
static
<
T
>
T
client
(
Class
<
T
>
service
,
String
url
)
{
public
static
<
T
>
T
client
(
Class
<
T
>
service
,
String
url
)
{
OkHttpClient
.
Builder
okHttp
=
new
OkHttpClient
().
newBuilder
();
okHttp
.
connectTimeout
(
60
,
TimeUnit
.
SECONDS
);
okHttp
.
writeTimeout
(
60
,
TimeUnit
.
SECONDS
);
okHttp
.
readTimeout
(
60
,
TimeUnit
.
SECONDS
);
HttpLoggingInterceptor
interceptor
=
new
HttpLoggingInterceptor
(
s
->
Log
.
e
(
"API-LOG"
,
s
));
interceptor
.
level
(
HttpLoggingInterceptor
.
Level
.
BODY
);
if
(
BuildConfig
.
DEBUG
)
okHttp
.
addInterceptor
(
interceptor
);
okHttp
.
addInterceptor
(
chain
->
{
Request
request
=
chain
.
request
();
Request
newReq
=
request
.
newBuilder
()
.
addHeader
(
"Content-Type"
,
"application/json"
)
.
addHeader
(
"Accept"
,
"application/json"
)
.
build
();
return
chain
.
proceed
(
newReq
);
});
OkHttpClient
client
=
okHttp
.
build
();
Retrofit
retrofit
=
new
Retrofit
.
Builder
()
Retrofit
retrofit
=
new
Retrofit
.
Builder
()
.
baseUrl
(
url
)
.
baseUrl
(
url
)
.
addConverterFactory
(
.
client
(
client
)
GsonConverterFactory
.
create
(
.
addConverterFactory
(
GsonConverterFactory
.
create
(
new
GsonBuilder
().
setLenient
().
create
()
new
GsonBuilder
().
setLenient
().
create
())
)
).
addConverterFactory
(
ScalarsConverterFactory
.
create
())
)
.
build
();
.
build
();
return
retrofit
.
create
(
service
);
return
retrofit
.
create
(
service
);
}
}
}
}
app/src/main/java/com/example/yourcashiertest/models/ResponseLogin.java
View file @
9e2c6217
...
@@ -5,13 +5,13 @@ import com.google.gson.annotations.SerializedName;
...
@@ -5,13 +5,13 @@ import com.google.gson.annotations.SerializedName;
public
class
ResponseLogin
{
public
class
ResponseLogin
{
@SerializedName
(
"data"
)
@SerializedName
(
"data"
)
p
rivate
Data
data
;
p
ublic
Data
data
;
@SerializedName
(
"messages"
)
@SerializedName
(
"messages"
)
p
rivate
String
messages
;
p
ublic
String
messages
;
@SerializedName
(
"status"
)
@SerializedName
(
"status"
)
p
rivate
boolean
status
;
p
ublic
boolean
status
;
public
void
setData
(
Data
data
){
public
void
setData
(
Data
data
){
this
.
data
=
data
;
this
.
data
=
data
;
...
@@ -36,4 +36,5 @@ public class ResponseLogin{
...
@@ -36,4 +36,5 @@ public class ResponseLogin{
public
boolean
isStatus
(){
public
boolean
isStatus
(){
return
status
;
return
status
;
}
}
}
}
\ No newline at end of file
app/src/main/java/com/example/yourcashiertest/services/UserService.java
View file @
9e2c6217
package
com
.
example
.
yourcashiertest
.
services
;
package
com
.
example
.
yourcashiertest
.
services
;
import
com.example.yourcashiertest.models.Data
;
import
com.example.yourcashiertest.models.Login
;
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.ResponseLogin
;
...
@@ -7,6 +8,7 @@ import com.example.yourcashiertest.models.ResponseUser;
...
@@ -7,6 +8,7 @@ import com.example.yourcashiertest.models.ResponseUser;
import
com.example.yourcashiertest.models.User
;
import
com.example.yourcashiertest.models.User
;
import
retrofit2.Call
;
import
retrofit2.Call
;
import
retrofit2.Response
;
import
retrofit2.http.Body
;
import
retrofit2.http.Body
;
import
retrofit2.http.GET
;
import
retrofit2.http.GET
;
import
retrofit2.http.Headers
;
import
retrofit2.http.Headers
;
...
@@ -23,7 +25,7 @@ public interface UserService {
...
@@ -23,7 +25,7 @@ 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"
)
//
@Headers("Content-Type: application/json")
@POST
(
"v1/login"
)
@POST
(
"v1/login"
)
Call
<
ResponseLogin
>
loginUser
(
@Body
Login
login
);
Call
<
ResponseLogin
>
loginUser
(
@Body
Login
login
);
...
...
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