Skip to content

Commit

Permalink
Add 'when' to substitute if (#56)
Browse files Browse the repository at this point in the history
* Adding 'when' to substitute if

ALSO:

* Removed the 'm' from 'mFilePathCallback'. The hungarian notation is not recommended anywhere, only on the AOSP but not for kotlin files. Here's a good discussion about it: https://stackoverflow.com/questions/111933/why-shouldnt-i-use-hungarian-notation\#112080

* Removing redundant usage of ActivityCompat

The ActivityCompat is a helper class to validate versions of the SDK and remove that code from the developer side. It's not needed in this case as the minSDK and the targetSDK are pointing to highe values than 16. The method startActivityForResult on this helper class checks for SDK >= 16.
  • Loading branch information
sierisimo authored Mar 30, 2020
1 parent 8c5e0ac commit a3941fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
26 changes: 13 additions & 13 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="to.dev.dev_android">
xmlns:tools="http://schemas.android.com/tools"
package="to.dev.dev_android">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:fullBackupContent="true"
android:usesCleartextTraffic="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute"
>
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute">
<activity
android:name=".view.main.view.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:scheme="https"
android:host="dev.to"
/>
android:scheme="https" />
</intent-filter>
</activity>
</application>
Expand Down
32 changes: 16 additions & 16 deletions app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.net.Uri
import android.os.Bundle
import android.view.View
import android.webkit.ValueCallback
import androidx.core.app.ActivityCompat
import to.dev.dev_android.R
import to.dev.dev_android.base.BuildConfig
import to.dev.dev_android.base.activity.BaseActivity
Expand All @@ -17,7 +16,7 @@ import to.dev.dev_android.util.AndroidWebViewBridge
class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.CustomListener {
private val webViewBridge: AndroidWebViewBridge = AndroidWebViewBridge()

private var mFilePathCallback: ValueCallback<Array<Uri>>? = null
private var filePathCallback: ValueCallback<Array<Uri>>? = null

override fun layout(): Int {
return R.layout.activity_main
Expand Down Expand Up @@ -75,16 +74,16 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.
}

override fun launchGallery(filePathCallback: ValueCallback<Array<Uri>>?) {
mFilePathCallback = filePathCallback
this.filePathCallback = filePathCallback

val galleryIntent = Intent()
// Show only images, no videos or anything else
galleryIntent.type = "image/*"
galleryIntent.action = Intent.ACTION_PICK
val galleryIntent = Intent().apply {
// Show only images, no videos or anything else
type = "image/*"
action = Intent.ACTION_PICK
}

// Always show the chooser (if there are multiple options available)
ActivityCompat.startActivityForResult(
this,
startActivityForResult(
Intent.createChooser(galleryIntent, "Select Picture"),
PIC_CHOOSER_REQUEST,
null // No additional data
Expand All @@ -96,14 +95,15 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.
return super.onActivityResult(requestCode, resultCode, data)
}

if (resultCode == Activity.RESULT_OK) {
if (data != null) {
mFilePathCallback?.onReceiveValue(arrayOf(data.data))
mFilePathCallback = null
when (resultCode) {
Activity.RESULT_OK -> data?.data?.let {
filePathCallback?.onReceiveValue(arrayOf(it))
filePathCallback = null
}
Activity.RESULT_CANCELED -> {
filePathCallback?.onReceiveValue(null)
filePathCallback = null
}
} else if (resultCode == Activity.RESULT_CANCELED) {
mFilePathCallback?.onReceiveValue(null)
mFilePathCallback = null
}
}

Expand Down

0 comments on commit a3941fb

Please sign in to comment.