diff --git a/app/src/main/java/me/gilo/wc/MainActivity.kt b/app/src/main/java/me/gilo/wc/MainActivity.kt index f9e3981..78f16b0 100644 --- a/app/src/main/java/me/gilo/wc/MainActivity.kt +++ b/app/src/main/java/me/gilo/wc/MainActivity.kt @@ -6,6 +6,7 @@ import android.support.v7.app.AppCompatActivity import me.gilo.raison.ui.user.onboarding.SignInActivity import me.gilo.raison.ui.user.onboarding.SignUpActivity import me.gilo.wc.ui.customer.BasicCustomerDetailsActivity +import me.gilo.wc.ui.customer.BillingAddressActivity import me.gilo.wc.ui.home.HomeActivity import me.gilo.wc.ui.product.ProductActivity @@ -17,7 +18,7 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - startActivity(Intent(baseContext, BasicCustomerDetailsActivity::class.java)) + startActivity(Intent(baseContext, BillingAddressActivity::class.java)) // val intent = Intent(baseContext, ProductActivity::class.java) // intent.putExtra("productId", 63) diff --git a/app/src/main/java/me/gilo/wc/repo/CustomerRepository.java b/app/src/main/java/me/gilo/wc/repo/CustomerRepository.java index 550ad22..f183cb1 100644 --- a/app/src/main/java/me/gilo/wc/repo/CustomerRepository.java +++ b/app/src/main/java/me/gilo/wc/repo/CustomerRepository.java @@ -1,6 +1,7 @@ package me.gilo.wc.repo; +import com.google.firebase.auth.FirebaseAuth; import me.gilo.wc.common.WooLiveData; import me.gilo.woodroid.Woocommerce; import me.gilo.woodroid.models.Customer; @@ -26,6 +27,15 @@ public class CustomerRepository { woocommerce.CustomerRepository().create(customer).enqueue(callBack); return callBack; } + + public WooLiveData> currentCustomer() { + final WooLiveData> callBack = new WooLiveData(); + CustomerFilter customerFilter = new CustomerFilter(); + customerFilter.setEmail(FirebaseAuth.getInstance().getCurrentUser().getEmail()); + + woocommerce.CustomerRepository().customers(customerFilter).enqueue(callBack); + return callBack; + } public WooLiveData customer(int id) { diff --git a/app/src/main/java/me/gilo/wc/ui/customer/BasicCustomerDetailsActivity.kt b/app/src/main/java/me/gilo/wc/ui/customer/BasicCustomerDetailsActivity.kt index d2c3ebe..025a3ce 100644 --- a/app/src/main/java/me/gilo/wc/ui/customer/BasicCustomerDetailsActivity.kt +++ b/app/src/main/java/me/gilo/wc/ui/customer/BasicCustomerDetailsActivity.kt @@ -4,6 +4,7 @@ import android.arch.lifecycle.Observer import android.content.Context import android.content.Intent import android.os.Bundle +import android.support.v4.content.ContextCompat.startActivity import android.widget.Toast import com.google.firebase.auth.FirebaseAuth import io.github.inflationx.viewpump.ViewPumpContextWrapper @@ -36,12 +37,46 @@ class BasicCustomerDetailsActivity : WooDroidActivity() { viewModel = getViewModel(CustomerViewModel::class.java) title = "Basic Details" - etEmail.setText(FirebaseAuth.getInstance().currentUser!!.email) + customer() flSave.setOnClickListener{save()} } + + private fun customer() { + viewModel.currentCustomer().observe(this, Observer { + response-> + when (response!!.status()){ + Status.LOADING ->{ + showLoading("Retrieve customer details", "This will only take a short while") + } + + Status.SUCCESS ->{ + stopShowingLoading() + var customer = response.data()[0] + + etEmail.setText(customer.email) + etFirstName.setText(customer.firstName) + etLastName.setText(customer.lastName) + etUsername.setText(customer.username) + + } + + Status.ERROR ->{ + stopShowingLoading() + Toast.makeText(baseContext, response.error().message.toString(), Toast.LENGTH_LONG).show() + } + + Status.EMPTY ->{ + stopShowingLoading() + } + + } + }) + + } + private fun save() { if (validates()) { val email = etEmail.text.toString() diff --git a/app/src/main/java/me/gilo/wc/ui/customer/BillingAddressActivity.kt b/app/src/main/java/me/gilo/wc/ui/customer/BillingAddressActivity.kt index 91386d4..9852081 100644 --- a/app/src/main/java/me/gilo/wc/ui/customer/BillingAddressActivity.kt +++ b/app/src/main/java/me/gilo/wc/ui/customer/BillingAddressActivity.kt @@ -1,16 +1,24 @@ package me.gilo.wc.ui.customer +import android.arch.lifecycle.Observer import android.content.Context +import android.content.Intent import android.os.Bundle +import android.widget.Toast import io.github.inflationx.viewpump.ViewPumpContextWrapper -import kotlinx.android.synthetic.main.activity_billing_address.* +import kotlinx.android.synthetic.main.customer_billing_address.* +import kotlinx.android.synthetic.main.drawer_filter.view.* import me.gilo.wc.R +import me.gilo.wc.common.Status import me.gilo.wc.ui.WooDroidActivity import me.gilo.wc.viewmodels.CustomerViewModel +import me.gilo.woodroid.models.BillingAddress +import me.gilo.woodroid.models.Customer class BillingAddressActivity : WooDroidActivity() { override lateinit var viewModel : CustomerViewModel + lateinit var customer : Customer override fun attachBaseContext(newBase: Context) { super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase)) @@ -23,6 +31,169 @@ class BillingAddressActivity : WooDroidActivity() { viewModel = getViewModel(CustomerViewModel::class.java) title = "Billing Address" + customer() + + } + + private fun customer() { + viewModel.currentCustomer().observe(this, Observer { + response-> + when (response!!.status()){ + Status.LOADING ->{ + showLoading("Retrieve customer details", "This will only take a short while") + } + + Status.SUCCESS ->{ + stopShowingLoading() + customer = response.data()[0] + + if (customer.billingAddress != null) { + etFirstName.setText(customer.billingAddress.firstName) + etLastName.setText(customer.billingAddress.firstName) + etCompany.setText(customer.billingAddress.company) + etStreetAddress.setText(customer.billingAddress.address1) + etStreetAddress2.setText(customer.billingAddress.address2) + etCity.setText(customer.billingAddress.city) + etState.setText(customer.billingAddress.state) + etZipcode.setText(customer.billingAddress.postcode) + etCountry.setText(customer.billingAddress.country) + etPhone.setText(customer.billingAddress.phone) + }else{ + etFirstName.setText(customer.firstName) + etLastName.setText(customer.firstName) + } + } + + Status.ERROR ->{ + stopShowingLoading() + Toast.makeText(baseContext, response.error().message.toString(), Toast.LENGTH_LONG).show() + } + + Status.EMPTY ->{ + stopShowingLoading() + } + + } + }) + + flSave.setOnClickListener{save()} + + } + + private fun save() { + if (validates()) { + val firstName = etFirstName.text.toString() + val lastName = etLastName.text.toString() + val company = etCompany.text.toString() + val streetAddress = etStreetAddress.text.toString() + val streetAddress2 = etStreetAddress2.text.toString() + val city = etCity.text.toString() + val state = etState.text.toString() + val zipcode = etZipcode.text.toString() + val country = etCountry.text.toString() + val phone = etPhone.text.toString() + + customer.billingAddress = BillingAddress() + + customer.billingAddress.firstName = firstName + customer.billingAddress.lastName = lastName + customer.billingAddress.company = company + customer.billingAddress.address1 = streetAddress + customer.billingAddress.address2 = streetAddress2 + + customer.billingAddress.city = city + customer.billingAddress.state = state + customer.billingAddress.postcode = zipcode + customer.billingAddress.country = country + customer.billingAddress.phone = phone + + viewModel.update(customer.id, customer).observe(this, Observer { + response-> + when (response!!.status()){ + Status.LOADING ->{ + showLoading("Uploading account details", "This will only take a short while") + } + + Status.SUCCESS ->{ + stopShowingLoading() + startActivity(Intent(baseContext, BillingAddressActivity::class.java)) + } + + Status.ERROR ->{ + stopShowingLoading() + Toast.makeText(baseContext, response.error().message.toString(), Toast.LENGTH_LONG).show() + } + + Status.EMPTY ->{ + + } + + } + }) + + + + } else { + Toast.makeText(this, "Please correct the information entered", Toast.LENGTH_SHORT).show() + } + } + + private fun validates(): Boolean { + tilFirstName.isErrorEnabled = false + tilLastName.isErrorEnabled = false + tilStreetAddress.isErrorEnabled = false + tilCity.isErrorEnabled = false + tilZipcode.isErrorEnabled = false + tilCountry.isErrorEnabled = false + tilPhone.isErrorEnabled = false + + var validation = true + + val firstName = etFirstName.text.toString() + val lastName = etLastName.text.toString() + val streetAddress = etStreetAddress.text.toString() + val city = etCity.text.toString() + val zipcode = etZipcode.text.toString() + val country = etCountry.text.toString() + val phone = etPhone.text.toString() + + if (firstName.isEmpty()) { + tilFirstName.error = "Please fill this" + validation = false + } + + if (lastName.isEmpty()) { + tilLastName.error = "Please fill this" + validation = false + } + + if (streetAddress.isEmpty()) { + tilStreetAddress.error = "Please fill this" + validation = false + } + + if (city.isEmpty()) { + tilCity.error = "Please fill this" + validation = false + } + + if (zipcode.isEmpty()) { + tilZipcode.error = "Please fill this" + validation = false + } + + if (country.isEmpty()) { + tilCountry.error = "Please fill this" + validation = false + } + + if (phone.isEmpty()) { + tilPhone.error = "Please fill this" + validation = false + } + + + return validation } } diff --git a/app/src/main/java/me/gilo/wc/viewmodels/CustomerViewModel.java b/app/src/main/java/me/gilo/wc/viewmodels/CustomerViewModel.java index ac82fa1..e5ae957 100644 --- a/app/src/main/java/me/gilo/wc/viewmodels/CustomerViewModel.java +++ b/app/src/main/java/me/gilo/wc/viewmodels/CustomerViewModel.java @@ -1,6 +1,7 @@ package me.gilo.wc.viewmodels; import android.arch.lifecycle.ViewModel; +import com.google.firebase.auth.FirebaseAuth; import me.gilo.wc.common.WooLiveData; import me.gilo.wc.repo.CustomerRepository; import me.gilo.woodroid.models.Customer; @@ -28,6 +29,10 @@ public final class CustomerViewModel extends ViewModel { return customerRepository.customer(id); } + public WooLiveData> currentCustomer() { + return customerRepository.currentCustomer(); + } + public WooLiveData> customers() { return customerRepository.customers(); } diff --git a/app/src/main/res/layout/customer_basic_details.xml b/app/src/main/res/layout/customer_basic_details.xml index 5cb0899..8e645e2 100644 --- a/app/src/main/res/layout/customer_basic_details.xml +++ b/app/src/main/res/layout/customer_basic_details.xml @@ -156,7 +156,7 @@ android:layout_height="wrap_content" android:hint="Username" android:id="@+id/etUsername" - android:inputType="textPassword" + android:inputType="text" android:paddingBottom="16dp" android:textColor="#5f6368" android:textColorHint="#9e9e9e" diff --git a/app/src/main/res/layout/customer_billing_address.xml b/app/src/main/res/layout/customer_billing_address.xml index fb0b09f..cbbc22b 100644 --- a/app/src/main/res/layout/customer_billing_address.xml +++ b/app/src/main/res/layout/customer_billing_address.xml @@ -126,7 +126,7 @@ android:layout_height="wrap_content" android:hint="Company" android:id="@+id/etCompany" - android:inputType="textEmailAddress" + android:inputType="text" android:paddingBottom="16dp" android:textColor="#5f6368" android:textColorHint="#9e9e9e" @@ -151,7 +151,7 @@ android:layout_height="wrap_content" android:hint="Street Address" android:id="@+id/etStreetAddress" - android:inputType="textPassword" + android:inputType="textPostalAddress" android:paddingBottom="16dp" android:textColor="#5f6368" android:textColorHint="#9e9e9e" @@ -174,7 +174,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Street Address 2" - android:inputType="textPassword" + android:inputType="textPostalAddress" android:paddingBottom="16dp" android:id="@+id/etStreetAddress2" android:textColor="#5f6368" @@ -206,7 +206,7 @@ android:layout_height="wrap_content" android:hint="City" android:id="@+id/etCity" - android:inputType="textPassword" + android:inputType="text" android:paddingBottom="16dp" android:textColor="#5f6368" android:textColorHint="#9e9e9e" @@ -231,7 +231,7 @@ android:layout_height="wrap_content" android:hint="State" android:id="@+id/etState" - android:inputType="textPassword" + android:inputType="text" android:paddingBottom="16dp" android:textColor="#5f6368" android:textColorHint="#9e9e9e" @@ -264,7 +264,7 @@ android:layout_height="wrap_content" android:hint="Zip code" android:id="@+id/etZipcode" - android:inputType="textPassword" + android:inputType="number" android:paddingBottom="16dp" android:textColor="#5f6368" android:textColorHint="#9e9e9e" @@ -298,7 +298,7 @@ android:layout_height="wrap_content" android:hint="Country" android:id="@+id/etCountry" - android:inputType="textPassword" + android:inputType="text" android:paddingBottom="16dp" android:textColor="#5f6368" android:textColorHint="#9e9e9e" @@ -322,7 +322,7 @@ android:layout_height="wrap_content" android:hint="Phone" android:id="@+id/etPhone" - android:inputType="textEmailAddress" + android:inputType="phone" android:paddingBottom="16dp" android:textColor="#5f6368" android:textColorHint="#9e9e9e" diff --git a/woodroid/src/main/java/me/gilo/woodroid/models/Customer.java b/woodroid/src/main/java/me/gilo/woodroid/models/Customer.java index 8c6c5cb..566c2d9 100644 --- a/woodroid/src/main/java/me/gilo/woodroid/models/Customer.java +++ b/woodroid/src/main/java/me/gilo/woodroid/models/Customer.java @@ -27,9 +27,9 @@ public class Customer implements Serializable{ public String totalSpent; @SerializedName("avatar_url") public String avatarUrl; - @SerializedName("billing_address") + @SerializedName("billing") public BillingAddress billingAddress; - @SerializedName("shipping_address") + @SerializedName("shipping") public ShippingAddress shippingAddress; public String getPassword() {