-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Semper-Viventem/feature/update_sample
Feature/update sample
- Loading branch information
Showing
26 changed files
with
693 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
sample/src/main/kotlin/ru/semper_viventem/backdropview/MainActivity.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
sample/src/main/kotlin/ru/semper_viventem/backdropview/ui/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
sample/src/main/kotlin/ru/semper_viventem/backdropview/ui/common/BaseListAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
sample/src/main/kotlin/ru/semper_viventem/backdropview/ui/common/Screen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
25 changes: 25 additions & 0 deletions
25
sample/src/main/kotlin/ru/semper_viventem/backdropview/ui/gallery/GalleryAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
sample/src/main/kotlin/ru/semper_viventem/backdropview/ui/gallery/GalleryScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
sample/src/main/kotlin/ru/semper_viventem/backdropview/ui/list/ListItemsAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.