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
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".Controller.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Controller.ProductsActivity"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.devf5.arthursalgado.coderswag.Adapters

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.devf5.arthursalgado.coderswag.Model.Category
import com.devf5.arthursalgado.coderswag.R

/**
* Created by arthursalgado on 13/10/17.
*/
class CategoryAdapter constructor(val context: Context, val categories: List<Category>) : BaseAdapter() {

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val categoryView : View
val holder: ViewHolder

if(convertView == null) {
categoryView = LayoutInflater.from(context).inflate(R.layout.category_list_item, null)
holder = ViewHolder()
holder.categoryImage = categoryView.findViewById(R.id.categoryImage)
holder.categoryName = categoryView.findViewById(R.id.categoryName)

categoryView.tag = holder // valor de cada view é o holder (unique value)
} else {
// Reutilizar a celular se já foi criada
holder = convertView.tag as ViewHolder
categoryView = convertView
}

val category = categories[position]
holder.categoryName?.text = category.title
val resourceId = context.resources.getIdentifier(category.image, "drawable", context.packageName)
holder.categoryImage?.setImageResource(resourceId)

return categoryView
}

override fun getItem(position: Int): Any {
return categories[position]
}

override fun getItemId(position: Int): Long {
return 0
}

override fun getCount(): Int {
return categories.count()
}

private class ViewHolder {
var categoryImage: ImageView? = null
var categoryName: TextView? = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.devf5.arthursalgado.coderswag.Adapters

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.devf5.arthursalgado.coderswag.Model.Category
import com.devf5.arthursalgado.coderswag.R

/**
* Created by arthursalgado on 13/10/17.
*/

/**
* val itemClick: (Category) -> Unit // Igual ao completion no Swift
* Unit = void
*/

class CategoryRecycleAdapter(val context: Context, val categories: List<Category>, val itemClick: (Category) -> Unit) : RecyclerView.Adapter<CategoryRecycleAdapter.Holder>() {

// Display data on specify location
override fun onBindViewHolder(holder: Holder?, position: Int) {
holder?.bindCategory(categories[position], context)
}

override fun getItemCount(): Int {
return categories.count()
}

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

// inner class pois é uma classe que está dentro de uma class maior
inner class Holder(itemView: View?, val itemClick: (Category) -> Unit) : RecyclerView.ViewHolder(itemView) {
val categoryImage = itemView?.findViewById<ImageView>(R.id.categoryImage)
val categoryName = itemView?.findViewById<TextView>(R.id.categoryName)

fun bindCategory(category: Category, context: Context) {
val resourceId = context.resources.getIdentifier(category.image, "drawable", context.packageName)
categoryImage?.setImageResource(resourceId)
categoryName?.text = category.title
itemView.setOnClickListener { itemClick(category) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.devf5.arthursalgado.coderswag.Adapters

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.devf5.arthursalgado.coderswag.Model.Product
import com.devf5.arthursalgado.coderswag.R

/**
* Created by arthursalgado on 13/10/17.
*/
class ProductsAdapter(val context: Context, val products: List<Product>) : RecyclerView.Adapter<ProductsAdapter.ProductHolder>() {

override fun onBindViewHolder(holder: ProductHolder?, position: Int) {
holder?.bindProduct(products[position], context)
}

override fun getItemCount(): Int {
return products.count()
}

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

}

inner class ProductHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
val productImage = itemView?.findViewById<ImageView>(R.id.productImage)
val productName = itemView?.findViewById<TextView>(R.id.categoryName)
val productPrice = itemView?.findViewById<TextView>(R.id.productPrice)

fun bindProduct(product: Product, context: Context) {
val resourceId = context.resources.getIdentifier(product.image, "drawable", context.packageName)
productImage?.setImageResource(resourceId)
productName?.text = product.title
productPrice?.text = product.price
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.devf5.arthursalgado.coderswag.Controller

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.devf5.arthursalgado.coderswag.Adapters.CategoryRecycleAdapter
import com.devf5.arthursalgado.coderswag.R
import com.devf5.arthursalgado.coderswag.Services.DataService
import com.devf5.arthursalgado.coderswag.Utilities.EXTRA_CATEGORY
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

lateinit var adapter : CategoryRecycleAdapter // Criamos com o recycle
//CategoryAdapter // Criamos um adapter específico para o layout do projeto
//ArrayAdapter<Category> // Adapter é o que controla as informações do model pra ser mostrado na ListView (view)

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

// ArrayAdapter(context, que tipo de view que será mostrado na ListView, qual dado que será mostrado)
adapter = CategoryRecycleAdapter(this, DataService.categories) { category ->
val productIntent = Intent(this, ProductsActivity::class.java)
productIntent.putExtra(EXTRA_CATEGORY, category.title)
startActivity(productIntent)
}
categoryListView.adapter = adapter

val layoutManager = LinearLayoutManager(this)
categoryListView.layoutManager = layoutManager
categoryListView.setHasFixedSize(true)

// Não é a melhor maneira de implementar
//ArrayAdapter(this, android.R.layout.simple_list_item_1, DataService.categories)

// Saber em qual clicou. Mas vamos utilizar outro método com recycle views
// categoryListView.setOnItemClickListener { parent, view, position, id ->
// val category = DataService.categories[position]
// Toast.makeText(this, "You clicked on ${category.title} !", Toast.LENGTH_SHORT).show()
// }


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.devf5.arthursalgado.coderswag.Controller

import android.content.res.Configuration
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import com.devf5.arthursalgado.coderswag.Adapters.ProductsAdapter
import com.devf5.arthursalgado.coderswag.R
import com.devf5.arthursalgado.coderswag.Services.DataService
import com.devf5.arthursalgado.coderswag.Utilities.EXTRA_CATEGORY
import kotlinx.android.synthetic.main.activity_products.*

class ProductsActivity : AppCompatActivity() {

lateinit var adapter : ProductsAdapter

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

val type = intent.getStringExtra(EXTRA_CATEGORY)
adapter = ProductsAdapter(this, DataService.getProducts(type))

var spanCount = 2
val orientation = resources.configuration.orientation
if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
spanCount = 3
}

val screenSize = resources.configuration.screenWidthDp
if(screenSize > 720) {
spanCount = 3
}

val layoutManager = GridLayoutManager(this, spanCount)
productListView.layoutManager = layoutManager as RecyclerView.LayoutManager?
productListView.adapter = adapter
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.devf5.arthursalgado.coderswag.Model

/**
* Created by arthursalgado on 13/10/17.
*/
class Category constructor(val title: String, val image: String) {
// Tivemos que fazer o override pq o default é mostrar o local de memória do objeto, assim trocamos para mostrar por default o título do objeto
override fun toString(): String {
return title
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.devf5.arthursalgado.coderswag.Model

/**
* Created by arthursalgado on 13/10/17.
*/
class Product constructor(val title: String, val price: String, val image: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.devf5.arthursalgado.coderswag.Services

import com.devf5.arthursalgado.coderswag.Model.Category
import com.devf5.arthursalgado.coderswag.Model.Product

/**
* Created by arthursalgado on 13/10/17.
*/
object DataService {

val categories = listOf(
Category("SHIRTS", "shirtimage"),
Category("HOODIES", "hoodieimage"),
Category("HATS", "hatimage"),
Category("DIGITAL", "digitalgoodsimage"),
Category("SHIRTS", "shirtimage"),
Category("HOODIES", "hoodieimage"),
Category("HATS", "hatimage"),
Category("DIGITAL", "digitalgoodsimage"),
Category("SHIRTS", "shirtimage"),
Category("HOODIES", "hoodieimage"),
Category("HATS", "hatimage"),
Category("DIGITAL", "digitalgoodsimage")
)

val hats = listOf(
Product("Devslopes Graphic Beanie", "$18", "hat1"),
Product("Devslopes Hat Black", "$14", "hat2"),
Product("Devslopes Hat White", "$16", "hat3"),
Product("Devslopes Hat Snapback", "$20", "hat4"),
Product("Devslopes Graphic Beanie", "$18", "hat1"),
Product("Devslopes Hat Black", "$14", "hat2"),
Product("Devslopes Hat White", "$16", "hat3"),
Product("Devslopes Hat Snapback", "$20", "hat4"),
Product("Devslopes Graphic Beanie", "$18", "hat1"),
Product("Devslopes Hat Black", "$14", "hat2"),
Product("Devslopes Hat White", "$16", "hat3"),
Product("Devslopes Hat Snapback", "$20", "hat4")
)

val hoodies = listOf(
Product("Devslopes Hoodie Gray", "$28", "hoodie1"),
Product("Devslopes Hoodie Red", "$32", "hoodie2"),
Product("Devslopes Gray Hoodie", "$33", "hoodie3"),
Product("Devslopes Black Hoodie", "$35", "hoodie4"),
Product("Devslopes Hoodie Gray", "$28", "hoodie1"),
Product("Devslopes Hoodie Red", "$32", "hoodie2"),
Product("Devslopes Gray Hoodie", "$33", "hoodie3"),
Product("Devslopes Black Hoodie", "$35", "hoodie4"),
Product("Devslopes Hoodie Gray", "$28", "hoodie1"),
Product("Devslopes Hoodie Red", "$32", "hoodie2"),
Product("Devslopes Gray Hoodie", "$33", "hoodie3"),
Product("Devslopes Black Hoodie", "$35", "hoodie4")
)

val shirts = listOf(
Product("Devslopes Shirt Black", "$18", "shirt1"),
Product("Devslopes Badge Light Gray", "$20", "shirt2"),
Product("Devslopes Logo Shirt Red", "$22", "shirt3"),
Product("Devslopes Hustle", "$22", "shirt4"),
Product("Kickflip Studios Hustle", "$18", "shirt5"),
Product("Devslopes Shirt Black", "$18", "shirt1"),
Product("Devslopes Badge Light Gray", "$20", "shirt2"),
Product("Devslopes Logo Shirt Red", "$22", "shirt3"),
Product("Devslopes Hustle", "$22", "shirt4"),
Product("Kickflip Studios Hustle", "$18", "shirt5"),
Product("Devslopes Shirt Black", "$18", "shirt1"),
Product("Devslopes Badge Light Gray", "$20", "shirt2"),
Product("Devslopes Logo Shirt Red", "$22", "shirt3"),
Product("Devslopes Hustle", "$22", "shirt4"),
Product("Kickflip Studios Hustle", "$18", "shirt5")
)

val digitalGood = listOf<Product>()

fun getProducts(category: String) : List<Product> {

return when(category) {
"SHIRTS" -> shirts
"HATS" -> hats
"HOODIES" -> hoodies
else -> digitalGood
}
}
}
Loading