Show connections in actionbar.

This commit is contained in:
Felix Ableitner 2015-09-22 01:33:58 +02:00
parent 87632b6225
commit e66bccc044
10 changed files with 181 additions and 30 deletions

View file

@ -12,6 +12,7 @@ buildscript {
dependencies {
compile "com.android.support:appcompat-v7:23.0.0"
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:multidex:1.0.1'
androidTestCompile "com.android.support:multidex-instrumentation:1.0.1",
{ exclude module: "multidex" }

View file

@ -30,7 +30,8 @@
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop" />
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name=".activities.AddContactsActivity"

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/primary"
android:layout_width="fill_parent"

View file

@ -1,12 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10"
android:orientation="vertical"
android:background="@color/chat_background">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_height="0dp"
android:layout_width="match_parent"

View file

@ -1,20 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_contacts_found"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp">
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/title_holder"
android:orientation="vertical"
android:clickable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingStart="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/subtitle"
android:textSize="16sp"
android:singleLine="true"
android:ellipsize="end" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_contacts_found"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>

View file

@ -14,4 +14,8 @@
<!-- Background color for ChatFragment -->
<color name="chat_background">#f3f2fb</color>
<color name="title_connections_error">#F44336</color>
<color name="title_connections_warning">#FFEB3B</color>
<color name="title_connections_ok">#8BC34A</color>
</resources>

View file

@ -27,6 +27,12 @@
<!-- ContactsFragment -->
<!-- Number of connections shown in actionbar -->
<plurals name="title_connections">
<item quantity="one">1 Connection</item>
<item quantity="other">%s Connections</item>
</plurals>
<!-- Empty text for contacts list -->
<string name="no_contacts_found">You haven\'t added any contacts yet</string>

View file

@ -9,6 +9,11 @@
<item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AlertDialogTheme" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>

View file

@ -4,6 +4,7 @@ import android.app.ListFragment
import android.content.{BroadcastReceiver, Context, Intent, IntentFilter}
import android.os.Bundle
import android.support.v4.content.LocalBroadcastManager
import android.support.v7.widget.Toolbar
import android.view.View.OnClickListener
import android.view.inputmethod.EditorInfo
import android.view.{KeyEvent, LayoutInflater, View, ViewGroup}
@ -31,6 +32,8 @@ class ChatFragment extends ListFragment with OnClickListener {
private lazy val database = new Database(getActivity)
private lazy val activity = getActivity.asInstanceOf[EnsichatActivity]
private var address: Address = _
private var chatService: ChatService = _
@ -46,7 +49,6 @@ class ChatFragment extends ListFragment with OnClickListener {
override def onActivityCreated(savedInstanceState: Bundle): Unit = {
super.onActivityCreated(savedInstanceState)
val activity = getActivity.asInstanceOf[EnsichatActivity]
activity.runOnServiceConnected(() => {
chatService = activity.service
@ -62,8 +64,11 @@ class ChatFragment extends ListFragment with OnClickListener {
}
override def onCreateView(inflater: LayoutInflater, container: ViewGroup,
savedInstanceState: Bundle): View = {
val view: View = inflater.inflate(R.layout.fragment_chat, container, false)
savedInstanceState: Bundle): View = {
val view = inflater.inflate(R.layout.fragment_chat, container, false)
val toolbar = view.findViewById(R.id.toolbar).asInstanceOf[Toolbar]
activity.setSupportActionBar(toolbar)
activity.getSupportActionBar.setDisplayHomeAsUpEnabled(true)
sendButton = view.findViewById(R.id.send).asInstanceOf[Button]
sendButton.setOnClickListener(this)
messageText = view.findViewById(R.id.message).asInstanceOf[EditText]

View file

@ -2,57 +2,85 @@ package com.nutomic.ensichat.fragments
import java.io.File
import android.app.ListFragment
import android.content.{IntentFilter, Context, BroadcastReceiver, Intent}
import android.app.{ActionBar, ListFragment}
import android.content.{BroadcastReceiver, Context, Intent, IntentFilter}
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.preference.PreferenceManager
import android.support.v4.content.LocalBroadcastManager
import android.support.v4.content.{ContextCompat, LocalBroadcastManager}
import android.support.v7.widget.Toolbar
import android.view.View.OnClickListener
import android.view._
import android.widget.ListView
import android.widget.{ListView, TextView, Toast}
import com.nutomic.ensichat.R
import com.nutomic.ensichat.activities.{AddContactsActivity, EnsichatActivity, MainActivity, SettingsActivity}
import com.nutomic.ensichat.protocol.{Crypto, ChatService}
import com.nutomic.ensichat.protocol.{ChatService, Crypto}
import com.nutomic.ensichat.util.Database
import com.nutomic.ensichat.views.UsersAdapter
import scala.collection.JavaConversions._
/**
* Lists all nearby, connected devices.
*/
class ContactsFragment extends ListFragment {
class ContactsFragment extends ListFragment with OnClickListener {
private lazy val adapter = new UsersAdapter(getActivity)
private lazy val database = new Database(getActivity)
private lazy val lbm = LocalBroadcastManager.getInstance(getActivity)
private lazy val activity = getActivity.asInstanceOf[EnsichatActivity]
private var title: TextView = _
private var subtitle: TextView = _
override def onCreate(savedInstanceState: Bundle): Unit = {
super.onCreate(savedInstanceState)
setListAdapter(adapter)
setHasOptionsMenu(true)
lbm.registerReceiver(onContactsUpdatedListener, new IntentFilter(Database.ActionContactsUpdated))
lbm.registerReceiver(onConnectionsChangedListener, new IntentFilter(ChatService.ActionConnectionsChanged))
}
getActivity.asInstanceOf[EnsichatActivity].runOnServiceConnected(() => {
override def onResume(): Unit = {
super.onResume()
activity.runOnServiceConnected(() => {
adapter.clear()
database.getContacts.foreach(adapter.add)
updateConnections()
})
LocalBroadcastManager.getInstance(getActivity)
.registerReceiver(onContactsUpdatedListener, new IntentFilter(Database.ActionContactsUpdated))
}
override def onDestroy(): Unit = {
super.onDestroy()
LocalBroadcastManager.getInstance(getActivity).unregisterReceiver(onContactsUpdatedListener)
lbm.unregisterReceiver(onContactsUpdatedListener)
lbm.unregisterReceiver(onConnectionsChangedListener)
}
override def onCreateView(inflater: LayoutInflater, container: ViewGroup,
savedInstanceState: Bundle): View =
inflater.inflate(R.layout.fragment_contacts, container, false)
savedInstanceState: Bundle): View = {
val v = inflater.inflate(R.layout.fragment_contacts, container, false)
val toolbar = v.findViewById(R.id.toolbar).asInstanceOf[Toolbar]
v.findViewById(R.id.title_holder).setOnClickListener(this)
activity.setSupportActionBar(toolbar)
toolbar.setNavigationIcon(R.drawable.ic_launcher)
title = v.findViewById(R.id.title).asInstanceOf[TextView]
subtitle = v.findViewById(R.id.subtitle).asInstanceOf[TextView]
v
}
override def onCreateOptionsMenu(menu: Menu, inflater: MenuInflater): Unit = {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.main, menu)
}
override def onClick(v: View): Unit =
startActivity(new Intent(getActivity, classOf[AddContactsActivity]))
override def onOptionsItemSelected(item: MenuItem): Boolean = item.getItemId match {
case R.id.add_contact =>
startActivity(new Intent(getActivity, classOf[AddContactsActivity]))
@ -105,4 +133,31 @@ class ContactsFragment extends ListFragment {
}
}
private val onConnectionsChangedListener = new BroadcastReceiver {
override def onReceive(context: Context, intent: Intent): Unit = updateConnections()
}
/**
* Updates TextViews in actionbar with current connections.
*/
private def updateConnections(): Unit = {
val service = activity.service
if (service == null || title == null)
return
val connections = service.connections()
val count = connections.size
val color = count match {
case 0 => R.color.title_connections_error
case 1 => R.color.title_connections_warning
case _ => R.color.title_connections_ok
}
title.setText(getResources.getQuantityString(R.plurals.title_connections, count, count.toString))
title.setTextColor(ContextCompat.getColor(getActivity, color))
subtitle.setText(connections.map(service.getUser(_).name).mkString(", "))
subtitle.setVisibility(if (count == 0) View.GONE else View.VISIBLE)
}
}