Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
Dunbar
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PLN
Dunbar
Commits
0543837d
Unverified
Commit
0543837d
authored
Nov 02, 2019
by
PLN (Algolia)
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(friends): Interactions, taps, reload
parent
2e335df3
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
102 additions
and
40 deletions
+102
-40
Friend.kt
app/src/main/java/fr/plnech/dunbar/Friend.kt
+7
-2
FriendsAdapter.kt
app/src/main/java/fr/plnech/dunbar/FriendsAdapter.kt
+48
-0
MainActivity.kt
app/src/main/java/fr/plnech/dunbar/MainActivity.kt
+33
-26
contact.xml
app/src/main/res/layout/contact.xml
+8
-8
content_main.xml
app/src/main/res/layout/content_main.xml
+6
-4
No files found.
app/src/main/java/fr/plnech/dunbar/Friend.kt
View file @
0543837d
...
...
@@ -2,18 +2,23 @@ package fr.plnech.dunbar
import
android.graphics.Bitmap
import
android.provider.ContactsContract.Contacts
import
java.util.*
data class
Friend
(
val
map
:
MutableMap
<
String
,
String
?>,
val
photo
:
Bitmap
?)
{
override
fun
toString
():
String
=
"$name"
fun
mapString
():
String
=
map
.
entries
.
filter
{
!
it
.
value
.
isNullOrEmpty
()
}.
toString
()
val
name
:
String
?
get
()
=
map
[
Contacts
.
DISPLAY_NAME
]
val
id
:
Int
get
()
=
map
[
Contacts
.
_ID
]
!!
.
toInt
()
val
lastTime
:
String
?
get
()
=
map
[
Contacts
.
LAST_TIME_CONTACTED
]
val
lastTimeStamp
=
map
[
Contacts
.
LAST_TIME_CONTACTED
]
!!
.
toLong
()
val
lastDate
:
Date
?
get
()
=
if
(
lastTimeStamp
>
0
)
Date
(
lastTimeStamp
)
else
null
val
timesContacted
:
Int
get
()
=
map
[
Contacts
.
TIMES_CONTACTED
]
?.
toInt
()
?:
0
...
...
app/src/main/java/fr/plnech/dunbar/FriendsAdapter.kt
0 → 100644
View file @
0543837d
package
fr.plnech.dunbar
import
android.text.format.DateUtils
import
android.view.LayoutInflater
import
android.view.View
import
android.view.ViewGroup
import
android.widget.Toast
import
androidx.recyclerview.widget.RecyclerView
import
kotlinx.android.synthetic.main.contact.view.*
class
FriendsAdapter
(
private
var
friends
:
List
<
Friend
>
=
mutableListOf
()
)
:
RecyclerView
.
Adapter
<
FriendsViewHolder
>()
{
override
fun
onCreateViewHolder
(
parent
:
ViewGroup
,
viewType
:
Int
):
FriendsViewHolder
=
FriendsViewHolder
(
LayoutInflater
.
from
(
parent
.
context
).
inflate
(
viewType
,
parent
,
false
))
override
fun
getItemCount
():
Int
=
friends
.
size
override
fun
getItemViewType
(
position
:
Int
):
Int
=
R
.
layout
.
contact
override
fun
onBindViewHolder
(
holder
:
FriendsViewHolder
,
position
:
Int
):
Unit
=
holder
.
bind
(
friends
[
position
])
}
class
FriendsViewHolder
(
private
val
view
:
View
)
:
RecyclerView
.
ViewHolder
(
view
)
{
private
val
name
=
view
.
contactName
private
val
data
=
view
.
contactData
private
val
pic
=
view
.
pic
fun
bind
(
friend
:
Friend
)
{
name
.
text
=
friend
.
name
data
.
text
=
buildString
{
if
(
friend
.
lastDate
!=
null
)
{
append
(
friend
.
timesContacted
)
append
(
" interactions, last "
)
append
(
DateUtils
.
getRelativeTimeSpanString
(
friend
.
lastTimeStamp
))
}
else
{
append
(
"Never interacted"
)
}
}
view
.
setOnClickListener
{
Toast
.
makeText
(
view
.
context
,
friend
.
mapString
(),
Toast
.
LENGTH_LONG
).
show
()
}
}
}
app/src/main/java/fr/plnech/dunbar/MainActivity.kt
View file @
0543837d
...
...
@@ -8,10 +8,9 @@ import android.provider.ContactsContract
import
android.util.Log
import
android.view.Menu
import
android.view.MenuItem
import
android.widget.ArrayAdapter
import
android.widget.Toast
import
androidx.appcompat.app.AppCompatActivity
import
com.google.android.material.snackbar.Snackba
r
import
androidx.recyclerview.widget.LinearLayoutManage
r
import
kotlinx.android.synthetic.main.activity_main.*
import
kotlinx.android.synthetic.main.content_main.*
import
java.io.IOException
...
...
@@ -27,11 +26,10 @@ class MainActivity : AppCompatActivity() {
setSupportActionBar
(
toolbar
)
fab
.
setOnClickListener
{
view
->
Snackbar
.
make
(
view
,
"Replace with your own action"
,
Snackbar
.
LENGTH_LONG
)
.
setAction
(
"Action"
,
null
).
show
()
reloadFriends
()
}
fetch
Friends
()
reload
Friends
()
}
override
fun
onCreateOptionsMenu
(
menu
:
Menu
):
Boolean
{
...
...
@@ -50,8 +48,15 @@ class MainActivity : AppCompatActivity() {
}
}
fun
fetchFriends
()
{
Toast
.
makeText
(
this
@MainActivity
,
"Fetching friends..."
,
Toast
.
LENGTH_SHORT
).
show
()
private
fun
reloadFriends
()
{
fetchFriends
()
displayFriends
()
Toast
.
makeText
(
this
,
"Friends reloaded."
,
Toast
.
LENGTH_SHORT
).
show
()
}
private
fun
fetchFriends
()
{
val
newFriends
=
mutableListOf
<
Friend
>()
contentResolver
.
query
(
ContactsContract
.
CommonDataKinds
.
Phone
.
CONTENT_URI
,
...
...
@@ -62,41 +67,43 @@ class MainActivity : AppCompatActivity() {
)
?.
let
{
while
(
it
.
moveToNext
())
{
mutableMapOf
<
String
,
String
?>().
also
{
map
->
val
indexID
=
it
.
getColumnIndex
(
ContactsContract
.
Contacts
.
_ID
)
val
id
=
it
.
getLong
(
indexID
)
val
indexId
=
it
.
getColumnIndex
(
ContactsContract
.
Contacts
.
_ID
)
val
id
=
it
.
getLong
(
indexId
)
val
photo
:
Bitmap
?
=
getPhoto
(
id
)
it
.
columnNames
.
forEach
{
name
->
val
index
=
it
.
getColumnIndex
(
name
)
map
[
name
]
=
it
.
getString
(
index
)
}
f
riends
.
add
(
Friend
(
map
,
photo
))
newF
riends
.
add
(
Friend
(
map
,
photo
))
}
}
it
.
close
()
val
adapter
=
ArrayAdapter
<
Friend
>(
this
,
R
.
layout
.
contact
,
R
.
id
.
name
,
friends
)
friends
.
clear
()
friends
.
addAll
(
newFriends
)
}
friendsList
.
adapter
=
adapter
friendsList
.
setOnItemClickListener
{
parent
,
view
,
position
,
id
->
val
contact
:
String
=
adapter
.
getItem
(
position
)
?.
toString
()
?:
"NONE"
friends
.
sortByDescending
{
it
.
timesContacted
}
}
private
fun
displayFriends
()
{
val
adapter
=
FriendsAdapter
(
friends
)
friendsList
.
layoutManager
=
LinearLayoutManager
(
this
)
friendsList
.
setHasFixedSize
(
true
)
friendsList
.
adapter
=
adapter
// friendsList.setOnItemClickListener { parent, view, position, id ->
// val contact: String = adapter.getItem(position)?.toString() ?: "NONE"
//
// Snackbar.make(friendsList, contact, Snackbar.LENGTH_INDEFINITE).show()
Toast
.
makeText
(
this
,
contact
,
Toast
.
LENGTH_LONG
).
show
()
}
//
Toast.makeText(this, contact, Toast.LENGTH_LONG).show()
//
}
val
nbFriends
=
adapter
.
c
ount
val
nbFriends
=
adapter
.
itemC
ount
welcomeTitle
.
text
=
"$nbFriends ${"
friend
".plural(nbFriends)} on Dunbar"
Toast
.
makeText
(
this
@MainActivity
,
"$nbFriends "
,
Toast
.
LENGTH_LONG
).
show
()
it
.
close
()
}
}
private
fun
getPhoto
(
id
:
Long
):
Bitmap
?
{
...
...
app/src/main/res/layout/contact.xml
View file @
0543837d
...
...
@@ -4,7 +4,7 @@
xmlns:app=
"http://schemas.android.com/apk/res-auto"
xmlns:tools=
"http://schemas.android.com/tools"
android:layout_width=
"match_parent"
android:layout_height=
"
match_par
ent"
android:layout_height=
"
wrap_cont
ent"
android:elevation=
"5dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
...
...
@@ -24,12 +24,12 @@
android:elevation=
"2dp"
android:src=
"@drawable/user"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintRight_toLeftOf=
"@id/
n
ame"
app:layout_constraintRight_toLeftOf=
"@id/
contactN
ame"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
/>
<TextView
android:id=
"@+id/
n
ame"
android:id=
"@+id/
contactN
ame"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:focusable=
"false"
...
...
@@ -38,15 +38,15 @@
android:gravity=
"end"
android:maxLines=
"2"
android:textAppearance=
"@style/TextAppearance.AppCompat.Title"
app:layout_constraintBottom_toTopOf=
"@id/
metad
ata"
app:layout_constraintBottom_toTopOf=
"@id/
contactD
ata"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintLeft_toRightOf=
"@id/pic"
app:layout_constraintRight_toLeftOf=
"@id/
n
ame"
app:layout_constraintRight_toLeftOf=
"@id/
contactN
ame"
app:layout_constraintTop_toTopOf=
"parent"
tools:text=
"@tools:sample/full_names"
/>
<TextView
android:id=
"@+id/
metad
ata"
android:id=
"@+id/
contactD
ata"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_marginBottom=
"15dp"
...
...
@@ -58,8 +58,8 @@
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintLeft_toRightOf=
"@id/pic"
app:layout_constraintRight_toLeftOf=
"@id/
n
ame"
app:layout_constraintTop_toBottomOf=
"@id/
n
ame"
app:layout_constraintRight_toLeftOf=
"@id/
contactN
ame"
app:layout_constraintTop_toBottomOf=
"@id/
contactN
ame"
tools:text=
"@tools:sample/full_names"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
...
...
app/src/main/res/layout/content_main.xml
View file @
0543837d
...
...
@@ -10,17 +10,19 @@
<TextView
android:id=
"@+id/welcomeTitle"
android:layout_marginTop=
"50dp"
android:layout_marginTop=
"10dp"
android:layout_marginBottom=
"10dp"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
tools:text=
"N friends on Dunbar"
android:textSize=
"24sp"
app:layout_constraintBottom_toTopOf=
"@id/friendsList"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
/>
app:layout_constraintTop_toTopOf=
"parent"
app:layout_constraintVertical_bias=
"0.0"
tools:text=
"N friends on Dunbar"
/>
<
List
View
<
androidx.recyclerview.widget.Recycler
View
android:id=
"@+id/friendsList"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment