Skip to content

Commit

Permalink
Merge pull request #6 from Semper-Viventem/release
Browse files Browse the repository at this point in the history
Release 0.1.2
  • Loading branch information
Semper-Viventem authored Jul 21, 2018
2 parents f971f70 + c6ae1fe commit ee88123
Show file tree
Hide file tree
Showing 29 changed files with 737 additions and 80 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This library makes it easy to implement a [Backdrop](https://material.io/design/
**JCenter (Recommended):**
```groovy
dependencies {
implementation 'ru.semper-viventem.backdrop:backdrop:0.1.1'
implementation 'ru.semper-viventem.backdrop:backdrop:0.1.2'
}
```
*or*
Expand All @@ -28,7 +28,7 @@ repositories {
}
dependencies {
implementation 'com.github.Semper-Viventem:BackdropView:0.1.1'
implementation 'com.github.Semper-Viventem:BackdropView:0.1.2'
}
```

Expand Down
2 changes: 1 addition & 1 deletion backdrop/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ext {
siteUrl = 'https://github.com/Semper-Viventem/BackdropView'
gitUrl = 'https://github.com/Semper-Viventem/BackdropView'

libraryVersion = '0.1.1'
libraryVersion = '0.1.2'

developerId = 'semper-viventem'
developerName = 'constantine'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.semper_viventem.backdrop

import android.content.Context
import android.os.Bundle
import android.os.Parcelable
import android.support.annotation.IdRes
import android.support.design.widget.CoordinatorLayout
import android.support.v7.widget.Toolbar
Expand All @@ -23,6 +25,9 @@ class BackdropBehavior : CoordinatorLayout.Behavior<View> {
companion object {
private const val DEFAULT_DURATION = 300L
private const val WITHOUT_DURATION = 0L
private val DEFAULT_DROP_STATE = DropState.CLOSE

private const val ARG_DROP_STATE = "arg_drop_state"
}

private var toolbarId: Int? = null
Expand All @@ -35,14 +40,26 @@ class BackdropBehavior : CoordinatorLayout.Behavior<View> {
private var closedIconId: Int = R.drawable.ic_menu
private var openedIconRes: Int = R.drawable.ic_close

private var dropState: DropState = DropState.CLOSE
private var dropState: DropState = DEFAULT_DROP_STATE

private var dropListeners = mutableListOf<OnDropListener>()

constructor() : super()

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

override fun onSaveInstanceState(parent: CoordinatorLayout?, child: View?): Parcelable {
return Bundle().apply {
putSerializable(ARG_DROP_STATE, dropState)
}
}

override fun onRestoreInstanceState(parent: CoordinatorLayout?, child: View?, state: Parcelable?) {
super.onRestoreInstanceState(parent, child, state)

dropState = (state as? Bundle)?.getSerializable(ARG_DROP_STATE) as? DropState ?: DEFAULT_DROP_STATE
}

override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
if (toolbarId == null || backContainerId == null) return false

Expand All @@ -62,7 +79,7 @@ class BackdropBehavior : CoordinatorLayout.Behavior<View> {
}

if (toolbar != null && backContainer != null) {
initViews(child, toolbar!!, backContainer!!)
initViews(parent, child, toolbar!!, backContainer!!)
}

return super.onDependentViewChanged(parent, child, dependency)
Expand Down Expand Up @@ -118,8 +135,9 @@ class BackdropBehavior : CoordinatorLayout.Behavior<View> {
true
}

private fun initViews(child: View, toolbar: Toolbar, backContainer: View) {
private fun initViews(parent: CoordinatorLayout, child: View, toolbar: Toolbar, backContainer: View) {
backContainer.y = toolbar.y + toolbar.height
child.layoutParams.height = parent.height - toolbar.height
drawDropState(child, toolbar, backContainer, false)

with(toolbar) {
Expand Down
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)
}
Loading

0 comments on commit ee88123

Please sign in to comment.