Skip to content

Commit

Permalink
add discount
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedshasho committed Aug 14, 2023
1 parent 5f1b37a commit bfc5fca
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 29 deletions.
26 changes: 23 additions & 3 deletions app/src/main/java/com/scholar/center/adapter/MaterialAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.scholar.center.adapter

import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -8,6 +9,7 @@ import com.bumptech.glide.Glide
import com.scholar.center.R
import com.scholar.center.databinding.SubjectItemBinding
import com.scholar.center.unit.Constants.BASE_URL
import com.scholar.center.unit.applyDiscount
import com.scholar.domain.model.MaterialWithTeacher

class MaterialAdapter(
Expand Down Expand Up @@ -41,9 +43,25 @@ class MaterialAdapter(
context.getString(R.string.hour, item.material.hoursNumberOfWeek)

if (item.material.price != null && item.material.price != 0) {
holder.binding.materialSubjectPriceText.text = context.getString(
R.string.syr, item.material.price
)
if (item.material.discount != null && item.material.discount != 0) {
holder.binding.materialSubjectDiscountText.text = context.getString(
R.string.price_only, item.material.price
)
holder.binding.materialSubjectDiscountText.paintFlags =
holder.binding.materialSubjectDiscountText.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
val discountedPrice =
item.material.price?.applyDiscount(item.material.discount ?: 0)

holder.binding.materialSubjectPriceText.text = context.getString(
R.string.syr, discountedPrice
)
} else {
holder.binding.materialSubjectDiscountText.visibility = View.GONE
holder.binding.materialSubjectPriceText.text = context.getString(
R.string.syr, item.material.price
)
}

} else {
holder.binding.materialSubjectPriceText.visibility = View.GONE
}
Expand Down Expand Up @@ -72,6 +90,8 @@ class MaterialAdapter(
notifyItemChanged(lastItemSize, l.size)
}



class ViewHolder(val binding: SubjectItemBinding) :
RecyclerView.ViewHolder(binding.root)
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class HomeFragment : Fragment(R.layout.fragment_home) {
materialAdapter.setList(materials)
}
}

lifecycleScope.launch {
viewModel.categories.collect { categories ->
categoryAdapter.setMaterialSubjectList(categories)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MainFragment : Fragment(R.layout.fragment_main), NavController.OnDestinati
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
lifecycleScope.launch {
viewModel.studentName.collectLatest { name ->
if (name == null) {
if (name.isNullOrEmpty()) {
binding.mainStudentName.visibility = View.GONE
} else {
binding.mainStudentName.visibility = View.VISIBLE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.scholar.center.ui.materials.detail

import android.graphics.Paint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
Expand All @@ -12,6 +13,7 @@ import com.google.android.material.tabs.TabLayoutMediator
import com.scholar.center.R
import com.scholar.center.adapter.TeacherPagerAdapter
import com.scholar.center.databinding.FragmentMaterialDetailBinding
import com.scholar.center.unit.applyDiscount
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -52,17 +54,37 @@ class MaterialDetailFragment : Fragment(R.layout.fragment_material_detail) {
val material = materialWithDetail.material
binding.materialName.text = material.title
binding.materialType.text = materialWithDetail.category
binding.materialClassName.text = "${materialWithDetail.stage}, ${materialWithDetail.classroom}"
// binding.materialDescription.text = material.description
if (material.price == null || material.price == 0) {

binding.materialClassName.text = getString(
R.string.material_class_name,
materialWithDetail.stage,
materialWithDetail.classroom
)

if (material.price != null && material.price != 0) {
if (material.discount != null && material.discount != 0) {
binding.materialDiscount.text = getString(
R.string.price_only, material.price
)
binding.materialDiscount.paintFlags =
binding.materialDiscount.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
val discountedPrice =
material.price?.applyDiscount(material.discount ?: 0)
binding.materialPriceValue.text = getString(
R.string.syr, discountedPrice
)
} else {
binding.materialDiscount.visibility = View.GONE
binding.materialPriceValue.text = getString(R.string.syr, material.price)
}

} else {
binding.materialPriceLayout.visibility = View.GONE
binding.materialPriceButton.text = getText(R.string.view)
} else {
binding.materialPriceValue.text =
getString(R.string.syr, material.price)
}



binding.materialPriceButton.setOnClickListener {
if (material.price == null || material.price == 0) {
// navigate to show
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/scholar/center/unit/ExtensionFun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ fun Context.getThemeColor(resId: Int): Int {
true
)
return typedValue.data
}

fun Int.applyDiscount(discountPercentage: Int): Int {
val discountAmount = (discountPercentage.toDouble() / 100) * this
return this - discountAmount.toInt()
}
25 changes: 25 additions & 0 deletions app/src/main/res/layout/dialog_loading.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="20dp">


<ProgressBar
android:layout_width="40dp"
android:layout_height="40dp"
android:indeterminate="true"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading"
android:layout_marginHorizontal="10dp"
android:textAppearance="@style/TextBody1"
android:layout_gravity="center_vertical"
/>


</LinearLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/layout/fragment_material_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@
android:text="@string/price"
android:textAppearance="@style/TextBody1" />

<TextView
android:id="@+id/material_discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:layout_marginHorizontal="5dp"
android:textAppearance="@style/TextBody2" />
<TextView
android:id="@+id/material_price_value"
android:layout_width="wrap_content"
Expand Down
28 changes: 9 additions & 19 deletions app/src/main/res/layout/subject_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,23 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:orientation="horizontal"
android:paddingHorizontal="10dp">

<TextView
android:id="@+id/material_subject_discount_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="500"
android:textAppearance="@style/TextBody2" />
<TextView
android:id="@+id/material_subject_price_text"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
android:text="1000 SYR"
android:textAppearance="@style/TextBody2" />

<!-- <TextView-->
<!-- android:id="@+id/material_subject_number_views_text"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:gravity="end"-->
<!-- android:padding="2dp"-->
<!-- android:text="100"-->
<!-- android:textAppearance="@style/TextSubTitle2"-->
<!-- />-->
<!-- <ImageView-->
<!-- android:id="@+id/material_subject_number_views_image"-->
<!-- android:layout_width="18dp"-->
<!-- android:layout_height="16dp"-->
<!-- android:src="@drawable/ic_eye"-->
<!-- app:tint="?attr/colorPrimaryVariant" />-->
</LinearLayout>
</LinearLayout>

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,9 @@
<string name="hi">مرحبا %s</string>
<string name="welcome">مرحبا بكم في تطبيق سكولار</string>
<string name="hour">%d ساعة</string>
<string name="price_only" >%d</string>

<string name="material_class_name">%1$s, %2$s</string>
<string name="loading">جاري التحميل…</string>

</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,9 @@
<string name="hi">Hi %s</string>
<string name="welcome">Welcome to the Scholar app</string>
<string name="hour">%d Hours</string>
<string name="price_only" >%d</string>

<string name="material_class_name">%1$s, %2$s</string>
<string name="loading">Loading...</string>

</resources>

0 comments on commit bfc5fca

Please sign in to comment.