From dfa5952f0d47d124e5cc8c789287f4a50771425a Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 29 Sep 2021 14:00:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/document/adapter/FileAdapter.kt | 4 +- .../io/legado/app/utils/ConstraintModify.kt | 283 ++++++++++++++++++ .../io/legado/app/utils/ConstraintUtil.kt | 282 ----------------- .../java/io/legado/app/utils/FileUtils.kt | 29 +- .../java/io/legado/app/utils/HtmlFormatter.kt | 1 + .../io/legado/app/utils/ToolBarExtensions.kt | 2 + .../main/java/io/legado/app/utils/ZipUtils.kt | 2 +- 7 files changed, 301 insertions(+), 302 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/ConstraintModify.kt delete mode 100644 app/src/main/java/io/legado/app/utils/ConstraintUtil.kt diff --git a/app/src/main/java/io/legado/app/ui/document/adapter/FileAdapter.kt b/app/src/main/java/io/legado/app/ui/document/adapter/FileAdapter.kt index 324e627b8..06917504a 100644 --- a/app/src/main/java/io/legado/app/ui/document/adapter/FileAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/document/adapter/FileAdapter.kt @@ -60,10 +60,10 @@ class FileAdapter(context: Context, val callBack: CallBack) : data.add(fileParent) } currentPath?.let { currentPath -> - val files: Array? = FileUtils.listDirsAndFiles(currentPath) + val files: Array? = FileUtils.listDirsAndFiles(currentPath) if (files != null) { for (file in files) { - if (file == null || (!callBack.isShowHideDir && file.name.startsWith("."))) { + if (!callBack.isShowHideDir && file.name.startsWith(".")) { continue } val fileItem = FileItem() diff --git a/app/src/main/java/io/legado/app/utils/ConstraintModify.kt b/app/src/main/java/io/legado/app/utils/ConstraintModify.kt new file mode 100644 index 000000000..ed4a55caf --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/ConstraintModify.kt @@ -0,0 +1,283 @@ +package io.legado.app.utils + +import androidx.annotation.IdRes +import androidx.constraintlayout.widget.ConstraintLayout +import androidx.constraintlayout.widget.ConstraintSet +import androidx.transition.TransitionManager + + +@Suppress("MemberVisibilityCanBePrivate", "unused") +class ConstraintModify(private val constraintLayout: ConstraintLayout) { + + private var begin: ConstraintBegin? = null + private val applyConstraintSet = ConstraintSet() + private val resetConstraintSet = ConstraintSet() + + init { + resetConstraintSet.clone(constraintLayout) + } + + /** + * 开始修改 + */ + fun begin(): ConstraintBegin { + synchronized(ConstraintBegin::class.java) { + if (begin == null) { + begin = ConstraintBegin(constraintLayout, applyConstraintSet) + } + } + applyConstraintSet.clone(constraintLayout) + return begin!! + } + + /** + * 带动画的修改 + * @return + */ + fun beginWithAnim(): ConstraintBegin { + TransitionManager.beginDelayedTransition(constraintLayout) + return begin() + } + + /** + * 重置 + */ + fun reSet() { + resetConstraintSet.applyTo(constraintLayout) + } + + /** + * 带动画的重置 + */ + fun reSetWidthAnim() { + TransitionManager.beginDelayedTransition(constraintLayout) + resetConstraintSet.applyTo(constraintLayout) + } + + + @Suppress("unused", "MemberVisibilityCanBePrivate") + class ConstraintBegin( + private val constraintLayout: ConstraintLayout, + private val applyConstraintSet: ConstraintSet + ) { + + /** + * 清除关系

+ * 注意:这里不仅仅会清除关系,还会清除对应控件的宽高为 w:0,h:0 + * @param viewIds + * @return + */ + fun clear(@IdRes vararg viewIds: Int): ConstraintBegin { + for (viewId in viewIds) { + applyConstraintSet.clear(viewId) + } + return this + } + + /** + * 清除某个控件的,某个关系 + * @param viewId + * @param anchor + * @return + */ + fun clear(viewId: Int, anchor: Int): ConstraintBegin { + applyConstraintSet.clear(viewId, anchor) + return this + } + + fun setHorizontalWeight(viewId: Int, weight: Float): ConstraintBegin { + applyConstraintSet.setHorizontalWeight(viewId, weight) + return this + } + + fun setVerticalWeight(viewId: Int, weight: Float): ConstraintBegin { + applyConstraintSet.setVerticalWeight(viewId, weight) + return this + } + + /** + * 为某个控件设置 margin + * @param viewId 某个控件ID + * @param left marginLeft + * @param top marginTop + * @param right marginRight + * @param bottom marginBottom + * @return + */ + fun setMargin( + @IdRes viewId: Int, + left: Int, + top: Int, + right: Int, + bottom: Int + ): ConstraintBegin { + setMarginLeft(viewId, left) + setMarginTop(viewId, top) + setMarginRight(viewId, right) + setMarginBottom(viewId, bottom) + return this + } + + /** + * 为某个控件设置 marginLeft + * @param viewId 某个控件ID + * @param left marginLeft + * @return + */ + fun setMarginLeft(@IdRes viewId: Int, left: Int): ConstraintBegin { + applyConstraintSet.setMargin(viewId, ConstraintSet.LEFT, left) + return this + } + + /** + * 为某个控件设置 marginRight + * @param viewId 某个控件ID + * @param right marginRight + * @return + */ + fun setMarginRight(@IdRes viewId: Int, right: Int): ConstraintBegin { + applyConstraintSet.setMargin(viewId, ConstraintSet.RIGHT, right) + return this + } + + /** + * 为某个控件设置 marginTop + * @param viewId 某个控件ID + * @param top marginTop + * @return + */ + fun setMarginTop(@IdRes viewId: Int, top: Int): ConstraintBegin { + applyConstraintSet.setMargin(viewId, ConstraintSet.TOP, top) + return this + } + + /** + * 为某个控件设置marginBottom + * @param viewId 某个控件ID + * @param bottom marginBottom + * @return + */ + fun setMarginBottom(@IdRes viewId: Int, bottom: Int): ConstraintBegin { + applyConstraintSet.setMargin(viewId, ConstraintSet.BOTTOM, bottom) + return this + } + + /** + * 为某个控件设置关联关系 left_to_left_of + * @param startId + * @param endId + * @return + */ + fun leftToLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { + applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.LEFT) + return this + } + + /** + * 为某个控件设置关联关系 left_to_right_of + * @param startId + * @param endId + * @return + */ + fun leftToRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { + applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.RIGHT) + return this + } + + /** + * 为某个控件设置关联关系 top_to_top_of + * @param startId + * @param endId + * @return + */ + fun topToTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { + applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.TOP) + return this + } + + /** + * 为某个控件设置关联关系 top_to_bottom_of + * @param startId + * @param endId + * @return + */ + fun topToBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { + applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.BOTTOM) + return this + } + + /** + * 为某个控件设置关联关系 right_to_left_of + * @param startId + * @param endId + * @return + */ + fun rightToLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { + applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.LEFT) + return this + } + + /** + * 为某个控件设置关联关系 right_to_right_of + * @param startId + * @param endId + * @return + */ + fun rightToRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { + applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.RIGHT) + return this + } + + /** + * 为某个控件设置关联关系 bottom_to_bottom_of + * @param startId + * @param endId + * @return + */ + fun bottomToBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { + applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.BOTTOM) + return this + } + + /** + * 为某个控件设置关联关系 bottom_to_top_of + * @param startId + * @param endId + * @return + */ + fun bottomToTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { + applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.TOP) + return this + } + + /** + * 为某个控件设置宽度 + * @param viewId + * @param width + * @return + */ + fun setWidth(@IdRes viewId: Int, width: Int): ConstraintBegin { + applyConstraintSet.constrainWidth(viewId, width) + return this + } + + /** + * 某个控件设置高度 + * @param viewId + * @param height + * @return + */ + fun setHeight(@IdRes viewId: Int, height: Int): ConstraintBegin { + applyConstraintSet.constrainHeight(viewId, height) + return this + } + + /** + * 提交应用生效 + */ + fun commit() { + applyConstraintSet.applyTo(constraintLayout) + } + } + +} diff --git a/app/src/main/java/io/legado/app/utils/ConstraintUtil.kt b/app/src/main/java/io/legado/app/utils/ConstraintUtil.kt deleted file mode 100644 index bb2cc7bad..000000000 --- a/app/src/main/java/io/legado/app/utils/ConstraintUtil.kt +++ /dev/null @@ -1,282 +0,0 @@ -package io.legado.app.utils - -import androidx.annotation.IdRes -import androidx.constraintlayout.widget.ConstraintLayout -import androidx.constraintlayout.widget.ConstraintSet -import androidx.transition.TransitionManager - - -@Suppress("MemberVisibilityCanBePrivate", "unused") -class ConstraintUtil(private val constraintLayout: ConstraintLayout) { - - private var begin: ConstraintBegin? = null - private val applyConstraintSet = ConstraintSet() - private val resetConstraintSet = ConstraintSet() - - init { - resetConstraintSet.clone(constraintLayout) - } - - /** - * 开始修改 - */ - fun begin(): ConstraintBegin { - synchronized(ConstraintBegin::class.java) { - if (begin == null) { - begin = ConstraintBegin(constraintLayout, applyConstraintSet) - } - } - applyConstraintSet.clone(constraintLayout) - return begin!! - } - - /** - * 带动画的修改 - * @return - */ - fun beginWithAnim(): ConstraintBegin? { - TransitionManager.beginDelayedTransition(constraintLayout) - return begin() - } - - /** - * 重置 - */ - fun reSet() { - resetConstraintSet.applyTo(constraintLayout) - } - - /** - * 带动画的重置 - */ - fun reSetWidthAnim() { - TransitionManager.beginDelayedTransition(constraintLayout) - resetConstraintSet.applyTo(constraintLayout) - } - -} - -@Suppress("unused", "MemberVisibilityCanBePrivate") -class ConstraintBegin( - private val constraintLayout: ConstraintLayout, - private val applyConstraintSet: ConstraintSet -) { - - /** - * 清除关系

- * 注意:这里不仅仅会清除关系,还会清除对应控件的宽高为 w:0,h:0 - * @param viewIds - * @return - */ - fun clear(@IdRes vararg viewIds: Int): ConstraintBegin { - for (viewId in viewIds) { - applyConstraintSet.clear(viewId) - } - return this - } - - /** - * 清除某个控件的,某个关系 - * @param viewId - * @param anchor - * @return - */ - fun clear(viewId: Int, anchor: Int): ConstraintBegin { - applyConstraintSet.clear(viewId, anchor) - return this - } - - fun setHorizontalWeight(viewId: Int, weight: Float): ConstraintBegin { - applyConstraintSet.setHorizontalWeight(viewId, weight) - return this - } - - fun setVerticalWeight(viewId: Int, weight: Float): ConstraintBegin { - applyConstraintSet.setVerticalWeight(viewId, weight) - return this - } - - /** - * 为某个控件设置 margin - * @param viewId 某个控件ID - * @param left marginLeft - * @param top marginTop - * @param right marginRight - * @param bottom marginBottom - * @return - */ - fun setMargin( - @IdRes viewId: Int, - left: Int, - top: Int, - right: Int, - bottom: Int - ): ConstraintBegin { - setMarginLeft(viewId, left) - setMarginTop(viewId, top) - setMarginRight(viewId, right) - setMarginBottom(viewId, bottom) - return this - } - - /** - * 为某个控件设置 marginLeft - * @param viewId 某个控件ID - * @param left marginLeft - * @return - */ - fun setMarginLeft(@IdRes viewId: Int, left: Int): ConstraintBegin { - applyConstraintSet.setMargin(viewId, ConstraintSet.LEFT, left) - return this - } - - /** - * 为某个控件设置 marginRight - * @param viewId 某个控件ID - * @param right marginRight - * @return - */ - fun setMarginRight(@IdRes viewId: Int, right: Int): ConstraintBegin { - applyConstraintSet.setMargin(viewId, ConstraintSet.RIGHT, right) - return this - } - - /** - * 为某个控件设置 marginTop - * @param viewId 某个控件ID - * @param top marginTop - * @return - */ - fun setMarginTop(@IdRes viewId: Int, top: Int): ConstraintBegin { - applyConstraintSet.setMargin(viewId, ConstraintSet.TOP, top) - return this - } - - /** - * 为某个控件设置marginBottom - * @param viewId 某个控件ID - * @param bottom marginBottom - * @return - */ - fun setMarginBottom(@IdRes viewId: Int, bottom: Int): ConstraintBegin { - applyConstraintSet.setMargin(viewId, ConstraintSet.BOTTOM, bottom) - return this - } - - /** - * 为某个控件设置关联关系 left_to_left_of - * @param startId - * @param endId - * @return - */ - fun leftToLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { - applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.LEFT) - return this - } - - /** - * 为某个控件设置关联关系 left_to_right_of - * @param startId - * @param endId - * @return - */ - fun leftToRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { - applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.RIGHT) - return this - } - - /** - * 为某个控件设置关联关系 top_to_top_of - * @param startId - * @param endId - * @return - */ - fun topToTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { - applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.TOP) - return this - } - - /** - * 为某个控件设置关联关系 top_to_bottom_of - * @param startId - * @param endId - * @return - */ - fun topToBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { - applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.BOTTOM) - return this - } - - /** - * 为某个控件设置关联关系 right_to_left_of - * @param startId - * @param endId - * @return - */ - fun rightToLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { - applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.LEFT) - return this - } - - /** - * 为某个控件设置关联关系 right_to_right_of - * @param startId - * @param endId - * @return - */ - fun rightToRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { - applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.RIGHT) - return this - } - - /** - * 为某个控件设置关联关系 bottom_to_bottom_of - * @param startId - * @param endId - * @return - */ - fun bottomToBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { - applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.BOTTOM) - return this - } - - /** - * 为某个控件设置关联关系 bottom_to_top_of - * @param startId - * @param endId - * @return - */ - fun bottomToTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { - applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.TOP) - return this - } - - /** - * 为某个控件设置宽度 - * @param viewId - * @param width - * @return - */ - fun setWidth(@IdRes viewId: Int, width: Int): ConstraintBegin { - applyConstraintSet.constrainWidth(viewId, width) - return this - } - - /** - * 某个控件设置高度 - * @param viewId - * @param height - * @return - */ - fun setHeight(@IdRes viewId: Int, height: Int): ConstraintBegin { - applyConstraintSet.constrainHeight(viewId, height) - return this - } - - /** - * 提交应用生效 - */ - fun commit() { - applyConstraintSet.applyTo(constraintLayout) - } -} diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index 071050a8a..2e109a188 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -162,20 +162,19 @@ object FileUtils { fun listDirs( startDirPath: String, excludeDirs: Array? = null, @SortType sortType: Int = BY_NAME_ASC - ): Array { + ): Array { var excludeDirs1 = excludeDirs val dirList = ArrayList() val startDir = File(startDirPath) if (!startDir.isDirectory) { - return arrayOfNulls(0) + return arrayOf() } val dirs = startDir.listFiles(FileFilter { f -> if (f == null) { return@FileFilter false } f.isDirectory - }) - ?: return arrayOfNulls(0) + }) ?: return arrayOf() if (excludeDirs1 == null) { excludeDirs1 = arrayOf() } @@ -217,22 +216,18 @@ object FileUtils { fun listDirsAndFiles( startDirPath: String, allowExtensions: Array? = null - ): Array? { - val dirs: Array? - val files: Array? = if (allowExtensions == null) { + ): Array? { + val dirs: Array? + val files: Array? = if (allowExtensions == null) { listFiles(startDirPath) } else { listFiles(startDirPath, allowExtensions) } - val dirsAndFiles: Array dirs = listDirs(startDirPath) if (files == null) { return null } - dirsAndFiles = arrayOfNulls(dirs.size + files.size) - System.arraycopy(dirs, 0, dirsAndFiles, 0, dirs.size) - System.arraycopy(files, 0, dirsAndFiles, dirs.size, files.size) - return dirsAndFiles + return dirs + files } /** @@ -242,11 +237,11 @@ object FileUtils { fun listFiles( startDirPath: String, filterPattern: Pattern? = null, @SortType sortType: Int = BY_NAME_ASC - ): Array { + ): Array { val fileList = ArrayList() val f = File(startDirPath) if (!f.isDirectory) { - return arrayOfNulls(0) + return arrayOf() } val files = f.listFiles(FileFilter { file -> if (file == null) { @@ -258,7 +253,7 @@ object FileUtils { filterPattern?.matcher(file.name)?.find() ?: true }) - ?: return arrayOfNulls(0) + ?: return arrayOf() for (file in files) { fileList.add(file.absoluteFile) } @@ -290,7 +285,7 @@ object FileUtils { /** * 列出指定目录下的所有文件 */ - fun listFiles(startDirPath: String, allowExtensions: Array?): Array? { + fun listFiles(startDirPath: String, allowExtensions: Array?): Array? { val file = File(startDirPath) return file.listFiles { _, name -> //返回当前目录所有以某些扩展名结尾的文件 @@ -303,7 +298,7 @@ object FileUtils { /** * 列出指定目录下的所有文件 */ - fun listFiles(startDirPath: String, allowExtension: String?): Array? { + fun listFiles(startDirPath: String, allowExtension: String?): Array? { return if (allowExtension == null) listFiles(startDirPath, allowExtension = null) else diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index ab779e288..ed2edd891 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -4,6 +4,7 @@ import io.legado.app.model.analyzeRule.AnalyzeUrl import java.net.URL import java.util.regex.Pattern +@Suppress("RegExpRedundantEscape") object HtmlFormatter { private val wrapHtmlRegex = "]*>".toRegex() private val commentRegex = "".toRegex() //注释 diff --git a/app/src/main/java/io/legado/app/utils/ToolBarExtensions.kt b/app/src/main/java/io/legado/app/utils/ToolBarExtensions.kt index 325923f15..3b7629671 100644 --- a/app/src/main/java/io/legado/app/utils/ToolBarExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ToolBarExtensions.kt @@ -1,3 +1,5 @@ +@file:Suppress("unused") + package io.legado.app.utils import android.graphics.PorterDuff diff --git a/app/src/main/java/io/legado/app/utils/ZipUtils.kt b/app/src/main/java/io/legado/app/utils/ZipUtils.kt index f7d6ec194..766a620bb 100644 --- a/app/src/main/java/io/legado/app/utils/ZipUtils.kt +++ b/app/src/main/java/io/legado/app/utils/ZipUtils.kt @@ -9,7 +9,7 @@ import java.util.zip.ZipEntry import java.util.zip.ZipFile import java.util.zip.ZipOutputStream -@Suppress("unused", "BlockingMethodInNonBlockingContext") +@Suppress("unused", "BlockingMethodInNonBlockingContext", "MemberVisibilityCanBePrivate") object ZipUtils { /**