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 0000000..d1b5202 Binary files /dev/null and b/app/src/main/res/drawable/cart_item.png differ 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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +