diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt new file mode 100644 index 00000000..bac352af --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/TabCustomizerActivity.kt @@ -0,0 +1,131 @@ +package com.pitchedapps.frost.activities + +import android.app.Activity +import android.content.res.ColorStateList +import android.os.Bundle +import android.support.design.widget.FloatingActionButton +import android.support.v7.widget.GridLayoutManager +import android.support.v7.widget.RecyclerView +import android.support.v7.widget.helper.ItemTouchHelper +import android.view.View +import android.view.animation.AnimationUtils +import android.widget.TextView +import ca.allanwang.kau.kotlin.lazyContext +import ca.allanwang.kau.utils.bindView +import ca.allanwang.kau.utils.scaleXY +import ca.allanwang.kau.utils.setIcon +import ca.allanwang.kau.utils.withAlpha +import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter +import com.mikepenz.fastadapter_extensions.drag.ItemTouchCallback +import com.mikepenz.fastadapter_extensions.drag.SimpleDragCallback +import com.mikepenz.google_material_typeface_library.GoogleMaterial +import com.pitchedapps.frost.R +import com.pitchedapps.frost.dbflow.TAB_COUNT +import com.pitchedapps.frost.dbflow.loadFbTabs +import com.pitchedapps.frost.dbflow.save +import com.pitchedapps.frost.facebook.FbItem +import com.pitchedapps.frost.iitems.TabIItem +import com.pitchedapps.frost.utils.Prefs +import com.pitchedapps.frost.utils.setFrostColors +import java.util.* + +/** + * Created by Allan Wang on 26/11/17. + */ +class TabCustomizerActivity : BaseActivity() { + + val toolbar: View by bindView(R.id.pseudo_toolbar) + val recycler: RecyclerView by bindView(R.id.tab_recycler) + val instructions: TextView by bindView(R.id.instructions) + val divider: View by bindView(R.id.divider) + val adapter = FastItemAdapter<TabIItem>() + val fabCancel: FloatingActionButton by bindView(R.id.fab_cancel) + val fabSave: FloatingActionButton by bindView(R.id.fab_save) + + private val wobble = lazyContext { AnimationUtils.loadAnimation(it, R.anim.rotate_delta) } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_tab_customizer) + + toolbar.setBackgroundColor(Prefs.headerColor) + + recycler.layoutManager = GridLayoutManager(this, TAB_COUNT, GridLayoutManager.VERTICAL, false) + recycler.adapter = adapter + recycler.setHasFixedSize(true) + + divider.setBackgroundColor(Prefs.textColor.withAlpha(30)) + instructions.setTextColor(Prefs.textColor) + + val tabs = loadFbTabs().toMutableList() + val remaining = FbItem.values().toMutableList() + remaining.removeAll(tabs) + tabs.addAll(remaining) + + adapter.add(tabs.map(::TabIItem)) + bindSwapper(adapter, recycler) + + adapter.withOnClickListener { view, _, _, _ -> view.wobble(); true } + + setResult(Activity.RESULT_CANCELED) + + fabSave.setIcon(GoogleMaterial.Icon.gmd_check, Prefs.iconColor) + fabSave.backgroundTintList = ColorStateList.valueOf(Prefs.accentColor) + fabSave.setOnClickListener { + adapter.adapterItems.subList(0, TAB_COUNT).map(TabIItem::item).save() + setResult(Activity.RESULT_OK) + finish() + } + fabCancel.setIcon(GoogleMaterial.Icon.gmd_close, Prefs.iconColor) + fabCancel.backgroundTintList = ColorStateList.valueOf(Prefs.accentColor) + fabCancel.setOnClickListener { finish() } + setFrostColors(themeWindow = true) + } + + private fun View.wobble() = startAnimation(wobble(context)) + + private fun bindSwapper(adapter: FastItemAdapter<*>, recycler: RecyclerView) { + val dragCallback = TabDragCallback(SimpleDragCallback.ALL, swapper(adapter)) + ItemTouchHelper(dragCallback).attachToRecyclerView(recycler) + } + + private fun swapper(adapter: FastItemAdapter<*>) = object : ItemTouchCallback { + override fun itemTouchOnMove(oldPosition: Int, newPosition: Int): Boolean { + Collections.swap(adapter.adapterItems, oldPosition, newPosition) + adapter.notifyAdapterDataSetChanged() + return true + } + + override fun itemTouchDropped(oldPosition: Int, newPosition: Int) = Unit + } + + + private class TabDragCallback( + directions: Int, itemTouchCallback: ItemTouchCallback + ) : SimpleDragCallback(directions, itemTouchCallback) { + + private var draggingView: TabIItem.ViewHolder? = null + + override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) { + super.onSelectedChanged(viewHolder, actionState) + when (actionState) { + ItemTouchHelper.ACTION_STATE_DRAG -> { + (viewHolder as? TabIItem.ViewHolder)?.apply { + draggingView = this + itemView.animate().scaleXY(1.3f) + text.animate().alpha(0f) + } + } + ItemTouchHelper.ACTION_STATE_IDLE -> { + draggingView?.apply { + itemView.animate().scaleXY(1f) + text.animate().alpha(1f) + } + draggingView = null + } + } + } + + } + +} |