diff --git a/week4/.gitignore b/week4/.gitignore new file mode 100644 index 0000000..6b7cf56 --- /dev/null +++ b/week4/.gitignore @@ -0,0 +1,19 @@ +*.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 +week1/ +week2 - 복사본/ +./week1/ +"../week2 - \353\263\265\354\202\254\353\263\270/" \ No newline at end of file diff --git a/week4/.idea/.gitignore b/week4/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/week4/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/week4/.idea/AndroidProjectSystem.xml b/week4/.idea/AndroidProjectSystem.xml new file mode 100644 index 0000000..4a53bee --- /dev/null +++ b/week4/.idea/AndroidProjectSystem.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/week4/.idea/compiler.xml b/week4/.idea/compiler.xml new file mode 100644 index 0000000..b86273d --- /dev/null +++ b/week4/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/week4/.idea/deploymentTargetSelector.xml b/week4/.idea/deploymentTargetSelector.xml new file mode 100644 index 0000000..b268ef3 --- /dev/null +++ b/week4/.idea/deploymentTargetSelector.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/week4/.idea/deviceManager.xml b/week4/.idea/deviceManager.xml new file mode 100644 index 0000000..91f9558 --- /dev/null +++ b/week4/.idea/deviceManager.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/week4/.idea/gradle.xml b/week4/.idea/gradle.xml new file mode 100644 index 0000000..02c4aa5 --- /dev/null +++ b/week4/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + + \ No newline at end of file diff --git a/week4/.idea/misc.xml b/week4/.idea/misc.xml new file mode 100644 index 0000000..92dea84 --- /dev/null +++ b/week4/.idea/misc.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/week4/.idea/runConfigurations.xml b/week4/.idea/runConfigurations.xml new file mode 100644 index 0000000..16660f1 --- /dev/null +++ b/week4/.idea/runConfigurations.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/week4/.idea/studiobot.xml b/week4/.idea/studiobot.xml new file mode 100644 index 0000000..539e3b8 --- /dev/null +++ b/week4/.idea/studiobot.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/week4/.idea/vcs.xml b/week4/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/week4/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/week4/app/.gitignore b/week4/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/week4/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/week4/app/build.gradle.kts b/week4/app/build.gradle.kts new file mode 100644 index 0000000..54ce36f --- /dev/null +++ b/week4/app/build.gradle.kts @@ -0,0 +1,74 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.compose.compiler) + id("kotlin-parcelize") +} + +android { + namespace = "com.example.week2" + compileSdk = 35 + + defaultConfig { + applicationId = "com.example.week2" + minSdk = 24 + targetSdk = 35 + 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 + } + kotlinOptions { + jvmTarget = "11" + } + buildFeatures { + viewBinding = true + compose = true + } +} + +dependencies { + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.collection) + implementation(libs.androidx.appcompat) + implementation(libs.material) + implementation(libs.androidx.activity) + implementation(libs.androidx.constraintlayout) + implementation(libs.androidx.fragment.ktx) + implementation(libs.androidx.navigation.fragment.ktx) + implementation(libs.androidx.navigation.ui.ktx) + + // DataStore & Gson + implementation(libs.androidx.datastore.preferences) + implementation(libs.gson) + + // Compose + implementation(platform(libs.androidx.compose.bom)) + implementation(libs.androidx.ui) + implementation(libs.androidx.ui.graphics) + implementation(libs.androidx.ui.tooling.preview) + implementation(libs.androidx.material3) + implementation(libs.androidx.activity.compose) + + testImplementation(libs.junit) + androidTestImplementation(libs.androidx.junit) + androidTestImplementation(libs.androidx.espresso.core) + androidTestImplementation(platform(libs.androidx.compose.bom)) + androidTestImplementation(libs.androidx.ui.test.junit4) + debugImplementation(libs.androidx.ui.tooling) + debugImplementation(libs.androidx.ui.test.manifest) +} diff --git a/week4/app/proguard-rules.pro b/week4/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/week4/app/proguard-rules.pro @@ -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 \ No newline at end of file diff --git a/week4/app/src/androidTest/java/com/example/week2/ExampleInstrumentedTest.kt b/week4/app/src/androidTest/java/com/example/week2/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..e20abc4 --- /dev/null +++ b/week4/app/src/androidTest/java/com/example/week2/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.example.week2 + +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.week2", appContext.packageName) + } +} \ No newline at end of file diff --git a/week4/app/src/main/AndroidManifest.xml b/week4/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..6c15aec --- /dev/null +++ b/week4/app/src/main/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/week4/app/src/main/java/com/example/week2/AllProductsFragment.kt b/week4/app/src/main/java/com/example/week2/AllProductsFragment.kt new file mode 100644 index 0000000..db01a2d --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/AllProductsFragment.kt @@ -0,0 +1,74 @@ +package com.example.week2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle +import androidx.navigation.fragment.findNavController +import androidx.recyclerview.widget.GridLayoutManager +import com.example.week2.databinding.FragmentAllProductsBinding +import kotlinx.coroutines.launch + +class AllProductsFragment : Fragment() { + private var _binding: FragmentAllProductsBinding? = null + private val binding get() = _binding!! + private lateinit var dataStoreManager: DataStoreManager + private var productAdapter: ProductAdapter? = null + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentAllProductsBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + dataStoreManager = DataStoreManager(requireContext()) + setupRecyclerView() + } + + private fun setupRecyclerView() { + productAdapter = ProductAdapter( + emptyList(), + onItemClick = { product -> + navigateToDetail(product) + }, + onWishlistClick = { product, _ -> + viewLifecycleOwner.lifecycleScope.launch { + dataStoreManager.updateWishlistStatus(product.id, !product.isWishlisted) + } + } + ) + + binding.rvAllProducts.layoutManager = GridLayoutManager(context, 2) + binding.rvAllProducts.adapter = productAdapter + + viewLifecycleOwner.lifecycleScope.launch { + viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { + dataStoreManager.productsFlow.collect { products -> + productAdapter?.updateList(products) + } + } + } + } + + private fun navigateToDetail(product: Product) { + val bundle = Bundle().apply { + putParcelable("product", product) + } + + findNavController().navigate(R.id.action_purchaseFragment_to_productDetailFragment, bundle) + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + productAdapter = null + } +} diff --git a/week4/app/src/main/java/com/example/week2/CartFragment.kt b/week4/app/src/main/java/com/example/week2/CartFragment.kt new file mode 100644 index 0000000..527c681 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/CartFragment.kt @@ -0,0 +1,36 @@ +package com.example.week2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.navigation.fragment.findNavController +import com.example.week2.databinding.FragmentCartBinding + +class CartFragment : Fragment() { + private var _binding: FragmentCartBinding? = null + private val binding get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentCartBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + binding.btnOrder.setOnClickListener { + // Navigation Component를 사용하여 구매하기 화면으로 이동 + findNavController().navigate(R.id.nav_purchase) + } + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/week4/app/src/main/java/com/example/week2/DataStoreManager.kt b/week4/app/src/main/java/com/example/week2/DataStoreManager.kt new file mode 100644 index 0000000..c258348 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/DataStoreManager.kt @@ -0,0 +1,59 @@ +package com.example.week2 + +import android.content.Context +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.datastore.preferences.preferencesDataStore +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.map + +val Context.dataStore: DataStore by preferencesDataStore(name = "settings") + +class DataStoreManager(private val context: Context) { + private val gson = Gson() + + companion object { + private val PRODUCTS_KEY = stringPreferencesKey("products") + } + + suspend fun saveProducts(products: List) { + val jsonString = gson.toJson(products) + context.dataStore.edit { preferences -> + preferences[PRODUCTS_KEY] = jsonString + } + } + + val productsFlow: Flow> = context.dataStore.data.map { preferences -> + val jsonString = preferences[PRODUCTS_KEY] ?: "" + if (jsonString.isEmpty()) { + getInitialProducts() + } else { + val type = object : TypeToken>() {}.type + gson.fromJson(jsonString, type) + } + } + + private fun getInitialProducts(): List { + return listOf( + Product(1, "Air Jordan XXXVI", "Basketball Shoes", "US$185", R.drawable.img_air_jordan_xxxvi, category = "Basketball Shoes"), + Product(2, "Nike Air Force 1 '07", "Men's Shoes", "US$115", R.drawable.img_nike_air_force, category = "Men's Shoes"), + Product(3, "Nike Everyday Plus Cushioned", "Training Socks", "US$20", R.drawable.img_nike_everyday_plus_cushioned, category = "Training Socks"), + Product(4, "Nike Elite Crew", "Basketball Socks", "US$16", R.drawable.img_training_ankle_socks, category = "Basketball Socks"), + Product(5, "Jordan Nike Air Force 1 '07 Essentials", "Men's Shoes", "US$115", R.drawable.img_air_jordan_xxxvi, category = "Men's Shoes") + ) + } + + suspend fun updateWishlistStatus(productId: Int, isWishlisted: Boolean) { + val products = productsFlow.first().toMutableList() + val index = products.indexOfFirst { it.id == productId } + if (index != -1) { + products[index] = products[index].copy(isWishlisted = isWishlisted) + saveProducts(products) + } + } +} diff --git a/week4/app/src/main/java/com/example/week2/HomeFragment.kt b/week4/app/src/main/java/com/example/week2/HomeFragment.kt new file mode 100644 index 0000000..6ce63a1 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/HomeFragment.kt @@ -0,0 +1,66 @@ +package com.example.week2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope +import androidx.navigation.fragment.findNavController +import androidx.recyclerview.widget.LinearLayoutManager +import com.example.week2.databinding.FragmentHomeBinding +import kotlinx.coroutines.launch + +class HomeFragment : Fragment() { + private var _binding: FragmentHomeBinding? = null + private val binding get() = _binding!! + private lateinit var dataStoreManager: DataStoreManager + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentHomeBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + dataStoreManager = DataStoreManager(requireContext()) + setupRecyclerView() + } + + private fun setupRecyclerView() { + viewLifecycleOwner.lifecycleScope.launch { + dataStoreManager.productsFlow.collect { products -> + val homeProducts = products.take(3) // 홈 화면에는 상위 3개만 표시 예시 + val adapter = ProductAdapter( + homeProducts, + onItemClick = { product -> + navigateToDetail(product) + }, + onWishlistClick = { product, _ -> + viewLifecycleOwner.lifecycleScope.launch { + dataStoreManager.updateWishlistStatus(product.id, !product.isWishlisted) + } + } + ) + + binding.rvHomeProducts.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) + binding.rvHomeProducts.adapter = adapter + } + } + } + + private fun navigateToDetail(product: Product) { + val bundle = Bundle().apply { + putParcelable("product", product) + } + findNavController().navigate(R.id.action_homeFragment_to_productDetailFragment, bundle) + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/week4/app/src/main/java/com/example/week2/MainActivity.kt b/week4/app/src/main/java/com/example/week2/MainActivity.kt new file mode 100644 index 0000000..96271b5 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/MainActivity.kt @@ -0,0 +1,63 @@ +package com.example.week2 + +import android.os.Bundle +import android.util.Log +import androidx.appcompat.app.AppCompatActivity +import androidx.navigation.fragment.NavHostFragment +import androidx.navigation.ui.setupWithNavController +import com.example.week2.databinding.ActivityMainBinding + +class MainActivity : AppCompatActivity() { + private lateinit var binding: ActivityMainBinding + private val TAG = "LIFE_QUIZ" + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + // ViewBinding 초기화 + binding = ActivityMainBinding.inflate(layoutInflater) + setContentView(binding.root) + + Log.d(TAG, "onCreate") + + initNavigation() + } + + private fun initNavigation() { + val navHostFragment = supportFragmentManager + .findFragmentById(R.id.nav_host_fragment) as NavHostFragment + val navController = navHostFragment.navController + + // BottomNavigationView와 NavController 연결 + binding.bottomNav.setupWithNavController(navController) + } + + override fun onStart() { + super.onStart() + Log.d(TAG, "onStart") + } + + override fun onResume() { + super.onResume() + Log.d(TAG, "onResume") + } + + override fun onPause() { + super.onPause() + Log.d(TAG, "onPause") + } + + override fun onStop() { + super.onStop() + Log.d(TAG, "onStop") + } + + override fun onDestroy() { + super.onDestroy() + Log.d(TAG, "onDestroy") + } + + override fun onRestart() { + super.onRestart() + Log.d(TAG, "onRestart") + } +} diff --git a/week4/app/src/main/java/com/example/week2/Product.kt b/week4/app/src/main/java/com/example/week2/Product.kt new file mode 100644 index 0000000..3a45829 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/Product.kt @@ -0,0 +1,15 @@ +package com.example.week2 + +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +@Parcelize +data class Product( + val id: Int, + val name: String, + val description: String, + val price: String, + val imageResId: Int, + var isWishlisted: Boolean = false, + val category: String = "" +) : Parcelable diff --git a/week4/app/src/main/java/com/example/week2/ProductAdapter.kt b/week4/app/src/main/java/com/example/week2/ProductAdapter.kt new file mode 100644 index 0000000..7a7825f --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/ProductAdapter.kt @@ -0,0 +1,50 @@ +package com.example.week2 + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import com.example.week2.databinding.ItemProductBinding + +class ProductAdapter( + private var products: List, + private val onItemClick: (Product) -> Unit, + private val onWishlistClick: (Product, Int) -> Unit +) : RecyclerView.Adapter() { + + fun updateList(newProducts: List) { + this.products = newProducts + notifyDataSetChanged() + } + + inner class ProductViewHolder(private val binding: ItemProductBinding) : + RecyclerView.ViewHolder(binding.root) { + + fun bind(product: Product) { + binding.tvProductName.text = product.name + binding.tvProductDesc.text = product.description + binding.tvProductPrice.text = product.price + binding.ivProductImage.setImageResource(product.imageResId) + + val heartRes = if (product.isWishlisted) { + R.drawable.ic_heart_fill + } else { + R.drawable.ic_heart_empty + } + binding.ibWishlist.setImageResource(heartRes) + + binding.root.setOnClickListener { onItemClick(product) } + binding.ibWishlist.setOnClickListener { onWishlistClick(product, adapterPosition) } + } + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder { + val binding = ItemProductBinding.inflate(LayoutInflater.from(parent.context), parent, false) + return ProductViewHolder(binding) + } + + override fun onBindViewHolder(holder: ProductViewHolder, position: Int) { + holder.bind(products[position]) + } + + override fun getItemCount(): Int = products.size +} diff --git a/week4/app/src/main/java/com/example/week2/ProductDetailFragment.kt b/week4/app/src/main/java/com/example/week2/ProductDetailFragment.kt new file mode 100644 index 0000000..757f815 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/ProductDetailFragment.kt @@ -0,0 +1,82 @@ +package com.example.week2 + +import android.os.Build +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle +import androidx.navigation.fragment.findNavController +import com.example.week2.databinding.FragmentProductDetailBinding +import kotlinx.coroutines.launch + +class ProductDetailFragment : Fragment() { + private var _binding: FragmentProductDetailBinding? = null + private val binding get() = _binding!! + private lateinit var dataStoreManager: DataStoreManager + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentProductDetailBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + dataStoreManager = DataStoreManager(requireContext()) + + val product = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + arguments?.getParcelable("product", Product::class.java) + } else { + @Suppress("DEPRECATION") + arguments?.getParcelable("product") + } + + product?.let { currentProduct -> + binding.tvHeaderTitle.text = currentProduct.name + binding.ivDetailImage.setImageResource(currentProduct.imageResId) + binding.tvDetailCategory.text = currentProduct.category + binding.tvDetailName.text = currentProduct.name + binding.tvDetailPrice.text = currentProduct.price + + // 실시간 반영을 위해 DataStore에서 상태 관찰 + viewLifecycleOwner.lifecycleScope.launch { + viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { + dataStoreManager.productsFlow.collect { products -> + val updatedProduct = products.find { it.id == currentProduct.id } + updatedProduct?.let { + updateWishlistButton(it.isWishlisted) + } + } + } + } + } + + binding.ibBack.setOnClickListener { + findNavController().popBackStack() + } + + binding.btnWishlist.setOnClickListener { + product?.let { currentProduct -> + viewLifecycleOwner.lifecycleScope.launch { + val isCurrentlyWishlisted = binding.ivWishlistHeart.isSelected + dataStoreManager.updateWishlistStatus(currentProduct.id, !isCurrentlyWishlisted) + } + } + } + } + + private fun updateWishlistButton(isWishlisted: Boolean) { + binding.ivWishlistHeart.isSelected = isWishlisted + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/week4/app/src/main/java/com/example/week2/ProfileFragment.kt b/week4/app/src/main/java/com/example/week2/ProfileFragment.kt new file mode 100644 index 0000000..2d186cc --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/ProfileFragment.kt @@ -0,0 +1,26 @@ +package com.example.week2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.example.week2.databinding.FragmentProfileBinding + +class ProfileFragment : Fragment() { + private var _binding: FragmentProfileBinding? = null + private val binding get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentProfileBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/week4/app/src/main/java/com/example/week2/PurchaseFragment.kt b/week4/app/src/main/java/com/example/week2/PurchaseFragment.kt new file mode 100644 index 0000000..d9203f1 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/PurchaseFragment.kt @@ -0,0 +1,46 @@ +package com.example.week2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.example.week2.databinding.FragmentPurchaseBinding +import com.google.android.material.tabs.TabLayoutMediator + +class PurchaseFragment : Fragment() { + private var _binding: FragmentPurchaseBinding? = null + private val binding get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentPurchaseBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + setupViewPager() + } + + private fun setupViewPager() { + val adapter = PurchasePagerAdapter(this) + binding.viewPagerPurchase.adapter = adapter + + TabLayoutMediator(binding.tabLayoutPurchase, binding.viewPagerPurchase) { tab, position -> + tab.text = when (position) { + 0 -> "All" + 1 -> "Top & T-shirts" + 2 -> "Sale" + else -> null + } + }.attach() + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/week4/app/src/main/java/com/example/week2/PurchasePagerAdapter.kt b/week4/app/src/main/java/com/example/week2/PurchasePagerAdapter.kt new file mode 100644 index 0000000..c429682 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/PurchasePagerAdapter.kt @@ -0,0 +1,17 @@ +package com.example.week2 + +import androidx.fragment.app.Fragment +import androidx.viewpager2.adapter.FragmentStateAdapter + +class PurchasePagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) { + override fun getItemCount(): Int = 3 + + override fun createFragment(position: Int): Fragment { + return when (position) { + 0 -> AllProductsFragment() + 1 -> TopsTshirtsFragment() + 2 -> SaleFragment() + else -> AllProductsFragment() + } + } +} diff --git a/week4/app/src/main/java/com/example/week2/SaleFragment.kt b/week4/app/src/main/java/com/example/week2/SaleFragment.kt new file mode 100644 index 0000000..c307e51 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/SaleFragment.kt @@ -0,0 +1,31 @@ +package com.example.week2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.example.week2.databinding.FragmentEmptyTabBinding + +class SaleFragment : Fragment() { + private var _binding: FragmentEmptyTabBinding? = null + private val binding get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentEmptyTabBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + binding.tvMessage.text = getString(R.string.purchase_sale) + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/week4/app/src/main/java/com/example/week2/TopsTshirtsFragment.kt b/week4/app/src/main/java/com/example/week2/TopsTshirtsFragment.kt new file mode 100644 index 0000000..a0eb6f9 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/TopsTshirtsFragment.kt @@ -0,0 +1,31 @@ +package com.example.week2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.example.week2.databinding.FragmentEmptyTabBinding + +class TopsTshirtsFragment : Fragment() { + private var _binding: FragmentEmptyTabBinding? = null + private val binding get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentEmptyTabBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + binding.tvMessage.text = getString(R.string.purchase_tops_tshirts) + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/week4/app/src/main/java/com/example/week2/WishlistFragment.kt b/week4/app/src/main/java/com/example/week2/WishlistFragment.kt new file mode 100644 index 0000000..8833134 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/WishlistFragment.kt @@ -0,0 +1,66 @@ +package com.example.week2 + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope +import androidx.navigation.fragment.findNavController +import androidx.recyclerview.widget.GridLayoutManager +import com.example.week2.databinding.FragmentWishlistBinding +import kotlinx.coroutines.launch + +class WishlistFragment : Fragment() { + private var _binding: FragmentWishlistBinding? = null + private val binding get() = _binding!! + private lateinit var dataStoreManager: DataStoreManager + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentWishlistBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + dataStoreManager = DataStoreManager(requireContext()) + setupRecyclerView() + } + + private fun setupRecyclerView() { + viewLifecycleOwner.lifecycleScope.launch { + dataStoreManager.productsFlow.collect { products -> + val wishlistProducts = products.filter { it.isWishlisted } + val adapter = ProductAdapter( + wishlistProducts, + onItemClick = { product -> + navigateToDetail(product) + }, + onWishlistClick = { product, _ -> + viewLifecycleOwner.lifecycleScope.launch { + dataStoreManager.updateWishlistStatus(product.id, !product.isWishlisted) + } + } + ) + + binding.rvWishlistProducts.layoutManager = GridLayoutManager(context, 2) + binding.rvWishlistProducts.adapter = adapter + } + } + } + + private fun navigateToDetail(product: Product) { + val bundle = Bundle().apply { + putParcelable("product", product) + } + findNavController().navigate(R.id.action_wishlistFragment_to_productDetailFragment, bundle) + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/week4/app/src/main/java/com/example/week2/ui/theme/Color.kt b/week4/app/src/main/java/com/example/week2/ui/theme/Color.kt new file mode 100644 index 0000000..de069ca --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package com.example.week2.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) \ No newline at end of file diff --git a/week4/app/src/main/java/com/example/week2/ui/theme/Theme.kt b/week4/app/src/main/java/com/example/week2/ui/theme/Theme.kt new file mode 100644 index 0000000..9168083 --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/ui/theme/Theme.kt @@ -0,0 +1,58 @@ +package com.example.week2.ui.theme + +import android.app.Activity +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext + +private val DarkColorScheme = darkColorScheme( + primary = Purple80, + secondary = PurpleGrey80, + tertiary = Pink80 +) + +private val LightColorScheme = lightColorScheme( + primary = Purple40, + secondary = PurpleGrey40, + tertiary = Pink40 + + /* Other default colors to override + background = Color(0xFFFFFBFE), + surface = Color(0xFFFFFBFE), + onPrimary = Color.White, + onSecondary = Color.White, + onTertiary = Color.White, + onBackground = Color(0xFF1C1B1F), + onSurface = Color(0xFF1C1B1F), + */ +) + +@Composable +fun Week2Theme( + darkTheme: Boolean = isSystemInDarkTheme(), + // Dynamic color is available on Android 12+ + dynamicColor: Boolean = true, + content: @Composable () -> Unit +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + + MaterialTheme( + colorScheme = colorScheme, + typography = Typography, + content = content + ) +} \ No newline at end of file diff --git a/week4/app/src/main/java/com/example/week2/ui/theme/Type.kt b/week4/app/src/main/java/com/example/week2/ui/theme/Type.kt new file mode 100644 index 0000000..d0bf0ca --- /dev/null +++ b/week4/app/src/main/java/com/example/week2/ui/theme/Type.kt @@ -0,0 +1,34 @@ +package com.example.week2.ui.theme + +import androidx.compose.material3.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// Set of Material typography styles to start with +val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) + /* Other default text styles to override + titleLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 22.sp, + lineHeight = 28.sp, + letterSpacing = 0.sp + ), + labelSmall = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Medium, + fontSize = 11.sp, + lineHeight = 16.sp, + letterSpacing = 0.5.sp + ) + */ +) \ No newline at end of file diff --git a/week4/app/src/main/res/drawable/bg_black_round.xml b/week4/app/src/main/res/drawable/bg_black_round.xml new file mode 100644 index 0000000..fcc2357 --- /dev/null +++ b/week4/app/src/main/res/drawable/bg_black_round.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/week4/app/src/main/res/drawable/bg_white_stroke.xml b/week4/app/src/main/res/drawable/bg_white_stroke.xml new file mode 100644 index 0000000..4f8e667 --- /dev/null +++ b/week4/app/src/main/res/drawable/bg_white_stroke.xml @@ -0,0 +1,9 @@ + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_bagcircle.xml b/week4/app/src/main/res/drawable/ic_bagcircle.xml new file mode 100644 index 0000000..1af2ce6 --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_bagcircle.xml @@ -0,0 +1,26 @@ + + + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_bagsimple.xml b/week4/app/src/main/res/drawable/ic_bagsimple.xml new file mode 100644 index 0000000..3b6e816 --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_bagsimple.xml @@ -0,0 +1,21 @@ + + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_caretleft.xml b/week4/app/src/main/res/drawable/ic_caretleft.xml new file mode 100644 index 0000000..1912c2d --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_caretleft.xml @@ -0,0 +1,17 @@ + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_heart_empty.xml b/week4/app/src/main/res/drawable/ic_heart_empty.xml new file mode 100644 index 0000000..a7904b5 --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_heart_empty.xml @@ -0,0 +1,20 @@ + + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_heart_fill.xml b/week4/app/src/main/res/drawable/ic_heart_fill.xml new file mode 100644 index 0000000..91664d4 --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_heart_fill.xml @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_heart_filled.xml b/week4/app/src/main/res/drawable/ic_heart_filled.xml new file mode 100644 index 0000000..67df58b --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_heart_filled.xml @@ -0,0 +1,17 @@ + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_heartstraight.xml b/week4/app/src/main/res/drawable/ic_heartstraight.xml new file mode 100644 index 0000000..6597a4a --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_heartstraight.xml @@ -0,0 +1,17 @@ + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_homeimg.png b/week4/app/src/main/res/drawable/ic_homeimg.png new file mode 100644 index 0000000..f42aa9d Binary files /dev/null and b/week4/app/src/main/res/drawable/ic_homeimg.png differ diff --git a/week4/app/src/main/res/drawable/ic_housesimple.xml b/week4/app/src/main/res/drawable/ic_housesimple.xml new file mode 100644 index 0000000..a0d86b9 --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_housesimple.xml @@ -0,0 +1,17 @@ + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_launcher_background.xml b/week4/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_launcher_foreground.xml b/week4/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/week4/app/src/main/res/drawable/ic_listmagnifyingglass.xml b/week4/app/src/main/res/drawable/ic_listmagnifyingglass.xml new file mode 100644 index 0000000..483d117 --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_listmagnifyingglass.xml @@ -0,0 +1,38 @@ + + + + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_magnifyingglass.xml b/week4/app/src/main/res/drawable/ic_magnifyingglass.xml new file mode 100644 index 0000000..9077a42 --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_magnifyingglass.xml @@ -0,0 +1,24 @@ + + + + + + + diff --git a/week4/app/src/main/res/drawable/ic_user.xml b/week4/app/src/main/res/drawable/ic_user.xml new file mode 100644 index 0000000..1fc97b8 --- /dev/null +++ b/week4/app/src/main/res/drawable/ic_user.xml @@ -0,0 +1,23 @@ + + + + + + + diff --git a/week4/app/src/main/res/drawable/img_air_jordan_xxxvi.png b/week4/app/src/main/res/drawable/img_air_jordan_xxxvi.png new file mode 100644 index 0000000..19129df Binary files /dev/null and b/week4/app/src/main/res/drawable/img_air_jordan_xxxvi.png differ diff --git a/week4/app/src/main/res/drawable/img_nike_air_force.png b/week4/app/src/main/res/drawable/img_nike_air_force.png new file mode 100644 index 0000000..1fc5039 Binary files /dev/null and b/week4/app/src/main/res/drawable/img_nike_air_force.png differ diff --git a/week4/app/src/main/res/drawable/img_nike_everyday_plus_cushioned.png b/week4/app/src/main/res/drawable/img_nike_everyday_plus_cushioned.png new file mode 100644 index 0000000..9a23c22 Binary files /dev/null and b/week4/app/src/main/res/drawable/img_nike_everyday_plus_cushioned.png differ diff --git a/week4/app/src/main/res/drawable/img_training_ankle_socks.png b/week4/app/src/main/res/drawable/img_training_ankle_socks.png new file mode 100644 index 0000000..04a81da Binary files /dev/null and b/week4/app/src/main/res/drawable/img_training_ankle_socks.png differ diff --git a/week4/app/src/main/res/drawable/sl_item_wishlist_icon.xml b/week4/app/src/main/res/drawable/sl_item_wishlist_icon.xml new file mode 100644 index 0000000..7ca06ad --- /dev/null +++ b/week4/app/src/main/res/drawable/sl_item_wishlist_icon.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/week4/app/src/main/res/drawable/sl_wishlist_icon.xml b/week4/app/src/main/res/drawable/sl_wishlist_icon.xml new file mode 100644 index 0000000..3e176b9 --- /dev/null +++ b/week4/app/src/main/res/drawable/sl_wishlist_icon.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/week4/app/src/main/res/drawable/sl_wishlist_tint.xml b/week4/app/src/main/res/drawable/sl_wishlist_tint.xml new file mode 100644 index 0000000..78fdb7c --- /dev/null +++ b/week4/app/src/main/res/drawable/sl_wishlist_tint.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/week4/app/src/main/res/layout/activity_main.xml b/week4/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..376288c --- /dev/null +++ b/week4/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,33 @@ + + + + + + + + \ No newline at end of file diff --git a/week4/app/src/main/res/layout/fragment_all_products.xml b/week4/app/src/main/res/layout/fragment_all_products.xml new file mode 100644 index 0000000..f1c431f --- /dev/null +++ b/week4/app/src/main/res/layout/fragment_all_products.xml @@ -0,0 +1,16 @@ + + + + + + diff --git a/week4/app/src/main/res/layout/fragment_cart.xml b/week4/app/src/main/res/layout/fragment_cart.xml new file mode 100644 index 0000000..2c8bfbd --- /dev/null +++ b/week4/app/src/main/res/layout/fragment_cart.xml @@ -0,0 +1,45 @@ + + + + + + + +