diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 570584c..5f2d47a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,11 +9,11 @@ android { buildToolsVersion "29.0.2" defaultConfig { applicationId "fr.plnech.dunbar" - minSdkVersion 15 + minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { @@ -26,10 +26,10 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'com.android.support:appcompat-v7:28.0.0' - implementation 'com.android.support.constraint:constraint-layout:1.1.3' - implementation 'com.android.support:design:28.0.0' + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation 'com.google.android.material:material:1.0.0' testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + androidTestImplementation 'androidx.test:runner:1.2.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' } diff --git a/app/src/androidTest/java/fr/plnech/dunbar/ExampleInstrumentedTest.kt b/app/src/androidTest/java/fr/plnech/dunbar/ExampleInstrumentedTest.kt index 47e1f0b..276446b 100644 --- a/app/src/androidTest/java/fr/plnech/dunbar/ExampleInstrumentedTest.kt +++ b/app/src/androidTest/java/fr/plnech/dunbar/ExampleInstrumentedTest.kt @@ -1,7 +1,7 @@ package fr.plnech.dunbar -import android.support.test.InstrumentationRegistry -import android.support.test.runner.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.Test import org.junit.runner.RunWith diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a661b03..7392302 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,8 +2,12 @@ + + + + - \ No newline at end of file diff --git a/app/src/main/java/fr/plnech/dunbar/Extensions.kt b/app/src/main/java/fr/plnech/dunbar/Extensions.kt new file mode 100644 index 0000000..6e5731b --- /dev/null +++ b/app/src/main/java/fr/plnech/dunbar/Extensions.kt @@ -0,0 +1,8 @@ +package fr.plnech.dunbar + +fun String.plural(count: Int = 1): String { + return when { + count > 1 -> this + "s" + else -> this + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/plnech/dunbar/Friends.kt b/app/src/main/java/fr/plnech/dunbar/Friends.kt index b75046c..c9b26b0 100644 --- a/app/src/main/java/fr/plnech/dunbar/Friends.kt +++ b/app/src/main/java/fr/plnech/dunbar/Friends.kt @@ -1,7 +1,5 @@ package fr.plnech.dunbar +// * @see [contacts doc](https://developer.android.com/training/contacts-provider/retrieve-names) class Friends { - fun loadFriends() { - - } } \ No newline at end of file diff --git a/app/src/main/java/fr/plnech/dunbar/MainActivity.kt b/app/src/main/java/fr/plnech/dunbar/MainActivity.kt index f41dbe5..483ffc0 100644 --- a/app/src/main/java/fr/plnech/dunbar/MainActivity.kt +++ b/app/src/main/java/fr/plnech/dunbar/MainActivity.kt @@ -1,24 +1,35 @@ package fr.plnech.dunbar import android.os.Bundle -import android.support.design.widget.Snackbar -import android.support.v7.app.AppCompatActivity +import android.provider.ContactsContract 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.Snackbar import kotlinx.android.synthetic.main.activity_main.* +import kotlinx.android.synthetic.main.content_main.* + class MainActivity : AppCompatActivity() { + private val CONTENT_VIEW_ID = 10101010 + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) + + + fab.setOnClickListener { view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) - .setAction("Action", null).show() + .setAction("Action", null).show() } + + fetchFriends() } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -36,4 +47,40 @@ class MainActivity : AppCompatActivity() { else -> super.onOptionsItemSelected(item) } } + + fun fetchFriends() { + contentResolver.query( + ContactsContract.CommonDataKinds.Phone.CONTENT_URI, + null, + null, + null, + null + )?.let { + + val contacts = mutableListOf>>() + while (it.moveToNext()) { + contacts.add(mutableListOf>().also { contact -> + it.columnNames.forEach { name -> + contact.add(Pair(name, it.getString(it.getColumnIndex(name)))) + } + }) + } + + friends.adapter = + ArrayAdapter(this, R.layout.contact, R.id.name, + contacts.map { contact -> + contact.filter { !it.second.isNullOrBlank() } + .joinToString { "${it.first}:${it.second}" } + }) + + val nbFriends = friends.adapter.count + welcomeTitle.text = "$nbFriends ${"friend".plural(nbFriends)} on Dunbar" + + Toast.makeText(this@MainActivity, "$nbFriends ", Toast.LENGTH_LONG).show() + + it?.close() + } + + + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index eed4d89..e9fa907 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,28 +1,28 @@ - - - - + - - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/layout/contact.xml b/app/src/main/res/layout/contact.xml new file mode 100644 index 0000000..e423727 --- /dev/null +++ b/app/src/main/res/layout/contact.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/contacts.xml b/app/src/main/res/layout/contacts.xml new file mode 100644 index 0000000..cd4b749 --- /dev/null +++ b/app/src/main/res/layout/contacts.xml @@ -0,0 +1,7 @@ + + diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 3fc2170..946822d 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -1,5 +1,5 @@ - - \ No newline at end of file + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index eccd7a6..e3245e7 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.5.0' + classpath 'com.android.tools.build:gradle:3.5.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle.properties b/gradle.properties index 85be9ea..3d8ce0c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,3 +13,5 @@ org.gradle.jvmargs=-Xmx1536m # org.gradle.parallel=true # Kotlin code style for this project: "official" or "obsolete": kotlin.code.style=official +android.useAndroidX=true +android.enableJetifier=true