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.

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

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

16 changes: 15 additions & 1 deletion 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,15 +31,27 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}

buildFeatures {
viewBinding true

dataBinding true
}
}

dependencies {


implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
implementation "androidx.navigation:navigation-ui-ktx:2.3.5"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.+'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.fragment:fragment:1.3.6'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
39 changes: 39 additions & 0 deletions app/src/main/java/com/example/hw_product_recycleview/Buy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.hw_product_recycleview

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


class Buy : Fragment() {

private var _binding: FragmentBuyBinding ?= null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentBuyBinding.inflate(inflater,container,false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
arguments?.let {
binding.phoneNameSmartphoneActivity.text = it.getString("phoneInfo").toString()
binding.phoneImageSmartPhoneActivity.setImageResource(it.getInt("phoneImage"))

}

}

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


Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@ package com.example.hw_product_recycleview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupActionBarWithNavController
import com.example.hw_product_recycleview.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var navController: NavController

private 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)
}

}
42 changes: 42 additions & 0 deletions app/src/main/java/com/example/hw_product_recycleview/Store.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.hw_product_recycleview

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

class Store : Fragment() {

var _binding: FragmentStoreBinding?=null
val binding get() = _binding

//- onCreaate() the fragment instantiated and on Create State, (view not created yet)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

}

//- `onCreateView()`: This method is where you inflate the layout (connects the xml with activity)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentStoreBinding.inflate(layoutInflater,container,false)
val view= binding!!.root
return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

binding?.recyclerView?.adapter = context?.let { SmartPhoneAdapter(this, it) }
}


override fun onDestroy() {
super.onDestroy()
_binding=null }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.hw_product_recycleview.adapter

import android.annotation.SuppressLint
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.StoreDirections
import com.example.hw_product_recycleview.R
import com.example.hw_product_recycleview.Store
import com.example.hw_product_recycleview.data.DataSource

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

private val dataSource = DataSource.smartPhone

class SmartPhoneViewHolder(view: View?) : RecyclerView.ViewHolder(view!!) {
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.vertical_list_item, parent, false)
)
}

@SuppressLint("ResourceAsColor")
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

if (item.isVip) {
holder.productIsVip?.visibility = View.VISIBLE
}
holder.addToCart?.setOnClickListener {
if (item.productQuantity > 0) {
// Toast.makeText(context, "added to cart", Toast.LENGTH_SHORT).show()
} else {
// Toast.makeText(context, "Item is out of stock", Toast.LENGTH_SHORT).show()
}
}

// stopped her (Perform the Navigation Action**)===================================================================
holder.productImage?.setOnClickListener {

val action = StoreDirections.actionBuyToStore( phoneInfo= holder.productName?.text.toString(),phoneImage =(item.productImage))
holder.itemView.findNavController().navigate(action)

}

}

override fun getItemCount(): Int = dataSource.size
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.hw_product_recycleview.conistant

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,21 @@
package com.example.hw_product_recycleview.data

import com.example.hw_product_recycleview.R
import com.example.hw_product_recycleview.model.SmartPhone


/**
* An object to generate a list of smartphones
*/
object DataSource {

val smartPhone: List<SmartPhone> = listOf(
SmartPhone( R.drawable.iphone, R.string.Iphone_12_64GB,"4500 SR", false,5 ),
SmartPhone(R.drawable.samsung_galaxy_z_flip, R.string.Samsung_Galaxy_Z_Flip3,"5000 SR", true,70),
SmartPhone(R.drawable.iphone13,R.string.Iphone_13,"6000 SR",false,36 ),
SmartPhone(R.drawable.samsung_galaxy_uitra, R.string.Samsung_Galaxy_Z_Flip3,"3500 SR",true,5 ),
SmartPhone(R.drawable.huawei_yp,R.string.Huawei_Y6p,"2900 SR",false,29 )


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

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes

data class SmartPhone(
@DrawableRes val productImage: Int,
@StringRes val productName: Int,
val productPrice: String,
val isVip: Boolean,
val productQuantity: Int
)
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_shopping_bag_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M18,6h-2c0,-2.21 -1.79,-4 -4,-4S8,3.79 8,6H6C4.9,6 4,6.9 4,8v12c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2V8C20,6.9 19.1,6 18,6zM10,10c0,0.55 -0.45,1 -1,1s-1,-0.45 -1,-1V8h2V10zM12,4c1.1,0 2,0.9 2,2h-4C10,4.9 10.9,4 12,4zM16,10c0,0.55 -0.45,1 -1,1s-1,-0.45 -1,-1V8h2V10z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_star_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFD700"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
</vector>
22 changes: 11 additions & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout 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=".MainActivity">
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!--=============================================================here have problem -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
38 changes: 38 additions & 0 deletions app/src/main/res/layout/fragment_buy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Buy">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/phone_image_smartPhone_activity"
android:layout_width="392dp"
android:layout_height="335dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/iphone" />

<TextView
android:id="@+id/phone_name_smartphone_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/phone_image_smartPhone_activity"
app:layout_constraintTop_toBottomOf="@+id/phone_image_smartPhone_activity" />


</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Loading