move breadcrumbs to views folder

This commit is contained in:
tibbi 2016-10-11 22:18:07 +02:00
parent 6d230260d8
commit 899255fc68
7 changed files with 9 additions and 176 deletions

View File

@ -15,7 +15,7 @@ import com.simplemobiletools.filemanager.R;
import com.simplemobiletools.filemanager.Utils;
import com.simplemobiletools.filemanager.fragments.ItemsFragment;
import com.simplemobiletools.filepicker.models.FileDirItem;
import com.simplemobiletools.filepicker.Breadcrumbs;
import com.simplemobiletools.filepicker.views.Breadcrumbs;
import butterknife.BindView;
import butterknife.ButterKnife;

View File

@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.simplemobiletools.filepicker.Breadcrumbs
<com.simplemobiletools.filepicker.views.Breadcrumbs
android:id="@+id/breadcrumbs"
android:layout_width="match_parent"
android:layout_height="wrap_content"

View File

@ -6,7 +6,7 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<com.simplemobiletools.filepicker.Breadcrumbs
<com.simplemobiletools.filepicker.views.Breadcrumbs
android:id="@+id/directory_picker_breadcrumbs"
android:layout_width="match_parent"
android:layout_height="wrap_content"

View File

@ -1,166 +0,0 @@
package com.simplemobiletools.filepicker
import android.content.Context
import android.graphics.Point
import android.os.Environment
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView
import com.simplemobiletools.filepicker.models.FileDirItem
class Breadcrumbs(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs), View.OnClickListener {
private var mDeviceWidth: Int = 0
private var mInflater: LayoutInflater? = null
private var mListener: BreadcrumbsListener? = null
init {
init(context)
}
private fun init(context: Context) {
mInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val display = (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
val deviceDisplay = Point()
display.getSize(deviceDisplay)
mDeviceWidth = deviceDisplay.x
}
fun setListener(listener: BreadcrumbsListener) {
mListener = listener
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
val paddingTop = paddingTop
val paddingLeft = paddingLeft
val paddingRight = paddingRight
val childRight = measuredWidth - paddingRight
val childBottom = measuredHeight - paddingBottom
val childHeight = childBottom - paddingTop
val usableWidth = mDeviceWidth - paddingLeft - paddingRight
var maxHeight = 0
var curWidth: Int
var curHeight: Int
var curLeft = paddingLeft
var curTop = paddingTop
val cnt = childCount
for (i in 0..cnt - 1) {
val child = getChildAt(i)
child.measure(MeasureSpec.makeMeasureSpec(usableWidth, MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST))
curWidth = child.measuredWidth
curHeight = child.measuredHeight
if (curLeft + curWidth >= childRight) {
curLeft = paddingLeft
curTop += maxHeight
maxHeight = 0
}
child.layout(curLeft, curTop, curLeft + curWidth, curTop + curHeight)
if (maxHeight < curHeight)
maxHeight = curHeight
curLeft += curWidth
}
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val usableWidth = mDeviceWidth - paddingLeft - paddingRight
var width = 0
var rowHeight = 0
var lines = 1
val cnt = childCount
for (i in 0..cnt - 1) {
val child = getChildAt(i)
measureChild(child, widthMeasureSpec, heightMeasureSpec)
width += child.measuredWidth
rowHeight = child.measuredHeight
if (width / usableWidth > 0) {
lines++
width = child.measuredWidth
}
}
val parentWidth = MeasureSpec.getSize(widthMeasureSpec)
val calculatedHeight = paddingTop + paddingBottom + rowHeight * lines
setMeasuredDimension(parentWidth, calculatedHeight)
}
fun setInitialBreadcrumb(fullPath: String) {
val showFullPath = false//com.simplemobiletools.filemanager.Config.newInstance(context).showFullPath
val basePath = Environment.getExternalStorageDirectory().toString()
var tempPath = fullPath
var currPath = basePath
if (!showFullPath) {
tempPath = fullPath.replace(basePath, context.getString(R.string.smtfp_initial_breadcrumb) + "/")
} else {
currPath = "/"
}
removeAllViewsInLayout()
val dirs = tempPath.split("/".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
for (i in dirs.indices) {
val dir = dirs[i]
if (i > 0) {
currPath += dir + "/"
} else if (showFullPath) {
addRootFolder()
}
if (dir.isEmpty())
continue
val item = FileDirItem(currPath, dir, true, 0, 0)
addBreadcrumb(item, i > 0 || showFullPath)
}
if (dirs.size == 0 && showFullPath) {
addRootFolder()
}
}
fun addBreadcrumb(item: FileDirItem, addPrefix: Boolean) {
val view = mInflater!!.inflate(R.layout.smtfp_breadcrumb_item, null, false)
val textView = view.findViewById(R.id.breadcrumb_text) as TextView
var textToAdd = item.name
if (addPrefix)
textToAdd = " -> " + textToAdd
textView.text = textToAdd
addView(view)
view.setOnClickListener(this)
view.tag = item
}
fun removeBreadcrumb() {
removeView(getChildAt(childCount - 1))
}
private fun addRootFolder() {
val item = FileDirItem("/", " / ", true, 0, 0)
addBreadcrumb(item, false)
}
override fun onClick(v: View) {
val cnt = childCount
for (i in 0..cnt - 1) {
if (getChildAt(i) != null && getChildAt(i) == v) {
mListener?.breadcrumbClicked(i)
}
}
}
interface BreadcrumbsListener {
fun breadcrumbClicked(id: Int)
}
}

View File

@ -7,11 +7,11 @@ import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.app.AlertDialog
import android.view.View
import com.simplemobiletools.filepicker.Breadcrumbs
import com.simplemobiletools.filepicker.R
import com.simplemobiletools.filepicker.adapters.ItemsAdapter
import com.simplemobiletools.filepicker.extensions.getFilenameFromPath
import com.simplemobiletools.filepicker.models.FileDirItem
import com.simplemobiletools.filepicker.views.Breadcrumbs
import kotlinx.android.synthetic.main.smtfp_directory_picker.view.*
import java.io.File
import java.util.*

View File

@ -53,8 +53,8 @@ class Breadcrumbs(context: Context, attrs: AttributeSet) : LinearLayout(context,
for (i in 0..cnt - 1) {
val child = getChildAt(i)
child.measure(View.MeasureSpec.makeMeasureSpec(usableWidth, View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(childHeight, View.MeasureSpec.AT_MOST))
child.measure(MeasureSpec.makeMeasureSpec(usableWidth, MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST))
curWidth = child.measuredWidth
curHeight = child.measuredHeight
@ -91,7 +91,7 @@ class Breadcrumbs(context: Context, attrs: AttributeSet) : LinearLayout(context,
}
}
val parentWidth = View.MeasureSpec.getSize(widthMeasureSpec)
val parentWidth = MeasureSpec.getSize(widthMeasureSpec)
val calculatedHeight = paddingTop + paddingBottom + rowHeight * lines
setMeasuredDimension(parentWidth, calculatedHeight)
}
@ -131,10 +131,10 @@ class Breadcrumbs(context: Context, attrs: AttributeSet) : LinearLayout(context,
fun addBreadcrumb(item: FileDirItem, addPrefix: Boolean) {
val view = mInflater!!.inflate(R.layout.smtfp_breadcrumb_item, null, false)
var textToAdd = item.name
if (addPrefix)
textToAdd = " -> " + textToAdd
view.breadcrumb_text.text = textToAdd
addView(view)
view.setOnClickListener(this)

View File

@ -6,14 +6,13 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<com.simplemobiletools.filepicker.Breadcrumbs
<com.simplemobiletools.filepicker.views.Breadcrumbs
android:id="@+id/directory_picker_breadcrumbs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/smtfp_activity_margin"/>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/directory_picker_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>