Skip to content

Commit

Permalink
Merge pull request #3 from Semper-Viventem/feature/update_sample
Browse files Browse the repository at this point in the history
Feature/update sample
  • Loading branch information
Semper-Viventem authored Jul 21, 2018
2 parents 0b09011 + b22ea51 commit ad3847e
Show file tree
Hide file tree
Showing 26 changed files with 693 additions and 73 deletions.
4 changes: 3 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.semper_viventem.backdropview">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package ru.semper_viventem.backdropview

import android.content.Context
import android.support.annotation.LayoutRes
import android.support.design.widget.CoordinatorLayout
import android.support.design.widget.CoordinatorLayout.Behavior
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions


fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View {
return LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot)
}

fun <T : CoordinatorLayout.Behavior<*>> View.findBehavior(): T = layoutParams.run {
if (this !is CoordinatorLayout.LayoutParams) throw IllegalArgumentException("View's layout params should be CoordinatorLayout.LayoutParams")

Expand All @@ -21,4 +31,18 @@ fun View.showKeyboard(show: Boolean) {
} else {
imm.hideSoftInputFromWindow(windowToken, 0)
}
}

fun ImageView.load(
url: String?,
round: Boolean = false
) {
Glide.with(context)
.load(url)
.apply {
if (round) {
apply(RequestOptions.circleCropTransform())
}
}
.into(this)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ru.semper_viventem.backdropview.ui

import android.os.Bundle
import android.support.annotation.IdRes
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import ru.semper_viventem.backdrop.BackdropBehavior
import ru.semper_viventem.backdropview.R
import ru.semper_viventem.backdropview.findBehavior
import ru.semper_viventem.backdropview.ui.gallery.GalleryScreen
import ru.semper_viventem.backdropview.ui.list.ListScreen
import ru.semper_viventem.backdropview.ui.text.TextScreen

class MainActivity : AppCompatActivity() {

companion object {
private const val ARG_LAST_MENU_ITEM = "last_menu_item"

private const val MENU_GALLERY = R.id.menuGallery
private const val MENU_TEXT = R.id.menuText
private const val MENU_LIST = R.id.menuList

private const val FRAGMENT_CONTAINER = R.id.foregroundContainer

private const val DEFAULT_ITEM = MENU_GALLERY
}

private lateinit var backdropBehavior: BackdropBehavior

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

backdropBehavior = foregroundContainer.findBehavior()
with(backdropBehavior) {
attacheBackContainer(R.id.backContainer)
attacheToolbar(R.id.toolbar)
}
with(toolbar) {
setTitle(R.string.app_name)
}

navigationView.setNavigationItemSelectedListener { item ->
checkMenuPosition(item.itemId)
backdropBehavior.close()
true
}

val currentItem = savedInstanceState?.getInt(ARG_LAST_MENU_ITEM) ?: DEFAULT_ITEM
navigationView.setCheckedItem(currentItem)
checkMenuPosition(navigationView.checkedItem!!.itemId)
}

override fun onBackPressed() {
if (!backdropBehavior.close()) {
finish()
}
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putInt(ARG_LAST_MENU_ITEM, navigationView.checkedItem!!.itemId)

super.onSaveInstanceState(outState)
}

private fun checkMenuPosition(@IdRes menuItemId: Int) {
when (menuItemId) {
MENU_GALLERY -> showPage(GalleryScreen())
MENU_TEXT -> showPage(TextScreen())
MENU_LIST -> showPage(ListScreen())
}
}

private fun showPage(page: Fragment) {
supportFragmentManager
.beginTransaction()
.replace(FRAGMENT_CONTAINER, page)
.commit()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ru.semper_viventem.backdropview.ui.common

import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView

/**
* Easy to use base adapter for lists with single item type.
* @author Dmitriy Gorbunov (@dmdevgo)
*/
abstract class BaseListAdapter<T, VH> : RecyclerView.Adapter<VH>() where VH : RecyclerView.ViewHolder, VH : BaseListAdapter.Bindable<T> {

private var items: MutableList<T> = mutableListOf()

override fun onBindViewHolder(holder: VH, position: Int) {
holder.bind(getItem(position))
}

override fun getItemCount() = items.size

fun getItem(position: Int) = items[position]

fun getItems(): List<T> {
return items
}

fun addItems(list: List<T>) {
val positionStart = items.size
items.addAll(list)
notifyItemRangeInserted(positionStart, list.size)
}

fun setItems(list: List<T>) {
items.clear()
items.addAll(list)
notifyDataSetChanged()
}

fun updateItems(list: List<T>, diffCallback: DiffItemsCallback<T>) {
if (items.isEmpty()) {
setItems(list)
} else {
val diffResult = DiffUtil.calculateDiff(DiffCallback(items, list, diffCallback))
items.clear()
items.addAll(list)
diffResult.dispatchUpdatesTo(this)
}
}

interface Bindable<in T> {
fun bind(item: T)
}

interface DiffItemsCallback<in T> {
fun areItemsTheSame(oldItem: T, newItem: T): Boolean
fun areContentsTheSame(oldItem: T, newItem: T): Boolean
}

private class DiffCallback<in T>(
private val oldList: List<T>,
private val newList: List<T>,
private val diffItemsCallback: DiffItemsCallback<T>
) : DiffUtil.Callback() {

override fun getOldListSize() = oldList.size

override fun getNewListSize() = newList.size

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return diffItemsCallback.areItemsTheSame(oldList[oldItemPosition], newList[newItemPosition])
}

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return diffItemsCallback.areContentsTheSame(oldList[oldItemPosition], newList[newItemPosition])
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.semper_viventem.backdropview.ui.common

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup


abstract class Screen : Fragment {

constructor(): super()

constructor(args: Bundle) : super() {
arguments = args
}

abstract val layoutId: Int

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(layoutId, container, false)
}

override fun onStart() {
super.onStart()

onInitView(view!!)
}

abstract fun onInitView(view: View)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.semper_viventem.backdropview.ui.gallery

import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.item_gallery.view.*
import ru.semper_viventem.backdropview.R
import ru.semper_viventem.backdropview.inflate
import ru.semper_viventem.backdropview.load
import ru.semper_viventem.backdropview.ui.common.BaseListAdapter
import ru.semper_viventem.domain.ItemData


class GalleryAdapter : BaseListAdapter<ItemData, GalleryAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, itemType: Int): ViewHolder =
ViewHolder(parent.inflate(R.layout.item_gallery))

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view), BaseListAdapter.Bindable<ItemData> {
override fun bind(item: ItemData) {
itemView.image.load(item.image)
itemView.title.text = item.title
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ru.semper_viventem.backdropview.ui.gallery

import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearSnapHelper
import android.view.View
import kotlinx.android.synthetic.main.screen_gallery.view.*
import ru.semper_viventem.backdropview.R
import ru.semper_viventem.backdropview.ui.common.Screen
import ru.semper_viventem.domain.ItemsFactory


class GalleryScreen : Screen() {

companion object {
private const val SNAP_COUNT = 2
}

override val layoutId: Int = R.layout.screen_gallery

private val galleryAdapter = GalleryAdapter()
private val snapHelper = LinearSnapHelper()

override fun onInitView(view: View) {

with(view.recyclerView) {
setHasFixedSize(true)
layoutManager = GridLayoutManager(context, SNAP_COUNT, GridLayoutManager.HORIZONTAL, false)
adapter = galleryAdapter
addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL).apply {
this.setDrawable(context.getDrawable(R.drawable.divider_empty))
})
addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL).apply {
this.setDrawable(context.getDrawable(R.drawable.divider_empty))
})
}
with(snapHelper) {
attachToRecyclerView(view.recyclerView)
}

galleryAdapter.setItems(ItemsFactory.getDefault())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.semper_viventem.backdropview.ui.list

import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.item_list.view.*
import ru.semper_viventem.backdropview.R
import ru.semper_viventem.backdropview.inflate
import ru.semper_viventem.backdropview.load
import ru.semper_viventem.backdropview.ui.common.BaseListAdapter
import ru.semper_viventem.domain.ItemData


class ListItemsAdapter : BaseListAdapter<ItemData, ListItemsAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, itemType: Int): ViewHolder =
ViewHolder(parent.inflate(R.layout.item_list))

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view), BaseListAdapter.Bindable<ItemData> {
override fun bind(item: ItemData) {
itemView.image.load(item.image)
itemView.title.text = item.title
itemView.description.text = item.description
}
}
}
Loading

0 comments on commit ad3847e

Please sign in to comment.