Skip to content

Commit

Permalink
handle sync and async data
Browse files Browse the repository at this point in the history
  • Loading branch information
hantrungkien committed Jul 20, 2018
1 parent 98bcfb2 commit a8cfdfa
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 96 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">

</activity>
<activity android:name=".SyncActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AsyncActivity"/>
</application>

</manifest>
84 changes: 84 additions & 0 deletions app/src/main/java/com/kienht/bubble_picker/AsyncActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.kienht.bubble_picker

import android.content.res.TypedArray
import android.os.Bundle
import android.os.Handler
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.kienht.bubblepicker.BubblePickerListener
import com.kienht.bubblepicker.adapter.BubblePickerAdapter
import com.kienht.bubblepicker.model.BubbleGradient
import com.kienht.bubblepicker.model.PickerItem
import com.kienht.bubblepicker.rendering.BubblePicker

/**
* @author kienht
* @since 20/07/2018
*/
class AsyncActivity : AppCompatActivity(), BubblePickerListener {

lateinit var images: TypedArray
lateinit var colors: TypedArray

private var picker: BubblePicker? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.async_activity)

val titles = resources.getStringArray(R.array.countries)
colors = resources.obtainTypedArray(R.array.colors)
images = resources.obtainTypedArray(R.array.images)

Handler().postDelayed({
picker = BubblePicker(this, null)
picker!!.adapter = object : BubblePickerAdapter {
override val totalCount = titles.size

override fun getItem(position: Int): PickerItem {
return PickerItem().apply {
title = titles[position]
gradient = BubbleGradient(colors.getColor((position * 2) % 8, 0),
colors.getColor((position * 2) % 8 + 1, 0), BubbleGradient.VERTICAL)
}
}
}

picker!!.bubbleSize = 10
picker!!.listener = this@AsyncActivity

setContentView(picker)

}, 3000)
}

override fun onResume() {
super.onResume()
if (picker != null) {
picker!!.onResume()
}
}

override fun onPause() {
super.onPause()
if (picker != null) {
picker!!.onPause()
}
}

override fun onDestroy() {
super.onDestroy()
colors.resources
images.resources
}

override fun onBubbleSelected(item: PickerItem) {
toast("Selected: " + item.title!!)
}

override fun onBubbleDeselected(item: PickerItem) {
toast("Unselected: " + item.title!!)
}

private fun toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
64 changes: 10 additions & 54 deletions app/src/main/java/com/kienht/bubble_picker/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package com.kienht.bubble_picker

import android.content.res.TypedArray
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.support.v4.content.ContextCompat
import android.util.Log
import android.widget.Toast
import com.kienht.bubblepicker.BubblePickerListener
import com.kienht.bubblepicker.adapter.BubblePickerAdapter
import com.kienht.bubblepicker.model.BubbleGradient
import com.kienht.bubblepicker.model.PickerItem
import kotlinx.android.synthetic.main.activity_main.*

/**
Expand All @@ -19,56 +11,20 @@ import kotlinx.android.synthetic.main.activity_main.*
*/
class MainActivity : AppCompatActivity() {

lateinit var images: TypedArray
lateinit var colors: TypedArray

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val titles = resources.getStringArray(R.array.countries)
colors = resources.obtainTypedArray(R.array.colors)
images = resources.obtainTypedArray(R.array.images)

picker.adapter = object : BubblePickerAdapter {
override val totalCount = titles.size

override fun getItem(position: Int): PickerItem {
return PickerItem().apply {
title = titles[position]
gradient = BubbleGradient(colors.getColor((position * 2) % 8, 0),
colors.getColor((position * 2) % 8 + 1, 0), BubbleGradient.VERTICAL)
}
}
}

picker.bubbleSize = 10
picker.listener = object : BubblePickerListener {
override fun onBubbleDeselected(item: PickerItem) {
toast("Unselected: " + item.title!!)
}

override fun onBubbleSelected(item: PickerItem) {
toast("Selected: " + item.title!!)
}
}
}

override fun onResume() {
super.onResume()
picker.onResume()
}
setContentView(R.layout.activity_main)

override fun onPause() {
super.onPause()
picker.onPause()
}
button_sync_activity.setOnClickListener({
val intent = Intent(this@MainActivity, SyncActivity::class.java)
startActivity(intent)
})

override fun onDestroy() {
super.onDestroy()
colors.resources
images.resources
button_async_activity.setOnClickListener({
val intent = Intent(this@MainActivity, AsyncActivity::class.java)
startActivity(intent)
})
}

private fun toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
73 changes: 73 additions & 0 deletions app/src/main/java/com/kienht/bubble_picker/SyncActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.kienht.bubble_picker

import android.content.res.TypedArray
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.kienht.bubblepicker.BubblePickerListener
import com.kienht.bubblepicker.adapter.BubblePickerAdapter
import com.kienht.bubblepicker.model.BubbleGradient
import com.kienht.bubblepicker.model.PickerItem
import kotlinx.android.synthetic.main.sync_activity.*

/**
* @author kienht
* @since 20/07/2018
*/
class SyncActivity : AppCompatActivity() {

lateinit var images: TypedArray
lateinit var colors: TypedArray

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.sync_activity)

val titles = resources.getStringArray(R.array.countries)
colors = resources.obtainTypedArray(R.array.colors)
images = resources.obtainTypedArray(R.array.images)

picker.adapter = object : BubblePickerAdapter {
override val totalCount = titles.size

override fun getItem(position: Int): PickerItem {
return PickerItem().apply {
title = titles[position]
gradient = BubbleGradient(colors.getColor((position * 2) % 8, 0),
colors.getColor((position * 2) % 8 + 1, 0), BubbleGradient.VERTICAL)
imgUrl = "http://sohanews.sohacdn.com/2018/4/11/hat9-1523392964439195574255.jpg"
}
}
}

picker.bubbleSize = 10
picker.listener = object : BubblePickerListener {
override fun onBubbleDeselected(item: PickerItem) {
toast("Unselected: " + item.title!!)
}

override fun onBubbleSelected(item: PickerItem) {
toast("Selected: " + item.title!!)
}
}
}

override fun onResume() {
super.onResume()
picker.onResume()
}

override fun onPause() {
super.onPause()
picker.onPause()
}

override fun onDestroy() {
super.onDestroy()
colors.resources
images.resources
}

private fun toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show()

}
18 changes: 14 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:background="@android:color/white"

android:gravity="center"
android:orientation="vertical">

<com.kienht.bubblepicker.rendering.BubblePicker
android:id="@+id/picker"
<Button
android:id="@+id/button_sync_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:backgroundColor="@android:color/white" />
android:layout_height="wrap_content"
android:text="SYNC DATA ACTIVITY" />

<Button
android:id="@+id/button_async_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="ASYNC DATA ACTIVITY" />
</LinearLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/async_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?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:background="@android:color/white"
android:orientation="vertical">

</LinearLayout>
15 changes: 15 additions & 0 deletions app/src/main/res/layout/sync_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@android:color/white"
android:orientation="vertical">

<com.kienht.bubblepicker.rendering.BubblePicker
android:id="@+id/picker"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:backgroundColor="@android:color/white" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object Engine {

fun build(bodiesCount: Int, scaleX: Float, scaleY: Float): List<CircleBody> {
val density = interpolate(0.8f, 0.2f, radius / 100f)
for (i in 0..bodiesCount - 1) {
for (i in 0 until bodiesCount) {
val x = if (Random().nextBoolean()) -startX else startX
val y = if (Random().nextBoolean()) -0.5f / scaleY else 0.5f / scaleY
bodies.add(CircleBody(world, Vec2(x, y), bubbleRadius * scaleX, (bubbleRadius * scaleX) * 1.3f, density,
Expand Down
Loading

0 comments on commit a8cfdfa

Please sign in to comment.