Skip to content

Выполниз домашнее задание View-Resources #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
111 changes: 110 additions & 1 deletion app/src/main/java/otus/gpb/homework/viewandresources/CartActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,120 @@
package otus.gpb.homework.viewandresources

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class CartActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cart)

// Set click listener for the back button
findViewById<ImageButton>(R.id.btn_back).setOnClickListener({
// Handle back button click
onBackPressedDispatcher.onBackPressed() // Example action
})

// Set click listener for the close button
findViewById<ImageButton>(R.id.btn_close).setOnClickListener({
// Handle back button click
finish() // Example action
})

findViewById<com.google.android.material.button.MaterialButton>(R.id.place_order_button).setOnClickListener({
// Handle back button click
finish() // Example action
})

val recyclerView: RecyclerView = findViewById(R.id.rv_items) // Replace with your RecyclerView ID

val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.recycler_view_devider)!!)
recyclerView.addItemDecoration(divider)

// Sample data
val items = listOf(
CartItem(
iconResId = R.drawable.ic_cart_item, // Replace with your drawable resource
title = "Item 1",
price = "$43.88",
category = "Category 1",
note = "This is a note for Item 1"
),
CartItem(
iconResId = R.drawable.ic_cart_item,
title = "Item 2",
price = "$43.88",
category = "Category 2",
note = "This is a note for Item 2"
),
CartItem(
iconResId = R.drawable.ic_cart_item,
title = "Item 3",
price = "$43.87",
category = "Category 3",
note = "This is a note for Item 3"
),
CartItem(
iconResId = R.drawable.ic_cart_item,
title = "Item 4",
price = "$43.87",
category = "Category 4",
note = "This is a note for Item 4"
)

)

// Set up RecyclerView
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = CartAdapter(items)
}

data class CartItem(
val iconResId: Int, // Resource ID for the image
val title: String, // Title of the item
val price: String, // Price of the item
val category: String, // Category of the item
val note: String // Additional note
)

class CartAdapter(private val items: List<CartItem>) :
RecyclerView.Adapter<CartAdapter.CartViewHolder>() {

// ViewHolder to hold references to views in the item layout
class CartViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val icon: ImageView = itemView.findViewById(R.id.icon)
val title: TextView = itemView.findViewById(R.id.cart_item_title)
val price: TextView = itemView.findViewById(R.id.cart_item_price_val)
val category: TextView = itemView.findViewById(R.id.cart_item_category)
val note: TextView = itemView.findViewById(R.id.cart_item_note)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CartViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.activity_cart_list_item, parent, false) // Replace with your item layout
return CartViewHolder(view)
}

override fun onBindViewHolder(holder: CartViewHolder, position: Int) {
val item = items[position]
holder.icon.setImageResource(item.iconResId)
holder.title.text = item.title
holder.price.text = item.price
holder.category.text = item.category
holder.note.text = item.note
}

override fun getItemCount(): Int = items.size
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,57 @@
package otus.gpb.homework.viewandresources

import androidx.appcompat.app.AppCompatActivity
import android.net.Uri
import android.os.Bundle
import android.widget.ImageButton
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

class ContactsActivity : AppCompatActivity() {
// Define the ActivityResultLauncher
private lateinit var filePickerLauncher: ActivityResultLauncher<String>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_contacts)

// Set click listener for the back button
findViewById<ImageButton>(R.id.btn_back).setOnClickListener({
// Handle back button click
onBackPressedDispatcher.onBackPressed() // Example action
})

// Set click listener for the attach file icon
findViewById<ImageButton>(R.id.btn_attach).setOnClickListener({
// Handle attach file click
openFilePicker() // Example action
})

// Set click listener for the save button
findViewById<com.google.android.material.button.MaterialButton>(R.id.save_button).setOnClickListener({
finish() // Example action
})

// Initialize the ActivityResultLauncher
filePickerLauncher =
registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
if (uri != null) {
handleFileSelection(uri)
} else {
Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show()
}
}
}


private fun openFilePicker() {
filePickerLauncher.launch("*/*")
}

// Handle the selected file URI
private fun handleFileSelection(uri: Uri) {
Toast.makeText(this, "File selected: $uri", Toast.LENGTH_SHORT).show()
// Implement your logic to use the selected file
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,99 @@ package otus.gpb.homework.viewandresources
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationManagerCompat
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.contacts_button).setOnClickListener {
startActivity(Intent(this, ContactsActivity::class.java))
openContacts()
}

findViewById<Button>(R.id.cart_button).setOnClickListener {
startActivity(Intent(this, CartActivity::class.java))
openCart()
}

findViewById<Button>(R.id.signin_button).setOnClickListener {
MaterialAlertDialogBuilder(this)
.setView(R.layout.dialog_signin)
.show()
showSignInDialog()
}

// Set up click listener for the button
findViewById<ImageButton>(R.id.btn_main_menu).setOnClickListener {
showMainMenu()
}

}

// Function to show an MDC3-styled main menu
private fun showMainMenu() {
// Items to display in the menu
val menuItems = arrayOf("Contacts", "Cart", "Sign in", "Exit")

// Create a MaterialAlertDialogBuilder
MaterialAlertDialogBuilder(this)
.setTitle("Main Menu")
.setItems(menuItems) { _, which ->
// Handle menu item click
when (which) {
0 -> openContacts()
1 -> openCart()
2 -> showSignInDialog()
3 -> exitApp()
}
}
.show()
}

private fun exitApp() {
NotificationManagerCompat.from(this).cancelAll()
finishAndRemoveTask()
}

private fun openContacts() {
startActivity(Intent(this, ContactsActivity::class.java))
}

private fun openCart() {
startActivity(Intent(this, CartActivity::class.java))
}

private fun showSignInDialog() {
// Создаем диалог
val dialog = MaterialAlertDialogBuilder(this)
.setView(R.layout.dialog_signin)
.create()

// Показываем диалог
dialog.show()

// Получаем ссылки на кнопки из кастомного макета
val loginButton = dialog.findViewById<MaterialButton>(R.id.login_button)
val cancelButton = dialog.findViewById<MaterialButton>(R.id.cancel_button)

// Обработчики нажатий
loginButton?.setOnClickListener {
val loginInput = dialog.findViewById<TextInputEditText>(R.id.login_input)?.text.toString()
val passwordInput = dialog.findViewById<TextInputEditText>(R.id.password_input)?.text.toString()

// Ваш код для входа
if (loginInput.isNotEmpty() && passwordInput.isNotEmpty()) {
Toast.makeText(this, "Login: $loginInput, Password: $passwordInput", Toast.LENGTH_SHORT).show()
dialog.dismiss() // Закрыть диалог после успешного ввода
} else {
Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show()
}
}

cancelButton?.setOnClickListener {
dialog.dismiss() // Закрыть диалог при нажатии на "Отмена"
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/card_item_ico_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorOnPrimary" />
<corners android:radius="24dp" />
<padding
android:left="12dp"
android:top="12dp"
android:right="12dp"
android:bottom="12dp" />
</shape>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_account.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/colorOnSurface"
android:pathData="M12,4A4,4 0 0,1 16,8A4,4 0 0,1 12,12A4,4 0 0,1 8,8A4,4 0 0,1 12,4M12,14C16.42,14 20,15.79 20,18V20H4V18C4,15.79 7.58,14 12,14Z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_arrow_back.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/colorOnSurface"
android:pathData="M20,11V13H8L13.5,18.5L12.08,19.92L4.16,12L12.08,4.08L13.5,5.5L8,11H20Z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_attach_file.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/colorOutline"
android:pathData="M16.5,6V17.5A4,4 0 0,1 12.5,21.5A4,4 0 0,1 8.5,17.5V5A2.5,2.5 0 0,1 11,2.5A2.5,2.5 0 0,1 13.5,5V15.5A1,1 0 0,1 12.5,16.5A1,1 0 0,1 11.5,15.5V6H10V15.5A2.5,2.5 0 0,0 12.5,18A2.5,2.5 0 0,0 15,15.5V5A4,4 0 0,0 11,1A4,4 0 0,0 7,5V17.5A5.5,5.5 0 0,0 12.5,23A5.5,5.5 0 0,0 18,17.5V6H16.5Z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_bookmark_outline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/colorOnSurface"
android:pathData="M17,18L12,15.82L7,18V5H17M17,3H7A2,2 0 0,0 5,5V21L12,18L19,21V5C19,3.89 18.1,3 17,3Z" />
</vector>
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_cart_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/colorOutline"
android:pathData="M11,13.5V21.5H3V13.5H11M12,2L17.5,11H6.5L12,2M17.5,13C20,13 22,15 22,17.5C22,20 20,22 17.5,22C15,22 13,20 13,17.5C13,15 15,13 17.5,13Z" />


</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_cell_phone_check.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/colorOnSurface"
android:pathData="M14.54 23H7C5.9 23 5 22.11 5 21V3C5 1.89 5.89 1 7 1H17C18.1 1 19 1.89 19 3V13C18.3 13 17.63 13.13 17 13.35V5H7V19H13C13 20.54 13.58 21.94 14.54 23M17.75 22.16L15 19.16L16.16 18L17.75 19.59L21.34 16L22.5 17.41L17.75 22.16" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_close_circle_outline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/colorOnSurface"
android:pathData="M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2C6.47,2 2,6.47 2,12C2,17.53 6.47,22 12,22C17.53,22 22,17.53 22,12C22,6.47 17.53,2 12,2M14.59,8L12,10.59L9.41,8L8,9.41L10.59,12L8,14.59L9.41,16L12,13.41L14.59,16L16,14.59L13.41,12L16,9.41L14.59,8Z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_date_picker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/colorOnSurface"
android:pathData="M7,10H12V15H7M19,19H5V8H19M19,3H18V1H16V3H8V1H6V3H5C3.89,3 3,3.9 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5A2,2 0 0,0 19,3Z" />
</vector>
Loading