Kaynağa Gözat

Merge branch '144-nullCheck' into 'master'

Resolve "避免使用「!!」"

See merge request kdanandroid/pdf/pdfreaderreadermodule!75
Wayne Huang 4 yıl önce
ebeveyn
işleme
42b4d88ef9

+ 2 - 1
src/main/java/com/kdanmobile/reader/ReaderActivity.kt

@@ -533,10 +533,11 @@ abstract class ReaderActivity :
          * 注意,需要在KMPDFFactory::setReaderView之後調用才不會被複寫
          */
         viewModel.getReaderView()?.apply {
+            val kmpdfFactory = viewModel.readerModel.kmpdfFactory ?: return@apply
             adapter = providePdfPageAdapter(
                     context,
                     FilePicker.FilePickerSupport {},
-                    viewModel.readerModel.kmpdfFactory!!
+                    kmpdfFactory
             )
             refresh(true)
         }

+ 1 - 1
src/main/java/com/kdanmobile/reader/Utils.kt

@@ -39,7 +39,7 @@ object Utils {
         stateListDrawable.addState(states[2], drawable)
         val state = stateListDrawable.constantState
         drawable = DrawableCompat.wrap(if (state == null) stateListDrawable else state.newDrawable()).mutate()
-        DrawableCompat.setTintList(drawable!!, colorList)
+        DrawableCompat.setTintList(drawable, colorList)
         drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
 
         if (view is AppCompatImageView) {

+ 3 - 3
src/main/java/com/kdanmobile/reader/copyfile/CopyFileTask.kt

@@ -48,15 +48,15 @@ class CopyFileTask(private val targetFolder: File) {
         if (null == source) return null
 
         val srcFileName = source.getFileName()
-        if (srcFileName.isNullOrEmpty()) return null
+        if (srcFileName == null || srcFileName.isEmpty()) return null
         val srcFilePath = source.getFilePath()
-        if (srcFilePath?.startsWith(targetFolder!!.absolutePath) == true) return srcFilePath
+        if (srcFilePath?.startsWith(targetFolder.absolutePath) == true) return srcFilePath
 
         var dstFile = File(targetFolder, srcFileName)
         if (dstFile.exists()) {
             when (copyMode) {
                 Mode.DO_NOTHING_IF_FILE_EXIST -> return dstFile.absolutePath
-                Mode.CREATE_NEW_FILE -> dstFile = generateUniqueFile(srcFileName!!)
+                Mode.CREATE_NEW_FILE -> dstFile = generateUniqueFile(srcFileName)
                 else -> {}
             }
         }

+ 2 - 2
src/main/java/com/kdanmobile/reader/copyfile/CopyFileViewModel.kt

@@ -60,8 +60,8 @@ class CopyFileViewModel(applicationContext: Context,
             return
         }
 
-        val source = if (!info.filePath.isNullOrEmpty()) {
-            FilePathSource(info.filePath!!)
+        val source = if (info.filePath != null && info.filePath.isNotEmpty()) {
+            FilePathSource(info.filePath)
         } else if (info.isActionView && null != info.uri) {
             FileUriSource(applicationContext, info.uri)
         } else {

+ 3 - 2
src/main/java/com/kdanmobile/reader/copyfile/FileCannotOpenDialogFragment.kt

@@ -13,10 +13,11 @@ class FileCannotOpenDialogFragment : DialogFragment() {
 
     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
 
-        return AlertDialog.Builder(activity!!)
+        val activity = activity ?: return super.onCreateDialog(savedInstanceState)
+        return AlertDialog.Builder(activity)
                 .setTitle(arguments?.getString(KEY_DIALOG_TITLE))
                 .setPositiveButton(getString(android.R.string.ok)) { dialog, which ->
-                    activity?.finish()
+                    activity.finish()
                 }
                 .create()
     }

+ 1 - 1
src/main/java/com/kdanmobile/reader/screen/adapter/BookmarkAdapter.kt

@@ -143,7 +143,7 @@ class BookmarkAdapter(private val pdfInfoHandler: PdfInfoHandler, private val th
         }
 
         internal fun stopLoadThumbSync() {
-            if (null != disposable && !disposable!!.isDisposed) {
+            if (disposable?.isDisposed == false) {
                 disposable?.dispose()
                 disposable = null
             }

+ 2 - 2
src/main/java/com/kdanmobile/reader/screen/adapter/OutlineAdapter.kt

@@ -33,11 +33,11 @@ class OutlineAdapter(private val outlineListItems: List<OutlineItem>): BaseAdapt
             holderView = view1.tag as HolderView
         }
 
-        val item = getItem(position)
+        val item = getItem(position) ?: return view1
         /****** 大纲层次缩进  */
         var space = ""
         val null_str = parent.context.getString(R.string.null_str)
-        for (i in 1 .. item!!.level) {
+        for (i in 1 .. item.level) {
             space += null_str + null_str
         }
 

+ 1 - 1
src/main/java/com/kdanmobile/reader/screen/adapter/ThumbnailAdapter.kt

@@ -115,7 +115,7 @@ class ThumbnailAdapter(private val pdfInfoHandler: PdfInfoHandler, private val t
         }
 
         internal fun stopLoadThumbSync() {
-            if (null != disposable && !disposable!!.isDisposed) {
+            if (disposable?.isDisposed == false) {
                 disposable?.dispose()
                 disposable = null
             }

+ 1 - 1
src/main/java/com/kdanmobile/reader/screen/contextmenu/MyKMPDFContextMenuCallback.kt

@@ -40,7 +40,7 @@ open class MyKMPDFContextMenuCallback(private val kmpdfFactory: KMPDFFactory?) :
         if (null != view && null == popupWindow) {
 
             //  create context menu depended on ContextMenuType
-            val context = view.context!!
+            val context = view.context ?: return
             val contentView = when (type) {
                 KMPDFContextMenuCallback.ContextMenuType.Markup -> createMarkupContextMenuView(context)
                 KMPDFContextMenuCallback.ContextMenuType.INK -> createInkContextMenuView(context)

+ 1 - 1
src/main/java/com/kdanmobile/reader/screen/reader/mediabox/signature/SignatureAdapter.kt

@@ -116,7 +116,7 @@ class SignatureViewHolder(view: View): RecyclerView.ViewHolder(view) {
     }
 
     internal fun stopLoadThumbSync() {
-        if (null != disposable && !disposable!!.isDisposed) {
+        if (disposable?.isDisposed == false) {
             disposable?.dispose()
             disposable = null
             ivSignature.setImageBitmap(null)

+ 7 - 6
src/main/java/com/kdanmobile/reader/screen/signature/SignatureDrawView.kt

@@ -73,12 +73,13 @@ class SignatureDrawView : View {
                 val y = event.getY(actionIndex)
                 when (event.action) {
                     MotionEvent.ACTION_DOWN -> {
-                        currentPath = Path()
-                        datas.add(SignaturePathData(currentPath!!, paintColor, strokeWidth))
-                        currentPath?.moveTo(x, y)
-                        currentPath?.lineTo(x, y)
-                        onSignatureDrawListener?.onStartSignature()
-                        pointList.add(PointF(x, y))
+                        currentPath = Path().also { currentPath ->
+                            datas.add(SignaturePathData(currentPath, paintColor, strokeWidth))
+                            currentPath.moveTo(x, y)
+                            currentPath.lineTo(x, y)
+                            onSignatureDrawListener?.onStartSignature()
+                            pointList.add(PointF(x, y))
+                        }
                     }
                     MotionEvent.ACTION_MOVE -> {
                         var last = pointList.last()

+ 1 - 1
src/main/java/com/kdanmobile/reader/screen/view/OutlineView.kt

@@ -47,7 +47,7 @@ class OutlineView: RelativeLayout, LeftToolbarView {
 
     override fun onDetachedFromWindow() {
         super.onDetachedFromWindow()
-        if (null != disposable && !disposable!!.isDisposed) {
+        if (disposable?.isDisposed == false) {
             disposable?.dispose()
             disposable = null
         }

+ 7 - 4
src/main/java/com/kdanmobile/reader/screen/view/SearchView.kt

@@ -320,13 +320,16 @@ class SearchView: RelativeLayout, View.OnClickListener, LeftToolbarView {
                 }
                 isDone = false
                 disposable = Completable.create {
+                    val pdfInfoHandler = pdfInfoHandler
+                    val searchHandler = searchHandler
+
                     if (null != pdfInfoHandler && null != searchHandler) {
-                        val pageCount = pdfInfoHandler!!.getPdfPageCount()
+                        val pageCount = pdfInfoHandler.getPdfPageCount()
                         for (pageIndex in searchStartPage until pageCount) {
                             if (!isDone) {
-                                val results = searchHandler!!.searchPage(pageIndex, searchText)
+                                val results = searchHandler.searchPage(pageIndex, searchText)
                                 if (results.isNotEmpty()) {
-                                    val chars = pdfInfoHandler!!.getPageChars(pageIndex)
+                                    val chars = pdfInfoHandler.getPageChars(pageIndex)
                                     val list = dealData(chars, results, searchText, pageIndex)
                                     msgHandler?.sendMessage(msgHandler?.obtainMessage(10, list))
                                     if (this.list.size + list.size >= searchResultNum) {
@@ -358,7 +361,7 @@ class SearchView: RelativeLayout, View.OnClickListener, LeftToolbarView {
 
     override fun onDetachedFromWindow() {
         super.onDetachedFromWindow()
-        if (null != disposable && !disposable!!.isDisposed) {
+        if (disposable?.isDisposed == false) {
             disposable?.dispose()
             disposable = null
         }

+ 3 - 2
src/main/java/com/kdanmobile/reader/setting/ReaderSettingDialogFragment.kt

@@ -38,13 +38,14 @@ open class ReaderSettingDialogFragment : DialogFragment() {
     protected open fun onNoPermissionToChangeTheme() {}
 
     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
+        val activity = activity ?: return super.onCreateDialog(savedInstanceState)
         val view = LayoutInflater.from(context).inflate(R.layout.activity_reader_setting, null)
         initViews(view)
         initViewModel()
 
         val dialog: Dialog
         if (isReaderSettingFullScreen) {
-            dialog = Dialog(activity!!)
+            dialog = Dialog(activity)
             dialog.window?.requestFeature(Window.FEATURE_NO_TITLE)
             dialog.setContentView(view)
             dialog.window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)
@@ -53,7 +54,7 @@ open class ReaderSettingDialogFragment : DialogFragment() {
                 dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
             }
         } else {
-            val builder = AlertDialog.Builder(activity!!)
+            val builder = AlertDialog.Builder(activity)
             dialog = builder.show()
             dialog.setContentView(view)
         }

+ 1 - 1
src/main/java/com/kdanmobile/reader/thumb/PdfThumbAdapter.kt

@@ -116,7 +116,7 @@ class PdfThumbAdapter(var data: ThumbData, var handler: PageMoveHandler, private
         }
 
         fun stopLoadThumbSync() {
-            if (null != disposable && !disposable!!.isDisposed) {
+            if (disposable?.isDisposed == false) {
                 disposable?.dispose()
                 disposable = null
             }

+ 15 - 13
src/main/java/com/kdanmobile/reader/thumb/PdfThumbDialogFragment.kt

@@ -69,7 +69,8 @@ abstract class PdfThumbDialogFragment:
         loadThumbAsynchronously()
         initToolBar()
 
-        val dialog = object: Dialog(activity!!) {
+        val activity = activity ?: return super.onCreateDialog(savedInstanceState)
+        val dialog = object: Dialog(activity) {
             override fun onBackPressed() {
                 onClickToolBarNavigation()
             }
@@ -100,41 +101,42 @@ abstract class PdfThumbDialogFragment:
     }
 
     private fun setViewSize() {
-        val context = activity!!
+        val activity = activity ?: return
         if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
-            if (ScreenUtil.getXDp(activity!!) >= 720) {
+            if (ScreenUtil.getXDp(activity) >= 720) {
                 columns = 6
-                width = (ScreenUtil.getScreenWidth(context) / 6.6).toInt()
+                width = (ScreenUtil.getScreenWidth(activity) / 6.6).toInt()
             } else {
                 columns = 5
-                width = (ScreenUtil.getScreenWidth(context) / 5.5).toInt()
+                width = (ScreenUtil.getScreenWidth(activity) / 5.5).toInt()
             }
         } else {
-            if (ScreenUtil.getXDp(activity!!) >= 720) {
+            if (ScreenUtil.getXDp(activity) >= 720) {
                 columns = 4
-                width = (ScreenUtil.getScreenWidth(context) / 4.4).toInt()
+                width = (ScreenUtil.getScreenWidth(activity) / 4.4).toInt()
             } else {
                 columns = 3
-                width = (ScreenUtil.getScreenWidth(context) / 3.3).toInt()
+                width = (ScreenUtil.getScreenWidth(activity) / 3.3).toInt()
             }
         }
-        padding = (ScreenUtil.getScreenWidth(context) - width * columns) / 2 * (columns + 1)
+        padding = (ScreenUtil.getScreenWidth(activity) - width * columns) / 2 * (columns + 1)
         height = 234 * width / 180
     }
 
     private fun loadThumbAsynchronously() {
+        val activity = activity ?: return
         disposable = viewModel.initThumbData(height, width, padding)
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
                 .subscribe({
                     pdfThumbAdapter = PdfThumbAdapter(it,this, viewModel.thumbnailHandler)
                     recyclerView.adapter = pdfThumbAdapter
-                    recyclerView.layoutManager = GridLayoutManager(activity!!, columns)
+                    recyclerView.layoutManager = GridLayoutManager(activity, columns)
                     recyclerView.addOnItemTouchListener(object : OnRecyclerItemClickListener(recyclerView) {
-                        override fun onItemClick(vh: RecyclerView.ViewHolder) {
-                            viewModel.mode.onPageItemClick(this@PdfThumbDialogFragment, vh.adapterPosition, viewModel.mapSelect, pdfThumbAdapter)
+                        override fun onItemClick(viewHolder: RecyclerView.ViewHolder) {
+                            viewModel.mode.onPageItemClick(this@PdfThumbDialogFragment, viewHolder.adapterPosition, viewModel.mapSelect, pdfThumbAdapter)
                         }
-                        override fun onItemLongClick(vh: RecyclerView.ViewHolder) {}
+                        override fun onItemLongClick(viewHolder: RecyclerView.ViewHolder) {}
                     })
                     recyclerView.scrollToPosition(viewModel.pdfInfoHandler.getCurrentPage())
                     val itemTouchHelperCallBack = ItemTouchHelpCallback(pdfThumbAdapter)

+ 1 - 1
src/main/java/com/kdanmobile/reader/thumb/PdfThumbViewModel.kt

@@ -253,7 +253,7 @@ class PdfThumbViewModel(private val readerModel: ReaderModel): ViewModel() {
     }
 
     override fun onCleared() {
-        if (null != disposable && !disposable!!.isDisposed) {
+        if (disposable?.isDisposed == false) {
             disposable?.dispose()
             disposable = null
         }

+ 8 - 9
src/main/java/com/kdanmobile/reader/utils/AnimationUtil.kt

@@ -77,7 +77,7 @@ class AnimationUtil private constructor() {
          * @param view
          */
         fun showViewFromBottomToTop(view: View?, duration: Long = 300) {
-            if (showBottomToTop != null && showBottomToTop!!.isRunning) {
+            if (showBottomToTop?.isRunning == true) {
                 if (showBottomToTop?.target == view) {
                     showBottomToTop?.cancel()
                     showBottomToTop = null
@@ -114,7 +114,7 @@ class AnimationUtil private constructor() {
          * @param view
          */
         fun hideViewFromTopToBottom(view: View?, duration: Long = 300) {
-            if (showBottomToTop != null && showBottomToTop!!.isRunning) {
+            if (showBottomToTop?.isRunning == true) {
                 if (showBottomToTop?.target == view) {
                     showBottomToTop?.cancel()
                     showBottomToTop = null
@@ -127,7 +127,6 @@ class AnimationUtil private constructor() {
                 val pvhA = PropertyValuesHolder.ofFloat("alpha", view.alpha, 0f)
                 val pvhY = PropertyValuesHolder.ofFloat("Y", Math.min(view.y, view.top.toFloat()), view.top.toFloat() + view.height)
                 showBottomToTop = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
-                showBottomToTop = ObjectAnimator.ofPropertyValuesHolder(view, pvhA, pvhY)
                 showBottomToTop?.apply {
                     interpolator = AccelerateInterpolator()
                     addListener(object : AnimatorListenerAdapter() {
@@ -147,7 +146,7 @@ class AnimationUtil private constructor() {
         }
 
         fun showViewFromRightToLeft(view: View?, duration: Long = 300) {
-            if (showRightToLeft != null && showRightToLeft!!.isRunning) {
+            if (showRightToLeft?.isRunning == true) {
                 if (showRightToLeft?.target == view) {
                     showRightToLeft?.cancel()
                     showRightToLeft = null
@@ -179,7 +178,7 @@ class AnimationUtil private constructor() {
         }
 
         fun hideViewFromLeftToRight(view: View?, duration: Long = 300) {
-            if (showRightToLeft != null && showRightToLeft!!.isRunning) {
+            if (showRightToLeft?.isRunning == true) {
                 if (showRightToLeft?.target == view) {
                     showRightToLeft?.cancel()
                     showRightToLeft = null
@@ -216,7 +215,7 @@ class AnimationUtil private constructor() {
          * @param view
          */
         fun showViewFromTopToBottom(view: View?, duration: Long = 300) {
-            if (showTopToBottom != null && showTopToBottom!!.isRunning) {
+            if (showTopToBottom?.isRunning == true) {
                 if (showTopToBottom?.target == view) {
                     showTopToBottom?.cancel()
                     showTopToBottom = null
@@ -253,7 +252,7 @@ class AnimationUtil private constructor() {
          * @param view
          */
         fun hideViewFromBottomToTop(view: View?, duration: Long = 300) {
-            if (showTopToBottom != null && showTopToBottom!!.isRunning) {
+            if (showTopToBottom?.isRunning == true) {
                 if (showTopToBottom?.target == view) {
                     showTopToBottom?.cancel()
                     showTopToBottom = null
@@ -285,7 +284,7 @@ class AnimationUtil private constructor() {
         }
 
         fun showViewFromLeftToRight(view: View?, duration: Long = 300) {
-            if (showLeftToRight != null && showLeftToRight!!.isRunning) {
+            if (showLeftToRight?.isRunning == true) {
                 if (showLeftToRight?.target == view) {
                     showLeftToRight?.cancel()
                     showLeftToRight = null
@@ -317,7 +316,7 @@ class AnimationUtil private constructor() {
         }
 
         fun hideViewFromRightToLeft(view: View?, duration: Long = 300) {
-            if (showLeftToRight != null && showLeftToRight!!.isRunning) {
+            if (showLeftToRight?.isRunning == true) {
                 if (showLeftToRight?.target == view) {
                     showLeftToRight?.cancel()
                     showLeftToRight = null