From f25203ba36f6eb4e4b1a622c41eda8635de28df9 Mon Sep 17 00:00:00 2001 From: Ekaterina Leonidova Date: Sun, 29 Dec 2024 20:10:01 +0300 Subject: [PATCH] Homework 10 --- app/build.gradle | 1 + .../gpb/homework/viewandresources/CartItem.kt | 93 +++++++++ app/src/main/res/drawable/account_outline.xml | 1 + app/src/main/res/drawable/arrow_back_24px.xml | 11 + .../main/res/drawable/attach_file_24px.xml | 10 + .../main/res/drawable/bookmark_outline.xml | 1 + app/src/main/res/drawable/calendar.xml | 1 + app/src/main/res/drawable/cancel_24px.xml | 10 + app/src/main/res/drawable/cart_item.png | Bin 0 -> 1717 bytes app/src/main/res/drawable/cellphone_check.xml | 1 + .../main/res/drawable/emoticon_outline.xml | 1 + app/src/main/res/drawable/microphone.xml | 1 + app/src/main/res/drawable/mood_24px.xml | 10 + app/src/main/res/layout/activity_cart.xml | 177 +++++++++++++++- app/src/main/res/layout/activity_contacts.xml | 190 +++++++++++++++++- app/src/main/res/layout/activity_main.xml | 6 +- app/src/main/res/layout/cart_item.xml | 58 ++++++ app/src/main/res/layout/dialog_signin.xml | 81 +++++++- app/src/main/res/menu/cart_app_bar.xml | 10 + app/src/main/res/menu/contacts_app_bar.xml | 17 ++ app/src/main/res/values-night/themes.xml | 25 ++- app/src/main/res/values/arrays.xml | 7 + app/src/main/res/values/attrs.xml | 11 + app/src/main/res/values/colors.xml | 7 + app/src/main/res/values/strings.xml | 27 +++ app/src/main/res/values/styles.xml | 14 ++ app/src/main/res/values/themes.xml | 24 ++- build.gradle | 4 +- 28 files changed, 774 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/otus/gpb/homework/viewandresources/CartItem.kt create mode 100644 app/src/main/res/drawable/account_outline.xml create mode 100644 app/src/main/res/drawable/arrow_back_24px.xml create mode 100644 app/src/main/res/drawable/attach_file_24px.xml create mode 100644 app/src/main/res/drawable/bookmark_outline.xml create mode 100644 app/src/main/res/drawable/calendar.xml create mode 100644 app/src/main/res/drawable/cancel_24px.xml create mode 100644 app/src/main/res/drawable/cart_item.png create mode 100644 app/src/main/res/drawable/cellphone_check.xml create mode 100644 app/src/main/res/drawable/emoticon_outline.xml create mode 100644 app/src/main/res/drawable/microphone.xml create mode 100644 app/src/main/res/drawable/mood_24px.xml create mode 100644 app/src/main/res/layout/cart_item.xml create mode 100644 app/src/main/res/menu/cart_app_bar.xml create mode 100644 app/src/main/res/menu/contacts_app_bar.xml create mode 100644 app/src/main/res/values/arrays.xml create mode 100644 app/src/main/res/values/attrs.xml create mode 100644 app/src/main/res/values/styles.xml diff --git a/app/build.gradle b/app/build.gradle index 8df24ea..c4e6a8c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,7 @@ plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' + id("kotlin-parcelize") } android { diff --git a/app/src/main/java/otus/gpb/homework/viewandresources/CartItem.kt b/app/src/main/java/otus/gpb/homework/viewandresources/CartItem.kt new file mode 100644 index 0000000..b84808c --- /dev/null +++ b/app/src/main/java/otus/gpb/homework/viewandresources/CartItem.kt @@ -0,0 +1,93 @@ +package otus.gpb.homework.viewandresources +import android.content.Context +import android.os.Parcelable +import android.util.AttributeSet +import android.view.Gravity.CENTER +import android.widget.TextView +import androidx.appcompat.widget.LinearLayoutCompat +import androidx.core.content.withStyledAttributes +import kotlinx.parcelize.Parcelize +class CartItem @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = R.attr.cart_item_style +) : LinearLayoutCompat(context, attrs, defStyleAttr) { + private var title: String = "" + set(value) { + field = value + findViewById(R.id.itemTitle).text = value + } + + private var category: String = "" + set(value) { + field = value + findViewById(R.id.itemCategory).text = value + } + + private var price: String = "" + set(value) { + field = value + findViewById(R.id.itemPrice).text = value + } + + init { + isSaveEnabled = true + inflate(getContext(), R.layout.cart_item, this) + initPanel(attrs, defStyleAttr) + initItemData(attrs, defStyleAttr) + } + + @Parcelize + private data class CartItemState(val superState: Parcelable?, val title: String, val category: String, val price: String): Parcelable + + override fun onSaveInstanceState(): Parcelable { + return CartItemState(super.onSaveInstanceState(), title, category, price) + } + + override fun onRestoreInstanceState(state: Parcelable?) { + if (state is CartItemState) { + super.onRestoreInstanceState(state.superState) + title = state.title + category = state.category + price = state.price + } + } + + private fun initPanel(attrs: AttributeSet?, defStyleAttr: Int) { + // for android limitation, before finding attrs, you need to sort all attrs you want to find in ascending order, otherwise some attrs cannot be found + // https://developer.android.com/reference/android/content/res/Resources.Theme.html#obtainStyledAttributes(android.util.AttributeSet, int[], int, int) + val toRetrieve = intArrayOf( + android.R.attr.layout_width, + android.R.attr.layout_height, + android.R.attr.orientation, + android.R.attr.gravity, + android.R.attr.paddingTop, + android.R.attr.paddingBottom, + android.R.attr.background + ).apply { sort() } + + context.withStyledAttributes(attrs, toRetrieve, defStyleAttr, R.style.cart_item) { + layoutParams = LayoutParams( + getInt(toRetrieve.indexOf(android.R.attr.layout_width), LayoutParams.WRAP_CONTENT), + getInt(toRetrieve.indexOf(android.R.attr.layout_height), LayoutParams.WRAP_CONTENT), + ) + orientation = getInt(toRetrieve.indexOf(android.R.attr.orientation), HORIZONTAL) + gravity = getInt(toRetrieve.indexOf(android.R.attr.gravity), CENTER) + setPadding( + 0, + getDimensionPixelSize(toRetrieve.indexOf(android.R.attr.paddingTop), 0), + 0, + getDimensionPixelSize(toRetrieve.indexOf(android.R.attr.paddingBottom), 0), + ) + background = getDrawable(toRetrieve.indexOf(android.R.attr.background)) + } + } + + private fun initItemData(attrs: AttributeSet?, defStyleAttr: Int) { + context.withStyledAttributes(attrs, R.styleable.CartItem, defStyleAttr, R.style.cart_item) { + title = getString(R.styleable.CartItem_item_title) ?: "" + category = getString(R.styleable.CartItem_item_category) ?: "" + price = getString(R.styleable.CartItem_item_price) ?: "" + } + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/account_outline.xml b/app/src/main/res/drawable/account_outline.xml new file mode 100644 index 0000000..b4b1b35 --- /dev/null +++ b/app/src/main/res/drawable/account_outline.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/src/main/res/drawable/arrow_back_24px.xml b/app/src/main/res/drawable/arrow_back_24px.xml new file mode 100644 index 0000000..0e2e863 --- /dev/null +++ b/app/src/main/res/drawable/arrow_back_24px.xml @@ -0,0 +1,11 @@ + + + diff --git a/app/src/main/res/drawable/attach_file_24px.xml b/app/src/main/res/drawable/attach_file_24px.xml new file mode 100644 index 0000000..6d9ac73 --- /dev/null +++ b/app/src/main/res/drawable/attach_file_24px.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/bookmark_outline.xml b/app/src/main/res/drawable/bookmark_outline.xml new file mode 100644 index 0000000..057a729 --- /dev/null +++ b/app/src/main/res/drawable/bookmark_outline.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/src/main/res/drawable/calendar.xml b/app/src/main/res/drawable/calendar.xml new file mode 100644 index 0000000..9bd05bf --- /dev/null +++ b/app/src/main/res/drawable/calendar.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/src/main/res/drawable/cancel_24px.xml b/app/src/main/res/drawable/cancel_24px.xml new file mode 100644 index 0000000..ea3a291 --- /dev/null +++ b/app/src/main/res/drawable/cancel_24px.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/cart_item.png b/app/src/main/res/drawable/cart_item.png new file mode 100644 index 0000000000000000000000000000000000000000..d1b520260f4e8dd58db5dcc208bb1525fc212125 GIT binary patch literal 1717 zcmV;m21@yfP)|&s^>=rH&vt?Qx~0DBX8cWb`ThQ< z{-79{z&%>`N@eSLoo3X@qaTGM5y+!&gQBV3%A!;*oUBRTWOarQayi znxPBipyA}GsZ!%%V}upT&;@Q$e~+iIlW;Kkx`Q(;hAJ?d%b{32g43v~W~`}vJ8Xs~ zuwJiI?l!mzqCiVSE}zG+8JfUtN_+>|j^gB6p_8pZOQitha)*59Sw zxntuEbl2^Pcnx2mO?-hi@det%7ibe-piO*%HVI;k;6$4DmrJFR(X6dE>PCGW2m~+{ z4kI=l#r*6n;_*0w!2q1VbRx}}{sHaHsx4!PDmk0 zU<}t&NfJ5@o9Lzm^5*u-SFasCPvz3W0&f0r9aiAoIo?|-9GE-JTad_wxjEdpe$CEk z)GbY6r%&?HVeR!=Gz-@LS+}&e!oNe7x;K)h_l;{QNZIp)t8MXY5aIRi_1m|$Hl~=} zY!?5%-+)y-E0DJZcsX%`y?hQw7EdMiq`(#hvTmosnMKod>S(8pCBZD76u6hmJKpyz z(^618GQZv6Ie|Rt_6r3#4R6#J4i8K}&vOFn^~S(XEUHwhFpK8|Hku9ENN-{gRIBE< z9XuzH7yLn;vENd^fgr*OFIN$RARx`_@SMO2B z@oY1J8=~A8b%OcX^SFHJBCOz9fjnAnB$EgR?6v0Ou^3W|i?E7$fdfdvgvA%-5qSIO zTgLij`g>9^nms>*+qaf%MfD)PPSMP#)}vEXxcdDSN6$;1pF0n$u)Lpc{z&buZR+dg z`)YAXN|>)+`3_euU!r!t^_-HvQ&FtHPp{Db?qCeyH@JNA;M&{znZAW7Gfi$<-?9UGO}{@kYG5y!$NXx((O z3=~qV28D*dXeGRlF@l9fBM}T7U3($vO?k%aAbd>4j%uN^>n)xNPUAgwN^7`&u5; zwwb^OhNQmi2bPZkqe3p)$zyu|F#;NmQpH0m_r|JP=>``TD&@vHrFWggogRxmSy7Zk zRK-1=FBwYB2P&7YQUY$i2kt_sK0c)Sd5grYf=b&8RcD_Eif;S|Shd^ehpn>h00000 LNkvXXu0mjfql`90 literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable/cellphone_check.xml b/app/src/main/res/drawable/cellphone_check.xml new file mode 100644 index 0000000..3a71709 --- /dev/null +++ b/app/src/main/res/drawable/cellphone_check.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/src/main/res/drawable/emoticon_outline.xml b/app/src/main/res/drawable/emoticon_outline.xml new file mode 100644 index 0000000..cf70254 --- /dev/null +++ b/app/src/main/res/drawable/emoticon_outline.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/src/main/res/drawable/microphone.xml b/app/src/main/res/drawable/microphone.xml new file mode 100644 index 0000000..ed375ff --- /dev/null +++ b/app/src/main/res/drawable/microphone.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/src/main/res/drawable/mood_24px.xml b/app/src/main/res/drawable/mood_24px.xml new file mode 100644 index 0000000..947ca7f --- /dev/null +++ b/app/src/main/res/drawable/mood_24px.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/activity_cart.xml b/app/src/main/res/layout/activity_cart.xml index 57dc4d4..99b9776 100644 --- a/app/src/main/res/layout/activity_cart.xml +++ b/app/src/main/res/layout/activity_cart.xml @@ -1,9 +1,176 @@ - + android:layout_height="match_parent"> - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +