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

View File

@ -1,7 +1,18 @@
<?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:tools="http://schemas.android.com/tools"
xmlns:tools="http://schemas.android.com/tools">
<data>
<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_height="match_parent">
@ -11,6 +22,8 @@
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:items="@{tunnels}"
app:layout="@{@layout/tv_tunnel_list_item}"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toTopOf="@id/import_button"
app:layout_constraintStart_toStartOf="parent"
@ -29,3 +42,4 @@
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View File

@ -9,10 +9,6 @@
<import type="com.wireguard.android.backend.Tunnel.State" />
<variable
name="collection"
type="com.wireguard.android.databinding.ObservableKeyedArrayList&lt;String, ObservableTunnel&gt;" />
<variable
name="key"
type="String" />
@ -20,11 +16,6 @@
<variable
name="item"
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>
<com.google.android.material.card.MaterialCardView