Skip to content

Commit

Permalink
Sign In with API. Bundle total price
Browse files Browse the repository at this point in the history
  • Loading branch information
whyfoo committed Jan 4, 2022
1 parent e51d811 commit 2c75e03
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 30 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Trolleapp">
android:theme="@style/Theme.Trolleapp"
android:usesCleartextTraffic="true">
<activity
android:name=".ui.pay.ShoppingListActivity"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
Expand Down
26 changes: 14 additions & 12 deletions app/src/main/java/com/trolle/trolleapp/PayFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ package com.trolle.trolleapp

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.trolle.trolleapp.data.Item
import com.trolle.trolleapp.data.adapter.ItemAdapter
import com.trolle.trolleapp.data.adapter.ListItemAdapter
import com.trolle.trolleapp.data.viewmodel.MainViewModel
import com.trolle.trolleapp.databinding.FragmentPayBinding
import com.trolle.trolleapp.ui.pay.CheckoutActivity
import com.trolle.trolleapp.ui.side_menu.EditProfileActivity
import com.trolle.trolleapp.ui.signin.SignInActivity
import java.util.*

class PayFragment : Fragment() {
Expand All @@ -34,8 +28,8 @@ class PayFragment : Fragment() {
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentPayBinding.inflate(inflater, container, false)
return binding.root
Expand All @@ -45,6 +39,8 @@ class PayFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

var subTotal: Int = 0

adapter = ItemAdapter()
adapter.notifyDataSetChanged()
viewModel = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(MainViewModel::class.java)
Expand All @@ -57,22 +53,28 @@ class PayFragment : Fragment() {
Toast.makeText(context, "Searching items", Toast.LENGTH_SHORT).show()

viewModel.getSearchItems().observe(viewLifecycleOwner, {
if (it!=null){
if (it != null) {
showLoading(true)
var totalPrice = -100000000
for (i in 0..it.size-1){
for (i in 0..it.size - 1) {
totalPrice += it.get(i).id
}
adapter.setList(it)
showLoading(false)
totalPrice /= 10000
binding.textViewSubTotalPrice.text = getString(R.string.sub_total_price_dummy, totalPrice)

subTotal = totalPrice
}
})

binding.buttonCheckout.setOnClickListener {
requireActivity().run{
startActivity(Intent(context, CheckoutActivity::class.java))
val intent = Intent(requireActivity().applicationContext, CheckoutActivity::class.java).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra("price", subTotal)
requireActivity().applicationContext.startActivity(intent)

//startActivity(Intent(context, CheckoutActivity::class.java))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/trolle/trolleapp/data/SignInBody.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.trolle.trolleapp.data

data class SignInBody(
val email: String,
val username: String,
val password: String
)
12 changes: 12 additions & 0 deletions app/src/main/java/com/trolle/trolleapp/data/SignInResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.trolle.trolleapp.data

data class SignInResponse (
val status: String,
val message: String,
val data: SignInData
)

data class SignInData(
val id: Int,
val token: String
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.trolle.trolleapp.data.network.api

import com.trolle.trolleapp.data.ItemResponse
import com.trolle.trolleapp.data.SignInBody
import com.trolle.trolleapp.data.User
import com.trolle.trolleapp.data.*
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.*
Expand All @@ -16,7 +14,7 @@ interface Api {

@POST("users/login")
@Headers("Content-Type:application/json")
fun login(@Body info: SignInBody): Call<ResponseBody>
fun login(@Body info: SignInBody): Call<SignInResponse>

@POST("users")
@Headers("Content-Type:application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import retrofit2.create
import java.util.concurrent.TimeUnit

object RetrofitClient {
private const val BASE_URL = "https://api.github.com/"
private const val BASE_URL = "http://192.168.100.22:3000/"

val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class HomeSuccessActivity : AppCompatActivity() {
binding = ActivityHomeSuccessBinding.inflate(layoutInflater)
setContentView(binding.root)

val extras = this.intent.extras
val totalPrice = extras!!.getInt("totalPrice")

binding.textViewGrandTotalPrice.text = getString(R.string.sub_total_price_dummy, totalPrice)

binding.buttonBackToHome.setOnClickListener {
startActivity(Intent(this, HomeActivity::class.java))
finish()
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/trolle/trolleapp/ui/pay/CheckoutActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.trolle.trolleapp.ui.pay

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.trolle.trolleapp.R
import com.trolle.trolleapp.databinding.ActivityCheckoutBinding
import com.trolle.trolleapp.ui.home.HomeSuccessActivity
Expand All @@ -16,8 +16,18 @@ class CheckoutActivity : AppCompatActivity() {
binding = ActivityCheckoutBinding.inflate(layoutInflater)
setContentView(binding.root)

val extras = this.intent.extras
val price = extras!!.getInt("price")
val total = price+6500

binding.textViewSubTotalPrice.text = getString(R.string.sub_total_price_dummy, price)
binding.textViewGrandTotalPrice.text = getString(R.string.sub_total_price_dummy, total)

binding.buttonPay.setOnClickListener {
startActivity(Intent(this, HomeSuccessActivity::class.java))
val intent = Intent(this, HomeSuccessActivity::class.java)
intent.putExtra("totalPrice", total)
startActivity(intent)
// startActivity(Intent(this, HomeSuccessActivity::class.java))
finish()
}
}
Expand Down
27 changes: 18 additions & 9 deletions app/src/main/java/com/trolle/trolleapp/ui/signin/SignInActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import android.os.Handler
import android.os.Looper
import android.widget.Toast
import com.trolle.trolleapp.data.SignInBody
import com.trolle.trolleapp.data.SignInResponse
import com.trolle.trolleapp.data.network.api.RetrofitClient
import com.trolle.trolleapp.databinding.ActivitySignInBinding
import com.trolle.trolleapp.ui.home.HomeActivity
import com.trolle.trolleapp.ui.signup.SignUpActivity
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit

class SignInActivity : AppCompatActivity() {

Expand All @@ -37,8 +38,6 @@ class SignInActivity : AppCompatActivity() {
binding.textFieldPassword.setError("Password cannot be empty.")
} else {
signin(username, password)
startActivity(Intent(this, HomeActivity::class.java))
finish()
}
}

Expand All @@ -50,15 +49,25 @@ class SignInActivity : AppCompatActivity() {
private fun signin(username: String, password: String){
val retIn = RetrofitClient.apiInstance
val signInInfo = SignInBody(username, password)
retIn.login(signInInfo).enqueue(object : Callback<ResponseBody> {
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
Toast.makeText(applicationContext, t.message, Toast.LENGTH_SHORT).show()
retIn.login(signInInfo).enqueue(object : Callback<SignInResponse> {
override fun onFailure(call: Call<SignInResponse>, t: Throwable) {
Toast.makeText(applicationContext, "on Failure", Toast.LENGTH_SHORT).show()
}
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
override fun onResponse(call: Call<SignInResponse>, response: Response<SignInResponse>) {
if (response.code() == 200) {
Toast.makeText(applicationContext, "Login success!", Toast.LENGTH_SHORT).show()
Toast.makeText(applicationContext, response.body()!!.message, Toast.LENGTH_LONG).show()
startActivity(Intent(applicationContext, HomeActivity::class.java))
finish()
} else {
Toast.makeText(applicationContext, "Login failed!", Toast.LENGTH_SHORT).show()
var responseMessage = response.message()
response.errorBody()?.string()?.let {
val jsonObject = JSONObject(it)
val msg = jsonObject.getString("message")
val status = jsonObject.getString("status")
responseMessage = "$msg"
}
val errorMessage = "Error ${response.code()}: $responseMessage"
Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_SHORT).show()
}
}
})
Expand Down

0 comments on commit 2c75e03

Please sign in to comment.