Skip to content

Commit

Permalink
Add UI for coupons
Browse files Browse the repository at this point in the history
  • Loading branch information
andraantariksa committed Apr 13, 2022
1 parent c12a823 commit 4c456b9
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ dependencies {
// debugImplementation "com.midtrans:uikit:1.31.0-SANDBOX"
implementation "com.midtrans:uikit:1.31.0"
implementation 'com.google.android.flexbox:flexbox:3.0.0'
implementation 'com.facebook.shimmer:shimmer:0.5.0'

implementation "pub.devrel:easypermissions:3.0.0"

Expand Down
8 changes: 5 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/Theme.Koffie"
android:supportsRtl="true"
tools:replace="android:allowBackup">
<activity
Expand All @@ -29,9 +30,10 @@
</activity>
<activity
android:name=".ui.auth.AuthActivity"
android:theme="@style/Theme.Koffie"
android:exported="true">
</activity>
android:exported="false" />
<activity
android:name=".ui.coupons.CouponsActivity"
android:exported="false" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package id.shaderboi.koffie.ui.coupons

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import dagger.hilt.android.AndroidEntryPoint
import id.shaderboi.koffie.R
import id.shaderboi.koffie.core.domain.model.Coupon
import id.shaderboi.koffie.databinding.ActivityCouponsBinding
import id.shaderboi.koffie.ui.coupons.adapter.CouponAdapter
import id.shaderboi.koffie.ui.coupons.adapter.CouponShimmerAdapter
import kotlinx.coroutines.delay

@AndroidEntryPoint
class CouponsActivity : AppCompatActivity() {
private var _binding: ActivityCouponsBinding? = null
val binding get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

_binding = ActivityCouponsBinding.inflate(layoutInflater)

collectUIEvent()
setupView()

setContentView(binding.root)
}

private fun collectUIEvent() {
val coupons = mutableListOf<Coupon>()
for (i in 1..10) {
coupons.add(
Coupon(
0,
"ABC",
"asdasdasdasd\nzxczcxzcxzcxc"
)
)
coupons.add(
Coupon(
1,
"asdzxzx",
"asdasdasdasd\nzxczcxzcxzcxc"
)
)
}
lifecycleScope.launchWhenCreated {
delay(3000)
binding.shimmerFrameLayoutMain.hideShimmer()
binding.recyclerViewCoupons.adapter = CouponAdapter(coupons) { view, coupon ->
val intent = Intent()
intent.putExtra("SELECTED_COUPON", coupon.id)
setResult(1, intent)
}
}
}

private fun setupView() {
setSupportActionBar(binding.toolbar)
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
}
binding.toolbar.setNavigationOnClickListener {
onBackPressed()
}

val dividerItemDecoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
dividerItemDecoration.setDrawable(
ContextCompat.getDrawable(this, R.drawable.divider8dp)!!
);
binding.recyclerViewCoupons.apply {
addItemDecoration(dividerItemDecoration)
adapter = CouponShimmerAdapter()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package id.shaderboi.koffie.ui.coupons.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import id.shaderboi.koffie.core.domain.model.Coupon
import id.shaderboi.koffie.databinding.ItemCouponBinding

class CouponAdapter(
private val coupons: List<Coupon>,
private val onClick: (view: View, coupon: Coupon) -> Unit
) :
RecyclerView.Adapter<CouponAdapter.ViewHolder>() {

class ViewHolder(val binding: ItemCouponBinding) :
RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return ViewHolder(ItemCouponBinding.inflate(inflater, parent, false))
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val coupon = coupons[position]
holder.binding.apply {
textViewDescription.text = coupon.description
textViewTitle.text = coupon.title
root.setOnClickListener { view ->
onClick(view, coupon)
}
}
}

override fun getItemCount(): Int = coupons.size
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package id.shaderboi.koffie.ui.coupons.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import id.shaderboi.koffie.R
import id.shaderboi.koffie.databinding.ItemCouponBinding

class CouponShimmerAdapter() :
RecyclerView.Adapter<CouponShimmerAdapter.ViewHolder>() {

class ViewHolder(val binding: ItemCouponBinding) :
RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return ViewHolder(ItemCouponBinding.inflate(inflater, parent, false))
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val context = holder.binding.root.context
holder.binding.apply {
textViewDescription.apply {
background = ContextCompat.getDrawable(context, R.color.placeholder)
minLines = 3
}
textViewTitle.background =
ContextCompat.getDrawable(context, R.color.placeholder)
}
}

override fun getItemCount(): Int = 10
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package id.shaderboi.koffie.ui.main.home

import Permission
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import dagger.hilt.android.AndroidEntryPoint
import id.shaderboi.koffie.databinding.FragmentHomeBinding
import id.shaderboi.koffie.ui.main.home.view_model.StoreFrontViewModel
import id.shaderboi.koffie.ui.coupons.CouponsActivity
import pub.devrel.easypermissions.EasyPermissions

@AndroidEntryPoint
Expand All @@ -19,6 +23,14 @@ class HomeFragment : Fragment() {

private val homeViewModel by viewModels<StoreFrontViewModel>()

private val takePromoCoupon = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
if (it.resultCode == Activity.RESULT_OK) {

}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -27,10 +39,18 @@ class HomeFragment : Fragment() {
_binding = FragmentHomeBinding.inflate(inflater, container, false)

askPermission()
setupView()

return binding.root
}

private fun setupView() {
binding.includeViewPromo.root.setOnClickListener {
val intent = Intent(requireContext(), CouponsActivity::class.java)
takePromoCoupon.launch(intent)
}
}

private fun askPermission() {
if (EasyPermissions.hasPermissions(requireContext(), *Permission.location)) {
return
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_cart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
android:showDividers="middle">

<include
android:id="@+id/viewPromo"
android:id="@+id/includeViewPromo"
layout="@layout/view_promo" />

<FrameLayout
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/res/layout/activity_coupons.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
xmlns:tools="http:https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:liftOnScroll="true">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="12dp"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:title="Promos for you" />
</com.google.android.material.appbar.AppBarLayout>

<com.facebook.shimmer.ShimmerFrameLayout
app:shimmer_auto_start="true"
android:id="@+id/shimmerFrameLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="24dp"
android:paddingTop="24dp"
android:paddingEnd="24dp"
android:paddingBottom="24dp">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewCoupons"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_coupon" />
</com.facebook.shimmer.ShimmerFrameLayout>
</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:orientation="vertical">

<include
android:id="@+id/viewPromo"
android:id="@+id/includeViewPromo"
layout="@layout/view_promo" />

<androidx.recyclerview.widget.RecyclerView
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:orientation="vertical">

<include
android:id="@+id/viewPromo"
android:id="@+id/includeViewPromo"
layout="@layout/view_promo" />

<androidx.recyclerview.widget.RecyclerView
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/res/layout/item_coupon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
xmlns:tools="http:https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/round_bordered8dp"
android:paddingStart="24dp"
android:paddingTop="16dp"
android:paddingEnd="24dp"
android:paddingBottom="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/divider8dp"
android:orientation="vertical"
android:showDividers="middle">

<TextView
android:id="@+id/textViewTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
tools:text="3k delivery discount" />

<TextView
android:id="@+id/textViewDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="15k off food + 3k off delivery" />
</LinearLayout>

</FrameLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="danger">#B8281E</color>
<color name="placeholder">#5E5E5E</color>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package id.shaderboi.koffie.core.domain.model

data class Coupon(
val id: Int,
val title: String,
val description: String,
)

0 comments on commit 4c456b9

Please sign in to comment.