Skip to content

Homework 10 #42

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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id("kotlin-parcelize")
}

android {
Expand Down
93 changes: 93 additions & 0 deletions app/src/main/java/otus/gpb/homework/viewandresources/CartItem.kt
Original file line number Diff line number Diff line change
@@ -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<TextView>(R.id.itemTitle).text = value
}

private var category: String = ""
set(value) {
field = value
findViewById<TextView>(R.id.itemCategory).text = value
}

private var price: String = ""
set(value) {
field = value
findViewById<TextView>(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) ?: ""
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/drawable/account_outline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- drawable/account_outline.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24"><path android:fillColor="#000000" 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,6A2,2 0 0,0 10,8A2,2 0 0,0 12,10A2,2 0 0,0 14,8A2,2 0 0,0 12,6M12,13C14.67,13 20,14.33 20,17V20H4V17C4,14.33 9.33,13 12,13M12,14.9C9.03,14.9 5.9,16.36 5.9,17V18.1H18.1V17C18.1,16.36 14.97,14.9 12,14.9Z" /></vector>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/arrow_back_24px.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal"
android:autoMirrored="true">
<path
android:fillColor="@android:color/white"
android:pathData="M313,520L537,744L480,800L160,480L480,160L537,216L313,440L800,440L800,520L313,520Z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/attach_file_24px.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="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M720,630Q720,734 647,807Q574,880 470,880Q366,880 293,807Q220,734 220,630L220,260Q220,185 272.5,132.5Q325,80 400,80Q475,80 527.5,132.5Q580,185 580,260L580,610Q580,656 548,688Q516,720 470,720Q424,720 392,688Q360,656 360,610L360,240L440,240L440,610Q440,623 448.5,631.5Q457,640 470,640Q483,640 491.5,631.5Q500,623 500,610L500,260Q499,218 470.5,189Q442,160 400,160Q358,160 329,189Q300,218 300,260L300,630Q299,701 349,750.5Q399,800 470,800Q540,800 589,750.5Q638,701 640,630L640,240L720,240L720,630Z"/>
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/drawable/bookmark_outline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- drawable/bookmark_outline.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24"><path android:fillColor="#000000" android:pathData="M17,18L12,15.82L7,18V5H17M17,3H7A2,2 0 0,0 5,5V21L12,18L19,21V5C19,3.89 18.1,3 17,3Z" /></vector>
1 change: 1 addition & 0 deletions app/src/main/res/drawable/calendar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- drawable/calendar.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24"><path android:fillColor="#000000" android:pathData="M19,19H5V8H19M16,1V3H8V1H6V3H5C3.89,3 3,3.89 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5C21,3.89 20.1,3 19,3H18V1M17,12H12V17H17V12Z" /></vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/cancel_24px.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="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M336,680L480,536L624,680L680,624L536,480L680,336L624,280L480,424L336,280L280,336L424,480L280,624L336,680ZM480,880Q397,880 324,848.5Q251,817 197,763Q143,709 111.5,636Q80,563 80,480Q80,397 111.5,324Q143,251 197,197Q251,143 324,111.5Q397,80 480,80Q563,80 636,111.5Q709,143 763,197Q817,251 848.5,324Q880,397 880,480Q880,563 848.5,636Q817,709 763,763Q709,817 636,848.5Q563,880 480,880ZM480,800Q614,800 707,707Q800,614 800,480Q800,346 707,253Q614,160 480,160Q346,160 253,253Q160,346 160,480Q160,614 253,707Q346,800 480,800ZM480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Z"/>
</vector>
Binary file added app/src/main/res/drawable/cart_item.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/src/main/res/drawable/cellphone_check.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- drawable/cellphone_check.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24"><path android:fillColor="#000000" 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>
1 change: 1 addition & 0 deletions app/src/main/res/drawable/emoticon_outline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- drawable/emoticon_outline.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24"><path android:fillColor="#000000" android:pathData="M12,17.5C14.33,17.5 16.3,16.04 17.11,14H6.89C7.69,16.04 9.67,17.5 12,17.5M8.5,11A1.5,1.5 0 0,0 10,9.5A1.5,1.5 0 0,0 8.5,8A1.5,1.5 0 0,0 7,9.5A1.5,1.5 0 0,0 8.5,11M15.5,11A1.5,1.5 0 0,0 17,9.5A1.5,1.5 0 0,0 15.5,8A1.5,1.5 0 0,0 14,9.5A1.5,1.5 0 0,0 15.5,11M12,20A8,8 0 0,1 4,12A8,8 0 0,1 12,4A8,8 0 0,1 20,12A8,8 0 0,1 12,20M12,2C6.47,2 2,6.5 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z" /></vector>
1 change: 1 addition & 0 deletions app/src/main/res/drawable/microphone.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- drawable/microphone.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24"><path android:fillColor="#000000" android:pathData="M12,2A3,3 0 0,1 15,5V11A3,3 0 0,1 12,14A3,3 0 0,1 9,11V5A3,3 0 0,1 12,2M19,11C19,14.53 16.39,17.44 13,17.93V21H11V17.93C7.61,17.44 5,14.53 5,11H7A5,5 0 0,0 12,16A5,5 0 0,0 17,11H19Z" /></vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/mood_24px.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="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M620,440Q645,440 662.5,422.5Q680,405 680,380Q680,355 662.5,337.5Q645,320 620,320Q595,320 577.5,337.5Q560,355 560,380Q560,405 577.5,422.5Q595,440 620,440ZM340,440Q365,440 382.5,422.5Q400,405 400,380Q400,355 382.5,337.5Q365,320 340,320Q315,320 297.5,337.5Q280,355 280,380Q280,405 297.5,422.5Q315,440 340,440ZM480,700Q548,700 603.5,661.5Q659,623 684,560L276,560Q301,623 356.5,661.5Q412,700 480,700ZM480,880Q397,880 324,848.5Q251,817 197,763Q143,709 111.5,636Q80,563 80,480Q80,397 111.5,324Q143,251 197,197Q251,143 324,111.5Q397,80 480,80Q563,80 636,111.5Q709,143 763,197Q817,251 848.5,324Q880,397 880,480Q880,563 848.5,636Q817,709 763,763Q709,817 636,848.5Q563,880 480,880ZM480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480ZM480,800Q614,800 707,707Q800,614 800,480Q800,346 707,253Q614,160 480,160Q346,160 253,253Q160,346 160,480Q160,614 253,707Q346,800 480,800Z"/>
</vector>
177 changes: 172 additions & 5 deletions app/src/main/res/layout/activity_cart.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,176 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CartActivity">
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:menu="@menu/cart_app_bar"
app:navigationIcon="@drawable/arrow_back_24px"
app:title="Cart" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:paddingStart="16dp"
android:paddingEnd="16dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<otus.gpb.homework.viewandresources.CartItem
android:id="@+id/cartItem1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:item_category="cat 1"
app:item_price="12$"
app:item_title="Item 1" />

<otus.gpb.homework.viewandresources.CartItem
android:id="@+id/cartItem2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:item_category="cat 2"
app:item_price="23$"
app:item_title="Another item" />

<otus.gpb.homework.viewandresources.CartItem
android:id="@+id/cartItem3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:item_category="cat 3"
app:item_price="43$"
app:item_title="Something" />

<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/order_total"
android:textSize="22sp" />

<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="10" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="180$"
android:textSize="22sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/subtotal"
android:textStyle="bold"
android:textSize="16sp" />

<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="10" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="79$"
android:textStyle="bold"
android:textSize="16sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/shipping"
android:textStyle="bold"
android:textSize="16sp" />

<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="10" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="1$"
android:textStyle="bold"
android:textSize="16sp" />
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tax"
android:textStyle="bold"
android:textSize="16sp" />

<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="10" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="100$"
android:textStyle="bold"
android:textSize="16sp" />
</LinearLayout>

<Button
style="@style/Widget.Material3.Button.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/place_order"
app:icon="@drawable/mood_24px" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Loading