Skip to content

Commit a3941fb

Browse files
authored
Add 'when' to substitute if (#56)
* 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.
1 parent 8c5e0ac commit a3941fb

2 files changed

Lines changed: 29 additions & 29 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
4-
package="to.dev.dev_android">
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="to.dev.dev_android">
55

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

88
<application
99
android:allowBackup="true"
1010
android:fullBackupContent="true"
11-
android:usesCleartextTraffic="true"
1211
android:icon="@mipmap/ic_launcher"
1312
android:label="@string/app_name"
1413
android:roundIcon="@mipmap/ic_launcher_round"
1514
android:supportsRtl="true"
1615
android:theme="@style/AppTheme"
17-
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute"
18-
>
16+
android:usesCleartextTraffic="true"
17+
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute">
1918
<activity
2019
android:name=".view.main.view.MainActivity"
2120
android:screenOrientation="portrait">
2221
<intent-filter>
23-
<action android:name="android.intent.action.MAIN"/>
22+
<action android:name="android.intent.action.MAIN" />
2423

25-
<category android:name="android.intent.category.LAUNCHER"/>
24+
<category android:name="android.intent.category.LAUNCHER" />
2625
</intent-filter>
2726
<intent-filter android:autoVerify="true">
28-
<action android:name="android.intent.action.VIEW"/>
29-
<category android:name="android.intent.category.DEFAULT"/>
30-
<category android:name="android.intent.category.BROWSABLE"/>
27+
<action android:name="android.intent.action.VIEW" />
28+
29+
<category android:name="android.intent.category.DEFAULT" />
30+
<category android:name="android.intent.category.BROWSABLE" />
31+
3132
<data
32-
android:scheme="https"
3333
android:host="dev.to"
34-
/>
34+
android:scheme="https" />
3535
</intent-filter>
3636
</activity>
3737
</application>

app/src/main/java/to/dev/dev_android/view/main/view/MainActivity.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import android.net.Uri
77
import android.os.Bundle
88
import android.view.View
99
import android.webkit.ValueCallback
10-
import androidx.core.app.ActivityCompat
1110
import to.dev.dev_android.R
1211
import to.dev.dev_android.base.BuildConfig
1312
import to.dev.dev_android.base.activity.BaseActivity
@@ -17,7 +16,7 @@ import to.dev.dev_android.util.AndroidWebViewBridge
1716
class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.CustomListener {
1817
private val webViewBridge: AndroidWebViewBridge = AndroidWebViewBridge()
1918

20-
private var mFilePathCallback: ValueCallback<Array<Uri>>? = null
19+
private var filePathCallback: ValueCallback<Array<Uri>>? = null
2120

2221
override fun layout(): Int {
2322
return R.layout.activity_main
@@ -75,16 +74,16 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), CustomWebChromeClient.
7574
}
7675

7776
override fun launchGallery(filePathCallback: ValueCallback<Array<Uri>>?) {
78-
mFilePathCallback = filePathCallback
77+
this.filePathCallback = filePathCallback
7978

80-
val galleryIntent = Intent()
81-
// Show only images, no videos or anything else
82-
galleryIntent.type = "image/*"
83-
galleryIntent.action = Intent.ACTION_PICK
79+
val galleryIntent = Intent().apply {
80+
// Show only images, no videos or anything else
81+
type = "image/*"
82+
action = Intent.ACTION_PICK
83+
}
8484

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

99-
if (resultCode == Activity.RESULT_OK) {
100-
if (data != null) {
101-
mFilePathCallback?.onReceiveValue(arrayOf(data.data))
102-
mFilePathCallback = null
98+
when (resultCode) {
99+
Activity.RESULT_OK -> data?.data?.let {
100+
filePathCallback?.onReceiveValue(arrayOf(it))
101+
filePathCallback = null
102+
}
103+
Activity.RESULT_CANCELED -> {
104+
filePathCallback?.onReceiveValue(null)
105+
filePathCallback = null
103106
}
104-
} else if (resultCode == Activity.RESULT_CANCELED) {
105-
mFilePathCallback?.onReceiveValue(null)
106-
mFilePathCallback = null
107107
}
108108
}
109109

0 commit comments

Comments
 (0)