diff --git a/V2rayNG/app/build.gradle b/V2rayNG/app/build.gradle index 9afbe5185..a026cd047 100644 --- a/V2rayNG/app/build.gradle +++ b/V2rayNG/app/build.gradle @@ -86,6 +86,7 @@ dependencies { implementation 'androidx.viewpager2:viewpager2:1.0.0' // Androidx ktx + implementation 'androidx.activity:activity-ktx:1.2.4' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1' diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Dialog.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Dialog.kt deleted file mode 100644 index 1addbfe10..000000000 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Dialog.kt +++ /dev/null @@ -1,244 +0,0 @@ -package com.v2ray.ang.extension - -import android.app.Fragment -import android.app.ProgressDialog -import android.content.Context -import android.content.DialogInterface -import android.database.Cursor -import android.graphics.drawable.Drawable -import androidx.appcompat.app.AlertDialog -import android.view.KeyEvent -import android.view.View -import android.widget.ListAdapter - - -fun Context.alertView( - title: String? = null, - view: View, - init: (KAlertDialogBuilder.() -> Unit)? = null -) = KAlertDialogBuilder(this).apply { - if (title != null) title(title) - if (title != null) customView(view) - if (init != null) init() -} - -fun Fragment.alert( - message: String, - title: String? = null, - init: (KAlertDialogBuilder.() -> Unit)? = null -) = activity.alert(message, title, init) - -fun Context.alert( - message: String, - title: String? = null, - init: (KAlertDialogBuilder.() -> Unit)? = null -) = KAlertDialogBuilder(this).apply { - if (title != null) title(title) - message(message) - if (init != null) init() -} - -fun Fragment.alert( - message: Int, - title: Int? = null, - init: (KAlertDialogBuilder.() -> Unit)? = null -) = activity.alert(message, title, init) - -fun Context.alert( - message: Int, - title: Int? = null, - init: (KAlertDialogBuilder.() -> Unit)? = null -) = KAlertDialogBuilder(this).apply { - if (title != null) title(title) - message(message) - if (init != null) init() -} - - -fun Fragment.alert(init: KAlertDialogBuilder.() -> Unit): KAlertDialogBuilder = activity.alert(init) - -fun Context.alert(init: KAlertDialogBuilder.() -> Unit) = KAlertDialogBuilder(this).apply { init() } - -fun Fragment.progressDialog( - message: Int? = null, - title: Int? = null, - init: (ProgressDialog.() -> Unit)? = null -) = activity.progressDialog(message, title, init) - -fun Context.progressDialog( - message: Int? = null, - title: Int? = null, - init: (ProgressDialog.() -> Unit)? = null -) = progressDialog(false, message?.let { getString(it) }, title?.let { getString(it) }, init) - -fun Fragment.indeterminateProgressDialog( - message: Int? = null, - title: Int? = null, - init: (ProgressDialog.() -> Unit)? = null -) = activity.progressDialog(message, title, init) - -fun Context.indeterminateProgressDialog( - message: Int? = null, - title: Int? = null, - init: (ProgressDialog.() -> Unit)? = null -) = progressDialog(true, message?.let { getString(it) }, title?.let { getString(it) }, init) - -fun Fragment.progressDialog( - message: String? = null, - title: String? = null, - init: (ProgressDialog.() -> Unit)? = null -) = activity.progressDialog(message, title, init) - -fun Context.progressDialog( - message: String? = null, - title: String? = null, - init: (ProgressDialog.() -> Unit)? = null -) = progressDialog(false, message, title, init) - -fun Fragment.indeterminateProgressDialog( - message: String? = null, - title: String? = null, - init: (ProgressDialog.() -> Unit)? = null -) = activity.indeterminateProgressDialog(message, title, init) - -fun Context.indeterminateProgressDialog( - message: String? = null, - title: String? = null, - init: (ProgressDialog.() -> Unit)? = null -) = progressDialog(true, message, title, init) - -private fun Context.progressDialog( - indeterminate: Boolean, - message: String? = null, - title: String? = null, - init: (ProgressDialog.() -> Unit)? = null -) = ProgressDialog(this).apply { - isIndeterminate = indeterminate - if (!indeterminate) setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) - if (message != null) setMessage(message) - if (title != null) setTitle(title) - if (init != null) init() - show() -} - -fun Fragment.selector( - title: CharSequence? = null, - items: List, - onClick: (Int) -> Unit -): Unit = activity.selector(title, items, onClick) - -fun Context.selector( - title: CharSequence? = null, - items: List, - onClick: (Int) -> Unit -) { - with(KAlertDialogBuilder(this)) { - if (title != null) title(title) - items(items, onClick) - show() - } -} - -class KAlertDialogBuilder(val ctx: Context) { - - val builder: AlertDialog.Builder = AlertDialog.Builder(ctx) - protected var dialog: AlertDialog? = null - - fun dismiss() { - dialog?.dismiss() - } - - fun show(): KAlertDialogBuilder { - dialog = builder.create() - dialog!!.show() - return this - } - - fun title(title: CharSequence) { - builder.setTitle(title) - } - - fun title(resource: Int) { - builder.setTitle(resource) - } - - fun message(title: CharSequence) { - builder.setMessage(title) - } - - fun message(resource: Int) { - builder.setMessage(resource) - } - - fun icon(icon: Int) { - builder.setIcon(icon) - } - - fun icon(icon: Drawable) { - builder.setIcon(icon) - } - - fun customTitle(title: View) { - builder.setCustomTitle(title) - } - - fun customView(view: View) { - builder.setView(view) - } - - fun cancellable(value: Boolean = true) { - builder.setCancelable(value) - } - - fun onCancel(f: () -> Unit) { - builder.setOnCancelListener { f() } - } - - fun onKey(f: (keyCode: Int, e: KeyEvent) -> Boolean) { - builder.setOnKeyListener({ dialog, keyCode, event -> f(keyCode, event) }) - } - - fun neutralButton(textResource: Int = android.R.string.ok, f: DialogInterface.() -> Unit = { dismiss() }) { - neutralButton(ctx.getString(textResource), f) - } - - fun neutralButton(title: String, f: DialogInterface.() -> Unit = { dismiss() }) { - builder.setNeutralButton(title, { dialog, which -> dialog.f() }) - } - - fun positiveButton(textResource: Int = android.R.string.ok, f: DialogInterface.() -> Unit) { - positiveButton(ctx.getString(textResource), f) - } - - fun positiveButton(title: String, f: DialogInterface.() -> Unit) { - builder.setPositiveButton(title, { dialog, which -> dialog.f() }) - } - - fun negativeButton(textResource: Int = android.R.string.cancel, f: DialogInterface.() -> Unit = { dismiss() }) { - negativeButton(ctx.getString(textResource), f) - } - - fun negativeButton(title: String, f: DialogInterface.() -> Unit = { dismiss() }) { - builder.setNegativeButton(title, { dialog, which -> dialog.f() }) - } - - fun items(itemsId: Int, f: (which: Int) -> Unit) { - items(ctx.resources!!.getTextArray(itemsId), f) - } - - fun items(items: List, f: (which: Int) -> Unit) { - items(items.toTypedArray(), f) - } - - fun items(items: Array, f: (which: Int) -> Unit) { - builder.setItems(items, { dialog, which -> f(which) }) - } - - fun adapter(adapter: ListAdapter, f: (which: Int) -> Unit) { - builder.setAdapter(adapter, { dialog, which -> f(which) }) - } - - fun adapter(cursor: Cursor, labelColumn: String, f: (which: Int) -> Unit) { - builder.setCursor(cursor, { dialog, which -> f(which) }, labelColumn) - } -} diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index ecbbec167..07d4a0089 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -1,7 +1,6 @@ package com.v2ray.ang.ui import android.Manifest -import androidx.lifecycle.ViewModelProviders import android.content.Intent import android.net.Uri import android.net.VpnService @@ -18,6 +17,7 @@ import android.view.Menu import android.view.MenuItem import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts +import androidx.activity.viewModels import com.tbruyelle.rxpermissions.RxPermissions import com.tencent.mmkv.MMKV import com.v2ray.ang.AppConfig @@ -55,7 +55,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } } private var mItemTouchHelper: ItemTouchHelper? = null - val mainViewModel: MainViewModel by lazy { ViewModelProviders.of(this).get(MainViewModel::class.java) } + val mainViewModel: MainViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -504,7 +504,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } fun showCircle() { - binding.fabProgressCircle?.show() + binding.fabProgressCircle.show() } fun hideCircle() { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyActivity.kt index 99430e6e7..2b611c866 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyActivity.kt @@ -152,7 +152,7 @@ class PerAppProxyActivity : BaseActivity() { } binding.switchBypassApps.isChecked = defaultSharedPreferences.getBoolean(AppConfig.PREF_BYPASS_APPS, false) - binding.etSearch.setOnEditorActionListener { v, actionId, event -> + binding.etSearch.setOnEditorActionListener { v, actionId, _ -> if (actionId == EditorInfo.IME_ACTION_SEARCH) { //hide var imm: InputMethodManager = v.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SettingsActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SettingsActivity.kt index ff5f57055..87226b082 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SettingsActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SettingsActivity.kt @@ -1,11 +1,11 @@ package com.v2ray.ang.ui -import androidx.lifecycle.ViewModelProviders import android.content.Intent import android.os.Bundle import androidx.preference.* import android.text.TextUtils import android.view.View +import androidx.activity.viewModels import com.v2ray.ang.R import com.v2ray.ang.AppConfig import com.v2ray.ang.extension.toast @@ -14,8 +14,7 @@ import com.v2ray.ang.util.Utils import com.v2ray.ang.viewmodel.SettingsViewModel class SettingsActivity : BaseActivity() { - - private val settingsViewModel by lazy { ViewModelProviders.of(this).get(SettingsViewModel::class.java) } + private val settingsViewModel: SettingsViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)