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
15 changes: 15 additions & 0 deletions Week3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions Week3/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
56 changes: 56 additions & 0 deletions Week3/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
plugins {
alias(libs.plugins.android.application)
}

android {
namespace = "com.example.week3"
compileSdk {
version = release(36) {
minorApiLevel = 1
}
}

defaultConfig {
applicationId = "com.example.week3"
minSdk = 24
targetSdk = 36
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

buildFeatures {
viewBinding = true
}
}

dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
implementation(libs.androidx.navigation.fragment.ktx)
implementation(libs.androidx.navigation.ui.ktx)
implementation("com.google.code.gson:gson:2.10.1")
implementation("com.google.code.gson:gson:2.10.1")
implementation("androidx.datastore:datastore-preferences:1.0.0")
}
21 changes: 21 additions & 0 deletions Week3/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.week3

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.week3", appContext.packageName)
}
}
24 changes: 24 additions & 0 deletions Week3/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Week3">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
52 changes: 52 additions & 0 deletions Week3/app/src/main/java/com/example/week3/BuyAllFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.week3

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.example.week3.ProductRepository
import com.example.week3.databinding.FragmentBuyAllBinding
import kotlinx.coroutines.launch

class BuyAllFragment : Fragment(R.layout.fragment_buy_all) {
private var _binding: FragmentBuyAllBinding? = null
private val binding get() = _binding!!

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentBuyAllBinding.bind(view)

val productAdapter = ProductAdapter { clickedItem ->
handleWishClick(clickedItem)
}

binding.recyclerGrid.apply {
adapter = productAdapter
layoutManager = GridLayoutManager(requireContext(), 2)
}
loadProducts()
}

private fun loadProducts() {
viewLifecycleOwner.lifecycleScope.launch {
val allProducts = ProductRepository.getProductsOnce(requireContext())

(binding.recyclerGrid.adapter as ProductAdapter).submitList(allProducts)
}
}

private fun handleWishClick(clickedItem: ProductData) {
val adapter = binding.recyclerGrid.adapter as ProductAdapter
val currentList = adapter.currentList.toMutableList()
val index = currentList.indexOfFirst { it.id == clickedItem.id }
if (index != -1) {
currentList[index] = clickedItem.copy(isWished = !clickedItem.isWished)
adapter.submitList(currentList)

viewLifecycleOwner.lifecycleScope.launch {
ProductRepository.saveProducts(requireContext(), currentList)
}
}
}
}
71 changes: 71 additions & 0 deletions Week3/app/src/main/java/com/example/week3/BuyFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.example.week3

import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.example.week3.databinding.FragmentBuyBinding
import com.google.android.material.tabs.TabLayout

class BuyFragment : Fragment(R.layout.fragment_buy) {

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

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentBuyBinding.bind(view)

setupTabs()
}

private fun setupTabs() {
binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.all)))
binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.Shirts)))
binding.tabLayout.addTab(binding.tabLayout.newTab().setText(getString(R.string.Shoes)))

replaceTabFragment(BuyAllFragment())

binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
updateTabStyle(tab, true)

when (tab?.position) {
0 -> replaceTabFragment(BuyAllFragment())
1 -> replaceTabFragment(BuyTopsFragment())
2 -> replaceTabFragment(BuyShoesFragment())
}
}

override fun onTabUnselected(tab: TabLayout.Tab?) {
updateTabStyle(tab, false)
}

override fun onTabReselected(tab: TabLayout.Tab?) {}
})

updateTabStyle(binding.tabLayout.getTabAt(0), true)
}

private fun replaceTabFragment(fragment: Fragment) {
childFragmentManager.beginTransaction()
.replace(R.id.tabContent, fragment)
.commit()
}

private fun updateTabStyle(tab: TabLayout.Tab?, isBold: Boolean) {
val tabView = (binding.tabLayout.getChildAt(0) as ViewGroup).getChildAt(tab?.position ?: 0) as ViewGroup
val textView = tabView.getChildAt(1) as? TextView

textView?.let {
it.typeface = if (isBold) Typeface.DEFAULT_BOLD else Typeface.DEFAULT
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
23 changes: 23 additions & 0 deletions Week3/app/src/main/java/com/example/week3/BuyShoesFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.week3

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class BuyShoesFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.fragment_buy_shoes, container, false)
}

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

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

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class BuyTopsFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.fragment_buy_tops, container, false)
}

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

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

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.week3.ProductRepository
import com.example.week3.databinding.FragmentHomeBinding
import kotlinx.coroutines.launch

class HomeFragment : Fragment(R.layout.fragment_home) {
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentHomeBinding.bind(view)

val productAdapter = ProductAdapter { clickedItem ->
handleWishClick(clickedItem)
}

binding.recyclerViewHome.apply {
adapter = productAdapter
layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
isNestedScrollingEnabled = false
}

loadProducts()
}

private fun loadProducts() {
viewLifecycleOwner.lifecycleScope.launch {
val allProducts = ProductRepository.getProductsOnce(requireContext())
val adapter = binding.recyclerViewHome.adapter as ProductAdapter
adapter.submitList(allProducts)
}
}

private fun handleWishClick(clickedItem: ProductData) {
val adapter = binding.recyclerViewHome.adapter as ProductAdapter
val newList = adapter.currentList.toMutableList()

val index = newList.indexOfFirst { it.id == clickedItem.id }
if (index != -1) {
newList[index] = clickedItem.copy(isWished = !clickedItem.isWished)

adapter.submitList(newList)

viewLifecycleOwner.lifecycleScope.launch {
ProductRepository.saveProducts(requireContext(), newList)
}
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Loading