Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

7 changes: 7 additions & 0 deletions .idea/misc.xml

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

9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'androidx.navigation.safeargs.kotlin'
}

android {
Expand Down Expand Up @@ -29,6 +31,10 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}

buildFeatures {
viewBinding true
}
}

dependencies {
Expand All @@ -37,7 +43,10 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
package com.example.hw_product_recycleview

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupActionBarWithNavController
import com.example.hw_product_recycleview.adapter.SmartPhoneAdapter
import com.example.hw_product_recycleview.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)


val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController
setupActionBarWithNavController(navController)

}


override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.hw_product_recycleview

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.hw_product_recycleview.conistant.ProductInfo
import com.example.hw_product_recycleview.conistant.SmartPhoneWebsite
import com.example.hw_product_recycleview.databinding.FragmentProductBinding


class ProductFragment : Fragment() {
private var _binding: FragmentProductBinding? = null
private val binding get() = _binding!!
private lateinit var phoneName: String

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

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentProductBinding.inflate(inflater, container, false)
return _binding?.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
arguments?.let {
phoneName = it.getString("name").toString()
binding.phoneNameSmartphoneActivity.text = phoneName
binding.phoneImageSmartPhoneActivity.setImageResource(it.getInt("imageId"))
binding.phonePriceSmartPhoneActivity.text = it.getString("price")

}

// start intent to open Uri on browser
binding.goToPhoneWebsite.setOnClickListener {
val queryUrl: Uri = Uri.parse(getUri(phoneName))
val intent = Intent(Intent.ACTION_VIEW, queryUrl)
startActivity(intent)

}

}

override fun onDestroy() {
super.onDestroy()
_binding = null
}

private fun getUri(phoneName: String?): String {

return when (phoneName) {
getString(R.string.Iphone_12_64GB) -> SmartPhoneWebsite.Iphone_12_64GB
getString(R.string.Samsung_Galaxy_S21_Ultra) -> SmartPhoneWebsite.Samsung_Galaxy_S21_Ultra
getString(R.string.Xiaomi_11T_Pro) -> SmartPhoneWebsite.Xiaomi_11T_Pro
getString(R.string.Huawei_Y6p) -> SmartPhoneWebsite.Huawei_Y6p
getString(R.string.Samsung_Galaxy_Z_Flip3) -> SmartPhoneWebsite.Samsung_Galaxy_Z_Flip3
getString(R.string.Iphone_13) -> SmartPhoneWebsite.Iphone_13
getString(R.string.Samsung_Galaxy_S20_FE) -> SmartPhoneWebsite.Samsung_Galaxy_S20_FE
getString(R.string.Samsung_Galaxy_S21) -> SmartPhoneWebsite.Samsung_Galaxy_S21
getString(R.string.OPPO_Reno_5_Pro) -> SmartPhoneWebsite.OPPO_Reno_5_Pro
getString(R.string.Iphone_11) -> SmartPhoneWebsite.Iphone_11
else -> "https://www.google.com/"
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.hw_product_recycleview

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.hw_product_recycleview.adapter.SmartPhoneAdapter
import com.example.hw_product_recycleview.databinding.FragmentProductsListBinding


class ProductsListFragment : Fragment() {
// this variable for receiving values but no accessible fro reading value from it
private var _binding: FragmentProductsListBinding? = null

// this variable for getting the value of _binding variable
private val binding get() = _binding!!


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

}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentProductsListBinding.inflate(inflater, container, false)
return _binding?.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.gridRecyclerView.adapter = SmartPhoneAdapter(this.context)
binding.gridRecyclerView.setHasFixedSize(true)
}

override fun onDestroy() {
super.onDestroy()
_binding = null
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.example.hw_product_recycleview.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.example.hw_product_recycleview.ProductsListFragmentDirections
import com.example.hw_product_recycleview.R
import com.example.hw_product_recycleview.data.DataSource

class SmartPhoneAdapter(private val context: Context?) :
RecyclerView.Adapter<SmartPhoneAdapter.SmartPhoneViewHolder>() {

// Initialize the data from DataSource class
private val dataSource = DataSource.smartPhone

class SmartPhoneViewHolder(val view: View?) : RecyclerView.ViewHolder(view!!) {

// Initialize the view elements
val productImage: ImageView? = view?.findViewById(R.id.product_image)
val productName: TextView? = view?.findViewById(R.id.product_name)
val productPrice: TextView? = view?.findViewById(R.id.product_price)
val productIsVip: ImageView? = view?.findViewById(R.id.isVip_icon)
val addToCart: ImageButton? = view?.findViewById(R.id.add_to_cart)

}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SmartPhoneViewHolder {
return SmartPhoneViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.grid_list_item, parent, false)
)
}


override fun onBindViewHolder(holder: SmartPhoneViewHolder, position: Int) {
val item = dataSource[position]
holder.productImage?.setImageResource(item.productImage)
holder.productName?.text = context?.getString(item.productName)
holder.productPrice?.text = item.productPrice

// check if the smart phone isVip make the view visible
if (item.isVip) {
holder.productIsVip?.visibility = View.VISIBLE
}

// change the color of add to cart button if the product is out of stock
if (item.productQuantity > 0) {
holder.addToCart?.setBackgroundColor(context?.getColor(R.color.red)!!)
}

// check if the product quantity out of stock when clicking on add to cart button
holder.addToCart?.setOnClickListener {
if (item.productQuantity > 0) {
Toast.makeText(context, "added to cart", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "The item is out of stock", Toast.LENGTH_SHORT).show()
}
}

// start SmartPhoneActivity when the user click on the image of the product
holder.productImage?.setOnClickListener {
val action = ProductsListFragmentDirections.actionProductsListFragmentToProductFragment(
name = context?.getString(item.productName)!!,
price = item.productPrice, imageId = item.productImage
)
holder.view?.findNavController()?.navigate(action)

}

}

override fun getItemCount(): Int = dataSource.size

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.hw_product_recycleview.conistant

/**
* An object holds constants used as keys for the intent from MainActivity to SmartPhoneActivity
*/


object ProductInfo {

const val PHONE_IMAGE = "PhoneImage"
const val PHONE_NAME = "PhoneName"
const val PHONE_PRICE = "PhonePrice"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.hw_product_recycleview.conistant

/**
* An object holds all websites related to the phones
*/


object SmartPhoneWebsite {
const val Iphone_12_64GB =
"https://www.apple.com/shop/buy-iphone/iphone-12/6.1-inch-display-64gb-black-unlocked"
const val Samsung_Galaxy_S21_Ultra =
"https://www.samsung.com/sa_en/smartphones/galaxy-s21-ultra-5g/"
const val Xiaomi_11T_Pro = "https://www.mi.com/global/product/xiaomi-11t-pro/"
const val Huawei_Y6p = "https://consumer.huawei.com/sa/phones/y6p/specs/"
const val Samsung_Galaxy_Z_Flip3 =
"https://www.samsung.com/sa_en/smartphones/galaxy-z-flip3-5g/buy/"
const val Iphone_13 = "https://www.apple.com/sa-ar/iphone-13/"
const val Samsung_Galaxy_S20_FE =
"https://www.samsung.com/ae_ar/smartphones/galaxy-s20/galaxy-s20-fe/"
const val Samsung_Galaxy_S21 = "https://www.samsung.com/sa_en/smartphones/galaxy-s21-5g/"
const val OPPO_Reno_5_Pro = "https://www.oppo.com/sa/smartphones/series-reno/reno5-pro-5g/"
const val Iphone_11 = "https://www.apple.com/sa-ar/iphone-11/specs/"


}
Loading