-
Notifications
You must be signed in to change notification settings - Fork 71
View-Resources-2 #60
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
gitIliyaM
wants to merge
1
commit into
Android-Developer-Basic:master
Choose a base branch
from
gitIliyaM:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
View-Resources-2 #60
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
93 changes: 92 additions & 1 deletion
93
app/src/main/java/otus/gpb/homework/viewandresources/CartActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
98 changes: 97 additions & 1 deletion
98
app/src/main/java/otus/gpb/homework/viewandresources/ContactsActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Код для работы с темой дублируется в трех активити. Это, конечно, учебный проект, но микро зарубку, что можно еще краше сделать код давайте запомним :)