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
|
||||
minSdkVersion 28
|
||||
}
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
buildFeatures {
|
||||
viewBinding true
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
|
|
|
@ -4,17 +4,23 @@ import android.app.Dialog
|
|||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import kotlinx.android.synthetic.main.dialog_add.*
|
||||
import kotlinx.android.synthetic.main.dialog_add.view.*
|
||||
import ru.karasevm.privatednstoggle.databinding.DialogAddBinding
|
||||
|
||||
|
||||
class AddServerDialogFragment(): DialogFragment() {
|
||||
class AddServerDialogFragment() : DialogFragment() {
|
||||
// Use this instance of the interface to deliver action events
|
||||
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
|
||||
* implement this interface in order to receive event callbacks.
|
||||
* Each method passes the DialogFragment in case the host needs to query it. */
|
||||
|
@ -31,27 +37,37 @@ class AddServerDialogFragment(): DialogFragment() {
|
|||
listener = context as NoticeDialogListener
|
||||
} catch (e: ClassCastException) {
|
||||
// The activity doesn't implement the interface, throw exception
|
||||
throw ClassCastException((context.toString() +
|
||||
" must implement NoticeDialogListener"))
|
||||
throw ClassCastException(
|
||||
(context.toString() +
|
||||
" must implement NoticeDialogListener")
|
||||
)
|
||||
}
|
||||
}
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
|
||||
override fun onCreateDialog(
|
||||
savedInstanceState: Bundle?
|
||||
): Dialog {
|
||||
return activity?.let {
|
||||
val builder = AlertDialog.Builder(it)
|
||||
// Get the layout inflater
|
||||
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
|
||||
// Pass null as the parent view because its going in the dialog layout
|
||||
builder.setTitle(R.string.add_server)
|
||||
.setView(view)
|
||||
// Add action buttons
|
||||
.setPositiveButton(R.string.add,
|
||||
DialogInterface.OnClickListener { dialog, id ->
|
||||
listener.onDialogPositiveClick(this,view.editTextServerAddr.text.toString())
|
||||
DialogInterface.OnClickListener { _, _ ->
|
||||
listener.onDialogPositiveClick(
|
||||
this,
|
||||
binding.editTextServerAddr.text.toString()
|
||||
)
|
||||
})
|
||||
.setNegativeButton(R.string.cancel,
|
||||
DialogInterface.OnClickListener { dialog, id ->
|
||||
DialogInterface.OnClickListener { _, _ ->
|
||||
getDialog()?.cancel()
|
||||
})
|
||||
builder.create()
|
||||
|
|
|
@ -4,11 +4,8 @@ import android.app.Dialog
|
|||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
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() {
|
||||
|
|
|
@ -4,35 +4,38 @@ import android.Manifest
|
|||
import android.content.SharedPreferences
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import ru.karasevm.privatednstoggle.databinding.ActivityMainBinding
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity(), AddServerDialogFragment.NoticeDialogListener, DeleteServerDialogFragment.NoticeDialogListener {
|
||||
|
||||
private lateinit var linearLayoutManager: LinearLayoutManager
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
public var items = mutableListOf<String>()
|
||||
lateinit var sharedPrefs: SharedPreferences
|
||||
lateinit var adapter: RecyclerAdapter
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
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) {
|
||||
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://karasevm.github.io/PrivateDNSAndroid/"))
|
||||
startActivity(browserIntent)
|
||||
finish()
|
||||
}
|
||||
linearLayoutManager = LinearLayoutManager(this)
|
||||
recyclerView.layoutManager = linearLayoutManager
|
||||
binding.recyclerView.layoutManager = linearLayoutManager
|
||||
|
||||
sharedPrefs = this.getSharedPreferences("app_prefs", 0);
|
||||
|
||||
|
@ -45,7 +48,7 @@ class MainActivity : AppCompatActivity(), AddServerDialogFragment.NoticeDialogLi
|
|||
val newFragment = DeleteServerDialogFragment(position)
|
||||
newFragment.show(supportFragmentManager, "delete_server")
|
||||
}
|
||||
recyclerView.adapter = adapter
|
||||
binding.recyclerView.adapter = adapter
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
|
@ -79,7 +82,7 @@ class MainActivity : AppCompatActivity(), AddServerDialogFragment.NoticeDialogLi
|
|||
}
|
||||
items.add(server)
|
||||
adapter.setData(items.toMutableList())
|
||||
recyclerView.adapter?.notifyItemInserted(items.size - 1)
|
||||
binding.recyclerView.adapter?.notifyItemInserted(items.size - 1)
|
||||
sharedPrefs.edit()
|
||||
.putString("dns_servers", items.joinToString(separator = ",") { it -> it }).commit()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue