tv: begin to wire up databindings

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-09-22 14:33:04 +02:00
parent aaa55c0dcc
commit dc002d77fa
3 changed files with 48 additions and 47 deletions

View File

@ -8,14 +8,14 @@ package com.wireguard.android.activity
import android.os.Bundle import android.os.Bundle
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.google.android.material.button.MaterialButton import com.wireguard.android.Application
import com.wireguard.android.R import com.wireguard.android.databinding.TvActivityBinding
import com.wireguard.android.model.ObservableTunnel
import com.wireguard.android.util.TunnelImporter import com.wireguard.android.util.TunnelImporter
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class TvMainActivity : BaseActivity() { class TvMainActivity : AppCompatActivity() {
private val tunnelFileImportResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { data -> private val tunnelFileImportResultLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { data ->
lifecycleScope.launch { lifecycleScope.launch {
TunnelImporter.importTunnel(contentResolver, data) { TunnelImporter.importTunnel(contentResolver, data) {
@ -24,18 +24,14 @@ class TvMainActivity : BaseActivity() {
} }
} }
override fun onSelectedTunnelChanged(oldTunnel: ObservableTunnel?, newTunnel: ObservableTunnel?) {
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.tv_activity) val binding = TvActivityBinding.inflate(layoutInflater)
findViewById<MaterialButton>(R.id.import_button).setOnClickListener { lifecycleScope.launch { binding.tunnels = Application.getTunnelManager().getTunnels() }
binding.importButton.setOnClickListener {
tunnelFileImportResultLauncher.launch("*/*") tunnelFileImportResultLauncher.launch("*/*")
} }
} binding.executePendingBindings()
setContentView(binding.root)
companion object {
const val TAG = "WireGuard/TvMainActivity"
} }
} }

View File

@ -1,31 +1,45 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools">
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView <data>
android:id="@+id/tunnel_list"
<import type="com.wireguard.android.model.ObservableTunnel" />
<variable
name="tunnels"
type="com.wireguard.android.databinding.ObservableKeyedArrayList&lt;String, ObservableTunnel&gt;" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toTopOf="@id/import_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="10"
tools:listitem="@layout/tv_tunnel_list_item" />
<com.google.android.material.button.MaterialButton <androidx.recyclerview.widget.RecyclerView
android:id="@+id/import_button" android:id="@+id/tunnel_list"
style="?attr/textAppearanceButton" android:layout_width="match_parent"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_height="wrap_content" android:layout_marginTop="16dp"
android:layout_margin="16dp" android:orientation="horizontal"
android:text="@string/create_from_file" app:items="@{tunnels}"
app:layout_constraintBottom_toBottomOf="parent" app:layout="@{@layout/tv_tunnel_list_item}"
app:layout_constraintEnd_toEndOf="parent" /> app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toTopOf="@id/import_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="10"
tools:listitem="@layout/tv_tunnel_list_item" />
</androidx.constraintlayout.widget.ConstraintLayout> <com.google.android.material.button.MaterialButton
android:id="@+id/import_button"
style="?attr/textAppearanceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/create_from_file"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View File

@ -9,10 +9,6 @@
<import type="com.wireguard.android.backend.Tunnel.State" /> <import type="com.wireguard.android.backend.Tunnel.State" />
<variable
name="collection"
type="com.wireguard.android.databinding.ObservableKeyedArrayList&lt;String, ObservableTunnel&gt;" />
<variable <variable
name="key" name="key"
type="String" /> type="String" />
@ -20,11 +16,6 @@
<variable <variable
name="item" name="item"
type="com.wireguard.android.model.ObservableTunnel" /> type="com.wireguard.android.model.ObservableTunnel" />
<!-- Unused on TV but we retain this so the existing Adapter and ViewHolder can be reused -->
<variable
name="fragment"
type="com.wireguard.android.fragment.TunnelListFragment" />
</data> </data>
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView