Skip to content

Commit

Permalink
Added coupon use case for create
Browse files Browse the repository at this point in the history
  • Loading branch information
gilokimu committed Feb 17, 2019
1 parent 130ac63 commit 17177d0
Show file tree
Hide file tree
Showing 20 changed files with 636 additions and 44 deletions.
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ui.coupon.AddCouponActivity"
android:label="@string/title_activity_add_coupon"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".ui.MenuActivity"
android:label="@string/title_activity_menu"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".ui.CouponActivity"
android:name=".ui.coupon.CouponActivity"
android:label="@string/title_activity_coupon"
android:theme="@style/AppTheme.NoActionBar">
</activity>
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/me/gilo/wc/adapter/CouponAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.gilo.wc.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import me.gilo.wc.R;
import me.gilo.wc.adapter.viewholder.CouponViewHolder;
import me.gilo.woodroid.models.Coupon;

import java.util.List;

public class CouponAdapter extends RecyclerView.Adapter<CouponViewHolder> {
private List<Coupon> coupons;

public CouponAdapter(List<Coupon> coupons) {
this.coupons = coupons;
}

@Override
public CouponViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new CouponViewHolder(parent.getContext(), LayoutInflater.from(parent.getContext()).inflate(R.layout.single_coupon_item, parent, false));
}

@Override
public void onBindViewHolder(CouponViewHolder holder, int position) {
holder.renderView(coupons.get(position));
}


@Override
public int getItemCount() {
return coupons.size() == 0 ? 0 : coupons.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.gilo.wc.adapter.viewholder

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.text.Html
import android.view.View
import android.widget.TextView
import me.gilo.wc.R
import me.gilo.woodroid.models.Coupon

class CouponViewHolder(val context: Context, itemView: View) :
RecyclerView.ViewHolder(itemView) {

fun renderView(coupon: Coupon) {
val tvTitle = itemView.findViewById<TextView>(R.id.tvTitle)
val tvDescription = itemView.findViewById<TextView>(R.id.tvDescription)

tvTitle.text = coupon.code.toUpperCase()
tvDescription.text = Html.fromHtml(coupon.description)
}


}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package me.gilo.wc.adapter.viewholder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import me.gilo.wc.R;
import me.gilo.wc.ui.CouponActivity;
import me.gilo.wc.ui.coupon.CouponActivity;
import me.gilo.wc.ui.ShopActivity;

public class MenuViewHolder extends RecyclerView.ViewHolder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package me.gilo.wc.adapter.viewholder

import android.content.Context
import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.text.Html
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.squareup.picasso.Picasso
import me.gilo.wc.R
import me.gilo.wc.ui.CouponActivity
import me.gilo.wc.ui.ShopActivity
import me.gilo.woodroid.models.Product

class ProductViewHolder(val context: Context, itemView: View) :
Expand Down
27 changes: 15 additions & 12 deletions app/src/main/java/me/gilo/wc/ui/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package me.gilo.wc.ui

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.app.AppCompatActivity
import io.github.inflationx.viewpump.ViewPumpContextWrapper
import me.gilo.wc.R

import kotlinx.android.synthetic.main.activity_menu.*
import kotlinx.android.synthetic.main.content_menu.*
import me.gilo.wc.adapter.MenuAdapter
import java.util.ArrayList
import me.gilo.wc.ui.state.ProgressDialogFragment

open class BaseActivity : AppCompatActivity() {

private lateinit var progressDialog : ProgressDialogFragment

override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase))
}

fun showLoading(title: String, message: String) {
val manager = supportFragmentManager
progressDialog = ProgressDialogFragment.newInstance(title, message)
progressDialog.isCancelable = false
progressDialog.show(manager, "progress")
}

fun stopShowingLoading() {
progressDialog.dismiss()
}
}
21 changes: 0 additions & 21 deletions app/src/main/java/me/gilo/wc/ui/CouponActivity.kt

This file was deleted.

59 changes: 59 additions & 0 deletions app/src/main/java/me/gilo/wc/ui/coupon/AddCouponActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.gilo.wc.ui.coupon

import android.os.Bundle
import kotlinx.android.synthetic.main.activity_add_coupon.*
import kotlinx.android.synthetic.main.content_add_coupon.*
import me.gilo.wc.R
import me.gilo.wc.ui.BaseActivity
import me.gilo.woodroid.Woocommerce
import me.gilo.woodroid.models.Coupon
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class AddCouponActivity : BaseActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_coupon)
setSupportActionBar(toolbar)

bCreate.setOnClickListener { submit() }

}

private fun submit() {
val code = etCode.text.toString()
val description = etDescription.text.toString()

val coupon = Coupon()
coupon.code = code
coupon.description = description

createCoupon(coupon)
}

private fun createCoupon(coupon: Coupon) {
showLoading("Loading", "This won't take long")

val woocommerce = Woocommerce.Builder()
.setSiteUrl("http://157.230.131.179")
.setApiVersion(Woocommerce.API_V2)
.setConsumerKey("ck_26c61abd7eeff238d87dc56585bf26cb2d1a1ec3")
.setConsumerSecret("cs_062e8e3a7ae0ce08fdebc0c39f8f834d5e87598e")
.build()

woocommerce.Coupon().create(coupon).enqueue(object : Callback<Coupon> {
override fun onResponse(call: Call<Coupon>, response: Response<Coupon>) {
val couponResponse = response.body()
stopShowingLoading()
finish()
}

override fun onFailure(call: Call<Coupon>, t: Throwable) {
stopShowingLoading()
}
})
}

}
73 changes: 73 additions & 0 deletions app/src/main/java/me/gilo/wc/ui/coupon/CouponActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package me.gilo.wc.ui.coupon

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_coupon.*
import kotlinx.android.synthetic.main.content_coupon.*
import me.gilo.wc.R
import me.gilo.wc.adapter.CouponAdapter
import me.gilo.wc.ui.BaseActivity
import me.gilo.woodroid.Woocommerce
import me.gilo.woodroid.models.Coupon
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.*

class CouponActivity : BaseActivity() {


lateinit var adapter : CouponAdapter
lateinit var coupons: ArrayList<Coupon>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_coupon)
setSupportActionBar(toolbar)

title = "Coupon"

val layoutManager = LinearLayoutManager(baseContext, LinearLayoutManager.VERTICAL, false)
rvCoupons.layoutManager = layoutManager
rvCoupons.isNestedScrollingEnabled = false

coupons = ArrayList()

adapter = CouponAdapter(coupons)
rvCoupons.adapter = adapter

coupons()

fab.setOnClickListener{
startActivity(Intent(baseContext, AddCouponActivity::class.java))
}

}

//Not best practise, but works for purposes of demo
private fun coupons() {
val woocommerce = Woocommerce.Builder()
.setSiteUrl("http://157.230.131.179")
.setApiVersion(Woocommerce.API_V2)
.setConsumerKey("ck_26c61abd7eeff238d87dc56585bf26cb2d1a1ec3")
.setConsumerSecret("cs_062e8e3a7ae0ce08fdebc0c39f8f834d5e87598e")
.build()

woocommerce.Coupon().coupons().enqueue(object : Callback<List<Coupon>> {
override fun onResponse(call: Call<List<Coupon>>, response: Response<List<Coupon>>) {
val couponResponse = response.body()
for (coupon in couponResponse!!) {
coupons.add(coupon)
}

adapter.notifyDataSetChanged()
}

override fun onFailure(call: Call<List<Coupon>>, t: Throwable) {

}
})
}

}
53 changes: 53 additions & 0 deletions app/src/main/java/me/gilo/wc/ui/state/ProgressDialogFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package me.gilo.wc.ui.state;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import me.gilo.wc.R;


public class ProgressDialogFragment extends DialogFragment {

private static final String ARG_TITLE= "title";
private static final String ARG_MESSAGE= "message";

String title = "";
String message = "";

public ProgressDialogFragment() {
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_MESSAGE, message);
setArguments(args);
}

public static ProgressDialogFragment newInstance(String title, String message) {
ProgressDialogFragment fragment = new ProgressDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_MESSAGE, message);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.loading_view, container);
TextView tvTitle = view.findViewById(R.id.tvProgress_title);
TextView tvMessage = view.findViewById(R.id.tvProgress_message);

title = getArguments().getString(ARG_TITLE);
message = getArguments().getString(ARG_MESSAGE);


tvTitle.setText(title);
tvMessage.setText(message);

return view;
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions app/src/main/res/layout/activity_add_coupon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.coupon.AddCouponActivity">

<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_add_coupon"/>

</android.support.design.widget.CoordinatorLayout>
Loading

0 comments on commit 17177d0

Please sign in to comment.