From a8cfdfac07ccd622d80a7ae4e3e06e4bb5580f78 Mon Sep 17 00:00:00 2001 From: KienHT Date: Fri, 20 Jul 2018 15:32:11 +0700 Subject: [PATCH] handle sync and async data --- app/src/main/AndroidManifest.xml | 4 + .../com/kienht/bubble_picker/AsyncActivity.kt | 84 +++++++++++++++++++ .../com/kienht/bubble_picker/MainActivity.kt | 64 +++----------- .../com/kienht/bubble_picker/SyncActivity.kt | 73 ++++++++++++++++ app/src/main/res/layout/activity_main.xml | 18 +++- app/src/main/res/layout/async_activity.xml | 9 ++ app/src/main/res/layout/sync_activity.xml | 15 ++++ .../com/kienht/bubblepicker/physics/Engine.kt | 2 +- .../bubblepicker/rendering/BubblePicker.kt | 60 ++++++------- .../bubblepicker/rendering/PickerRenderer.kt | 15 ++-- 10 files changed, 248 insertions(+), 96 deletions(-) create mode 100644 app/src/main/java/com/kienht/bubble_picker/AsyncActivity.kt create mode 100644 app/src/main/java/com/kienht/bubble_picker/SyncActivity.kt create mode 100644 app/src/main/res/layout/async_activity.xml create mode 100644 app/src/main/res/layout/sync_activity.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c9be394..0ff45b3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,12 +11,16 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> + + + + \ No newline at end of file diff --git a/app/src/main/java/com/kienht/bubble_picker/AsyncActivity.kt b/app/src/main/java/com/kienht/bubble_picker/AsyncActivity.kt new file mode 100644 index 0000000..4ce80b6 --- /dev/null +++ b/app/src/main/java/com/kienht/bubble_picker/AsyncActivity.kt @@ -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() +} \ No newline at end of file diff --git a/app/src/main/java/com/kienht/bubble_picker/MainActivity.kt b/app/src/main/java/com/kienht/bubble_picker/MainActivity.kt index 6e7d29e..215f903 100644 --- a/app/src/main/java/com/kienht/bubble_picker/MainActivity.kt +++ b/app/src/main/java/com/kienht/bubble_picker/MainActivity.kt @@ -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.* /** @@ -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() } diff --git a/app/src/main/java/com/kienht/bubble_picker/SyncActivity.kt b/app/src/main/java/com/kienht/bubble_picker/SyncActivity.kt new file mode 100644 index 0000000..0a99ea3 --- /dev/null +++ b/app/src/main/java/com/kienht/bubble_picker/SyncActivity.kt @@ -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() + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index fbc81ef..0d2b5ef 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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"> - + android:layout_height="wrap_content" + android:text="SYNC DATA ACTIVITY" /> +