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
23 changes: 0 additions & 23 deletions .gitignore

This file was deleted.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
Задание содержит 3 страницы:
1. `Contacts` - страница контакта записной книжки
2. `Cart` - страница корзины
3. `Login` - [MaterialAlertDialog](https://m2.material.io/components/dialogs/android) формы логина
3. `Login` - [MaterialAlertDialog](https://m2.material.io/components/dialogs/android) формы логина. создай
Simple dialog
Simple dialogs display a list of items that take immediate effect when selected. При логине вводим имейл и попадаем в личную кабинет
, который выглядит как картинка с названием файла Contacts Day.png.

Для первого и второго задания в проекте сделаны две пустых `Activity`:
1. `ContactsActivity`
Expand Down
1 change: 0 additions & 1 deletion app/.gitignore

This file was deleted.

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

import androidx.appcompat.app.AppCompatActivity
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.google.android.material.button.MaterialButton
import kotlin.random.Random

class CartActivity : AppCompatActivity() {

private lateinit var recyclerView: RecyclerView
private lateinit var adapter: CartAdapter
private lateinit var totalAmountTextView: TextView

private val cartItems = mutableListOf<CartItem>()
private companion object {
const val PREFS_NAME = "ThemePrefs"
const val KEY_DARK_THEME = "dark_theme"
}

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

recyclerView = findViewById(R.id.cart_recycler_view)
totalAmountTextView = findViewById(R.id.total_amount)

for (i in 1..5) {
cartItems.add(CartItem("Item $i", 10.0 + i, Random.nextInt(1, 5)))
}

adapter = CartAdapter(cartItems) { item ->
cartItems.remove(item)
adapter.notifyItemRemoved(cartItems.indexOf(item))
updateTotal()
}
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)

updateTotal()
}

private fun updateTotal() {
val total = cartItems.sumOf { it.price * it.quantity }
totalAmountTextView.text = getString(R.string.item_price_template, total)
}

data class CartItem(val name: String, val price: Double, val quantity: Int)

inner class CartAdapter(
private val items: MutableList<CartItem>,
private val onDeleteClick: (CartItem) -> Unit
) : RecyclerView.Adapter<CartAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_cart_product, parent, false)
return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items[position]
holder.bind(item)
}

override fun getItemCount() = items.size

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val nameTextView: TextView = itemView.findViewById(R.id.product_name)
private val priceTextView: TextView = itemView.findViewById(R.id.product_price)
private val quantityTextView: TextView = itemView.findViewById(R.id.product_quantity)
private val deleteButton: MaterialButton = itemView.findViewById(R.id.delete_button)

fun bind(item: CartItem) {
nameTextView.text = item.name
priceTextView.text = itemView.context.getString(R.string.item_price_template, item.price)
quantityTextView.text = itemView.context.getString(R.string.item_quantity_template, item.quantity)

deleteButton.setOnClickListener {
val position = adapterPosition
if (position != RecyclerView.NO_POSITION) {
onDeleteClick(items[position])
}
}
}
}
}
private fun applyTheme() {
val sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val isDarkTheme = sharedPreferences.getBoolean(KEY_DARK_THEME, false)
val mode = if (isDarkTheme) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO
AppCompatDelegate.setDefaultNightMode(mode)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,107 @@
package otus.gpb.homework.viewandresources

import androidx.appcompat.app.AppCompatActivity
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import com.google.android.material.textfield.TextInputEditText
import java.text.SimpleDateFormat
import java.util.*

class ContactsActivity : AppCompatActivity() {

private lateinit var dateInputEditText: TextInputEditText
private lateinit var phoneTypeAutoComplete: AutoCompleteTextView
private lateinit var themeToggleButton: View
private companion object {
const val PREFS_NAME = "ThemePrefs"
const val KEY_DARK_THEME = "dark_theme"
}

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

dateInputEditText = findViewById(R.id.date_input_edit_text)
phoneTypeAutoComplete = findViewById(R.id.phone_type_autocomplete)
themeToggleButton = findViewById(R.id.theme_toggle_button)

dateInputEditText.setOnClickListener {
showDatePicker()
}

setupPhoneTypeSpinner()

themeToggleButton.setOnClickListener {
toggleTheme()
}
}

private fun showDatePicker() {
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)

val datePickerDialog = android.app.DatePickerDialog(
this,
{ _, selectedYear, selectedMonth, selectedDayOfMonth ->
val selectedDate = Calendar.getInstance().apply {
set(selectedYear, selectedMonth, selectedDayOfMonth)
}
val dateFormat = SimpleDateFormat("dd.MM.yyyy", Locale.getDefault())
val formattedDate = dateFormat.format(selectedDate.time)

dateInputEditText.setText(formattedDate)
},
year,
month,
dayOfMonth
)

datePickerDialog.show()
}

private fun setupPhoneTypeSpinner() {
val phoneTypes = arrayOf("Mobile", "Home", "Work", "Fax", "Other")

val adapter = ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, phoneTypes)

phoneTypeAutoComplete.setAdapter(adapter)
}

private fun toggleTheme() {
val sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val isDarkTheme = sharedPreferences.getBoolean(KEY_DARK_THEME, false)

val newNightMode = if (isDarkTheme) {
AppCompatDelegate.MODE_NIGHT_NO
} else {
AppCompatDelegate.MODE_NIGHT_YES
}

with(sharedPreferences.edit()) {
putBoolean(KEY_DARK_THEME, newNightMode == AppCompatDelegate.MODE_NIGHT_YES)
apply()
}

AppCompatDelegate.setDefaultNightMode(newNightMode)

recreate()
}

fun onBackPressed(view: View) {
finish()
}
private fun applyTheme() {
val sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val isDarkTheme = sharedPreferences.getBoolean(KEY_DARK_THEME, false)

val mode = if (isDarkTheme) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO
AppCompatDelegate.setDefaultNightMode(mode)
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package otus.gpb.homework.viewandresources

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class MainActivity : AppCompatActivity() {
private companion object {
const val PREFS_NAME = "ThemePrefs"
const val KEY_DARK_THEME = "dark_theme"
}

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

findViewById<Button>(R.id.contacts_button).setOnClickListener {
startActivity(Intent(this, ContactsActivity::class.java))
}
Expand All @@ -19,9 +29,31 @@ class MainActivity : AppCompatActivity() {
}

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

private fun showSignInDialog() {
val dialogView = layoutInflater.inflate(R.layout.dialog_signin, null)
val emailInput = dialogView.findViewById<EditText>(R.id.email_input)

MaterialAlertDialogBuilder(this)
.setTitle(R.string.login_dialog_title)
.setView(dialogView)
.setPositiveButton(R.string.sign_in_button_text) { _, _ ->
val email = emailInput.text.toString()
val intent = Intent(this, ContactsActivity::class.java)
startActivity(intent)
}
.setNegativeButton(R.string.cancel_button_text, null)
.show()
}

private fun applyTheme() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Код для работы с темой дублируется в трех активити. Это, конечно, учебный проект, но микро зарубку, что можно еще краше сделать код давайте запомним :)

val sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val isDarkTheme = sharedPreferences.getBoolean(KEY_DARK_THEME, false)

val mode = if (isDarkTheme) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO
AppCompatDelegate.setDefaultNightMode(mode)
}
}
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 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorOnSurface">
<path
android:fillColor="@android:color/white"
android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_calendar_month.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorOnSurface">
<path
android:fillColor="@android:color/white"
android:pathData="M19,3h-1V1h-2v2H8V1H6v2H5c-1.11,0 -2,0.89 -2,2v14c0,1.11 0.89,2 2,2h14c1.11,0 2,-0.89 2,-2V5c0,-1.11 -0.89,-2 -2,-2zM19,19H5V8h14v11z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_delete.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorOnSurface">
<path
android:fillColor="@android:color/white"
android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_edit_note.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorOnSurface">
<path
android:fillColor="@android:color/white"
android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_email.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorOnSurface">
<path
android:fillColor="@android:color/white"
android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_home.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorOnSurface">
<path
android:fillColor="@android:color/white"
android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z"/>
</vector>
Loading