Skip to content

Commit

Permalink
Implement searching in server list from Urlbar
Browse files Browse the repository at this point in the history
Re: #196
  • Loading branch information
gujjwal00 committed Dec 25, 2023
1 parent 12f235f commit c2ff8ae
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ interface ServerProfileDao {
@Query("SELECT * FROM profiles WHERE name = :name")
suspend fun getByName(name: String): List<ServerProfile>

@Query("SELECT * FROM profiles WHERE name LIKE :query OR host LIKE :query OR sshHost LIKE :query")
fun search(query: String): LiveData<List<ServerProfile>>

@Insert
suspend fun insert(profile: ServerProfile): Long

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/gaurav/avnc/ui/home/ServerTabs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class ServerTabs(val activity: HomeActivity) {
/**
* Adapter for saved servers
*/
class SavedServerAdapter(val viewModel: HomeViewModel)
class SavedServerAdapter(val viewModel: HomeViewModel, val canEditServers: Boolean = true)
: ListAdapter<ServerProfile, SavedServerAdapter.ViewHolder>(Differ) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand All @@ -147,7 +147,7 @@ class ServerTabs(val activity: HomeActivity) {
}

inner class ViewHolder(val binding: ServerSavedItemBinding)
: ProfileViewHolder(viewModel, binding.root, R.menu.saved_server)
: ProfileViewHolder(viewModel, binding.root, if (canEditServers) R.menu.saved_server_editable else R.menu.saved_server)

object Differ : DiffUtil.ItemCallback<ServerProfile>() {
override fun areItemsTheSame(old: ServerProfile, new: ServerProfile) = (old.ID == new.ID)
Expand Down
22 changes: 21 additions & 1 deletion app/src/main/java/com/gaurav/avnc/ui/home/UrlBarActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,36 @@ package com.gaurav.avnc.ui.home
import android.os.Bundle
import android.view.Window
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.gaurav.avnc.R
import com.gaurav.avnc.databinding.ActivityUrlBinding
import com.gaurav.avnc.ui.vnc.startVncActivity
import com.gaurav.avnc.viewmodel.HomeViewModel
import com.gaurav.avnc.viewmodel.UrlBarViewModel
import com.gaurav.avnc.vnc.VncUri

/**
* Activity allowing user to directly connect to a server.
*
* Possible future improvements:
* - Keep history of recent entries
* - Show suggestions from saved profiles
* - Show suggestions from discovered servers.
*/
class UrlBarActivity : AppCompatActivity() {

private val viewModel by viewModels<UrlBarViewModel>()
private val homeViewModel by viewModels<HomeViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)

val binding = DataBindingUtil.setContentView<ActivityUrlBinding>(this, R.layout.activity_url)
binding.viewModel = viewModel
binding.lifecycleOwner = this

binding.url.setOnEditorActionListener { _, _, _ -> go(binding.url.text.toString()) }
binding.backBtn.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
Expand All @@ -43,6 +51,18 @@ class UrlBarActivity : AppCompatActivity() {
binding.url.setText("")
}

// TODO: Refactor server lists and remove dependency on HomeViewModel
val adapter = ServerTabs.SavedServerAdapter(homeViewModel, false)
binding.serversRv.layoutManager = LinearLayoutManager(this)
binding.serversRv.adapter = adapter
binding.serversRv.setHasFixedSize(true)
binding.serversRv.itemAnimator?.addDuration = 0 // Disable "flashing" of added items
viewModel.filteredServers.observe(this) { adapter.submitList(it) }
homeViewModel.newConnectionEvent.observe(this) {
startVncActivity(this, it)
finish()
}

binding.url.requestFocus()
}

Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/gaurav/avnc/viewmodel/UrlBarViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2023 Gaurav Ujjwal.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* See COPYING.txt for more details.
*/

package com.gaurav.avnc.viewmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.switchMap

class UrlBarViewModel(app: Application) : BaseViewModel(app) {

val query = MutableLiveData("")
val filteredServers = query.switchMap {
if (it.isNotBlank()) serverProfileDao.search("%$it%")
else MutableLiveData(listOf())
}
}
39 changes: 35 additions & 4 deletions app/src/main/res/layout/activity_url.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@
-->

<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">

<FrameLayout
<data>

<variable
name="viewModel"
type="com.gaurav.avnc.viewmodel.UrlBarViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:touchscreenBlocksFocus="false">
android:touchscreenBlocksFocus="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
style="@style/UrlBar"
Expand Down Expand Up @@ -44,6 +55,7 @@
android:imeOptions="actionGo|flagNoExtractUi"
android:importantForAutofill="no"
android:inputType="textUri"
android:text="@={viewModel.query}"
android:textColorHint="@android:color/transparent"
tools:ignore="LabelFor" />

Expand All @@ -57,8 +69,27 @@
</LinearLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/servers_rv"
style="@style/ServerList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:contentDescription="@string/desc_saved_servers_list"
android:paddingBottom="@dimen/padding_small"
app:isVisible="@{!viewModel.filteredServers.empty}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/appbar"
tools:listitem="@layout/server_saved_item" />

<TextView
style="@style/TipView"
android:text="@string/tip_urlbar" />
</FrameLayout>
android:text="@string/tip_urlbar"
app:isVisible="@{viewModel.filteredServers.empty}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/appbar"
app:layout_constraintVertical_bias=".2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
10 changes: 2 additions & 8 deletions app/src/main/res/menu/saved_server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/edit"
android:title="@string/title_edit" />
<item
android:id="@+id/duplicate"
android:title="@string/title_duplicate" />
android:id="@+id/copy_name"
android:title="@string/title_copy_name" />
<item
android:id="@+id/copy_host"
android:title="@string/title_copy_host" />
<item
android:id="@+id/delete"
android:title="@string/title_delete" />
</menu>
22 changes: 22 additions & 0 deletions app/src/main/res/menu/saved_server_editable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2020 Gaurav Ujjwal.
~
~ SPDX-License-Identifier: GPL-3.0-or-later
~
~ See COPYING.txt for more details.
-->

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/edit"
android:title="@string/title_edit" />
<item
android:id="@+id/duplicate"
android:title="@string/title_duplicate" />
<item
android:id="@+id/copy_host"
android:title="@string/title_copy_host" />
<item
android:id="@+id/delete"
android:title="@string/title_delete" />
</menu>

0 comments on commit c2ff8ae

Please sign in to comment.