mirror of
https://github.com/karasevm/PrivateDNSAndroid.git
synced 2025-06-28 20:29:56 +00:00
Migrate from Kotlin synthetics to Jetpack view binding
This commit is contained in:
parent
39eb9a36c0
commit
3d1e824033
4 changed files with 40 additions and 22 deletions
|
@ -17,7 +17,9 @@ android {
|
||||||
targetSdkVersion 30
|
targetSdkVersion 30
|
||||||
minSdkVersion 28
|
minSdkVersion 28
|
||||||
}
|
}
|
||||||
apply plugin: 'kotlin-android-extensions'
|
buildFeatures {
|
||||||
|
viewBinding true
|
||||||
|
}
|
||||||
buildTypes {
|
buildTypes {
|
||||||
release {
|
release {
|
||||||
minifyEnabled false
|
minifyEnabled false
|
||||||
|
|
|
@ -4,17 +4,23 @@ import android.app.Dialog
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.DialogInterface
|
import android.content.DialogInterface
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.view.LayoutInflater
|
||||||
|
import android.view.ViewGroup
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
import kotlinx.android.synthetic.main.dialog_add.*
|
import ru.karasevm.privatednstoggle.databinding.DialogAddBinding
|
||||||
import kotlinx.android.synthetic.main.dialog_add.view.*
|
|
||||||
|
|
||||||
|
|
||||||
class AddServerDialogFragment(): DialogFragment() {
|
class AddServerDialogFragment() : DialogFragment() {
|
||||||
// Use this instance of the interface to deliver action events
|
// Use this instance of the interface to deliver action events
|
||||||
internal lateinit var listener: NoticeDialogListener
|
internal lateinit var listener: NoticeDialogListener
|
||||||
|
|
||||||
|
private var _binding: DialogAddBinding? = null
|
||||||
|
|
||||||
|
// This property is only valid between onCreateView and
|
||||||
|
// onDestroyView.
|
||||||
|
private val binding get() = _binding!!
|
||||||
|
|
||||||
/* The activity that creates an instance of this dialog fragment must
|
/* The activity that creates an instance of this dialog fragment must
|
||||||
* implement this interface in order to receive event callbacks.
|
* implement this interface in order to receive event callbacks.
|
||||||
* Each method passes the DialogFragment in case the host needs to query it. */
|
* Each method passes the DialogFragment in case the host needs to query it. */
|
||||||
|
@ -31,27 +37,37 @@ class AddServerDialogFragment(): DialogFragment() {
|
||||||
listener = context as NoticeDialogListener
|
listener = context as NoticeDialogListener
|
||||||
} catch (e: ClassCastException) {
|
} catch (e: ClassCastException) {
|
||||||
// The activity doesn't implement the interface, throw exception
|
// The activity doesn't implement the interface, throw exception
|
||||||
throw ClassCastException((context.toString() +
|
throw ClassCastException(
|
||||||
" must implement NoticeDialogListener"))
|
(context.toString() +
|
||||||
|
" must implement NoticeDialogListener")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
||||||
|
override fun onCreateDialog(
|
||||||
|
savedInstanceState: Bundle?
|
||||||
|
): Dialog {
|
||||||
return activity?.let {
|
return activity?.let {
|
||||||
val builder = AlertDialog.Builder(it)
|
val builder = AlertDialog.Builder(it)
|
||||||
// Get the layout inflater
|
// Get the layout inflater
|
||||||
val inflater = requireActivity().layoutInflater;
|
val inflater = requireActivity().layoutInflater;
|
||||||
val view = inflater.inflate(R.layout.dialog_add, null)
|
_binding = DialogAddBinding.inflate(inflater)
|
||||||
|
|
||||||
|
val view = binding.root
|
||||||
// Inflate and set the layout for the dialog
|
// Inflate and set the layout for the dialog
|
||||||
// Pass null as the parent view because its going in the dialog layout
|
// Pass null as the parent view because its going in the dialog layout
|
||||||
builder.setTitle(R.string.add_server)
|
builder.setTitle(R.string.add_server)
|
||||||
.setView(view)
|
.setView(view)
|
||||||
// Add action buttons
|
// Add action buttons
|
||||||
.setPositiveButton(R.string.add,
|
.setPositiveButton(R.string.add,
|
||||||
DialogInterface.OnClickListener { dialog, id ->
|
DialogInterface.OnClickListener { _, _ ->
|
||||||
listener.onDialogPositiveClick(this,view.editTextServerAddr.text.toString())
|
listener.onDialogPositiveClick(
|
||||||
|
this,
|
||||||
|
binding.editTextServerAddr.text.toString()
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.setNegativeButton(R.string.cancel,
|
.setNegativeButton(R.string.cancel,
|
||||||
DialogInterface.OnClickListener { dialog, id ->
|
DialogInterface.OnClickListener { _, _ ->
|
||||||
getDialog()?.cancel()
|
getDialog()?.cancel()
|
||||||
})
|
})
|
||||||
builder.create()
|
builder.create()
|
||||||
|
|
|
@ -4,11 +4,8 @@ import android.app.Dialog
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.DialogInterface
|
import android.content.DialogInterface
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
import kotlinx.android.synthetic.main.dialog_add.*
|
|
||||||
import kotlinx.android.synthetic.main.dialog_add.view.*
|
|
||||||
|
|
||||||
|
|
||||||
class DeleteServerDialogFragment(val position: Int): DialogFragment() {
|
class DeleteServerDialogFragment(val position: Int): DialogFragment() {
|
||||||
|
|
|
@ -4,35 +4,38 @@ import android.Manifest
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.ListAdapter
|
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import ru.karasevm.privatednstoggle.databinding.ActivityMainBinding
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity(), AddServerDialogFragment.NoticeDialogListener, DeleteServerDialogFragment.NoticeDialogListener {
|
class MainActivity : AppCompatActivity(), AddServerDialogFragment.NoticeDialogListener, DeleteServerDialogFragment.NoticeDialogListener {
|
||||||
|
|
||||||
private lateinit var linearLayoutManager: LinearLayoutManager
|
private lateinit var linearLayoutManager: LinearLayoutManager
|
||||||
|
private lateinit var binding: ActivityMainBinding
|
||||||
public var items = mutableListOf<String>()
|
public var items = mutableListOf<String>()
|
||||||
lateinit var sharedPrefs: SharedPreferences
|
lateinit var sharedPrefs: SharedPreferences
|
||||||
lateinit var adapter: RecyclerAdapter
|
lateinit var adapter: RecyclerAdapter
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
|
||||||
|
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||||
|
val view = binding.root
|
||||||
|
setContentView(view)
|
||||||
|
|
||||||
if (checkSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
|
if (checkSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
|
||||||
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://karasevm.github.io/PrivateDNSAndroid/"))
|
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://karasevm.github.io/PrivateDNSAndroid/"))
|
||||||
startActivity(browserIntent)
|
startActivity(browserIntent)
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
linearLayoutManager = LinearLayoutManager(this)
|
linearLayoutManager = LinearLayoutManager(this)
|
||||||
recyclerView.layoutManager = linearLayoutManager
|
binding.recyclerView.layoutManager = linearLayoutManager
|
||||||
|
|
||||||
sharedPrefs = this.getSharedPreferences("app_prefs", 0);
|
sharedPrefs = this.getSharedPreferences("app_prefs", 0);
|
||||||
|
|
||||||
|
@ -45,7 +48,7 @@ class MainActivity : AppCompatActivity(), AddServerDialogFragment.NoticeDialogLi
|
||||||
val newFragment = DeleteServerDialogFragment(position)
|
val newFragment = DeleteServerDialogFragment(position)
|
||||||
newFragment.show(supportFragmentManager, "delete_server")
|
newFragment.show(supportFragmentManager, "delete_server")
|
||||||
}
|
}
|
||||||
recyclerView.adapter = adapter
|
binding.recyclerView.adapter = adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||||
|
@ -79,7 +82,7 @@ class MainActivity : AppCompatActivity(), AddServerDialogFragment.NoticeDialogLi
|
||||||
}
|
}
|
||||||
items.add(server)
|
items.add(server)
|
||||||
adapter.setData(items.toMutableList())
|
adapter.setData(items.toMutableList())
|
||||||
recyclerView.adapter?.notifyItemInserted(items.size - 1)
|
binding.recyclerView.adapter?.notifyItemInserted(items.size - 1)
|
||||||
sharedPrefs.edit()
|
sharedPrefs.edit()
|
||||||
.putString("dns_servers", items.joinToString(separator = ",") { it -> it }).commit()
|
.putString("dns_servers", items.joinToString(separator = ",") { it -> it }).commit()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue