From 4e2b75d94525486fab8fc4a73f9e47af4e2065bd Mon Sep 17 00:00:00 2001 From: Mori Atsushi Date: Wed, 23 May 2018 19:52:36 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[add]=20custom=20adapter=E3=82=92=E4=BD=9C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syncpod/model/PublishingSettingItem.kt | 6 +++ .../view/activity/CreateRoomActivity.kt | 39 ++++++++++++++++--- .../viewmodel/CreateRoomActivityViewModel.kt | 25 ++++++------ 3 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 app/src/main/kotlin/com/cyder/android/syncpod/model/PublishingSettingItem.kt diff --git a/app/src/main/kotlin/com/cyder/android/syncpod/model/PublishingSettingItem.kt b/app/src/main/kotlin/com/cyder/android/syncpod/model/PublishingSettingItem.kt new file mode 100644 index 00000000..ed0706ce --- /dev/null +++ b/app/src/main/kotlin/com/cyder/android/syncpod/model/PublishingSettingItem.kt @@ -0,0 +1,6 @@ +package com.cyder.android.syncpod.model + +data class PublishingSettingItem( + val title: String, + val description: String +) diff --git a/app/src/main/kotlin/com/cyder/android/syncpod/view/activity/CreateRoomActivity.kt b/app/src/main/kotlin/com/cyder/android/syncpod/view/activity/CreateRoomActivity.kt index 69674746..5256801b 100644 --- a/app/src/main/kotlin/com/cyder/android/syncpod/view/activity/CreateRoomActivity.kt +++ b/app/src/main/kotlin/com/cyder/android/syncpod/view/activity/CreateRoomActivity.kt @@ -4,11 +4,15 @@ import android.content.Context import android.content.Intent import android.databinding.DataBindingUtil import android.os.Bundle -import android.widget.SimpleAdapter +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter import com.cyder.android.syncpod.R import com.cyder.android.syncpod.databinding.ActivityCreateRoomBinding +import com.cyder.android.syncpod.model.PublishingSettingItem import com.cyder.android.syncpod.view.helper.setUpSnackbar import com.cyder.android.syncpod.viewmodel.CreateRoomActivityViewModel +import kotlinx.android.synthetic.main.spinner_item_with_description.view.* import javax.inject.Inject /** @@ -16,7 +20,8 @@ import javax.inject.Inject */ class CreateRoomActivity : BaseActivity() { - @Inject lateinit var viewModel: CreateRoomActivityViewModel + @Inject + lateinit var viewModel: CreateRoomActivityViewModel private lateinit var binding: ActivityCreateRoomBinding override fun onCreate(savedInstanceState: Bundle?) { @@ -33,15 +38,37 @@ class CreateRoomActivity : BaseActivity() { } private fun setUpSpinner() { - val adapter = SimpleAdapter( + val adapter = PublishingSettingSpinnerAdapter( this, - viewModel.publishingSettingItems, R.layout.spinner_item_with_description, - viewModel.publishingSettingKeys, - intArrayOf(R.id.title, R.id.description)) + viewModel.publishingSettingItems) binding.publishingSetting.adapter = adapter } + inner class PublishingSettingSpinnerAdapter( + context: Context, + private val resourceId: Int, + val list: List + ) : ArrayAdapter(context, resourceId, list) { + override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View { + return getCustomView(position, convertView, parent) + } + + override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { + return getCustomView(position, convertView, parent) + } + + private fun getCustomView(position: Int, convertView: View?, parent: ViewGroup?): View { + val item = getItem(position) + val view = convertView ?: layoutInflater.inflate(resourceId, parent, false) + + view.title.text = item.title + view.description.text = item.description + return view + } + } + + companion object { fun createIntent(context: Context): Intent = Intent(context, CreateRoomActivity::class.java) } diff --git a/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt b/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt index 6a8984f2..993a67e6 100644 --- a/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt +++ b/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt @@ -4,6 +4,7 @@ import android.content.res.Resources import android.databinding.ObservableField import android.databinding.ObservableInt import com.cyder.android.syncpod.R +import com.cyder.android.syncpod.model.PublishingSettingItem import com.cyder.android.syncpod.repository.RoomRepository import com.cyder.android.syncpod.util.NotFilledFormsException import com.cyder.android.syncpod.view.helper.Navigator @@ -21,19 +22,19 @@ class CreateRoomActivityViewModel @Inject constructor( var roomDescription: ObservableField = ObservableField() var publishingSetting = ObservableInt() lateinit var resources: Resources - val publishingSettingItems: MutableList> by lazy { - val publicRoom = hashMapOf( - ID to PUBLIC_ROOM, - TITLE to resources.getString(R.string.public_room), - DESCRIPTION to resources.getString(R.string.public_room_description)) - val privateRoom = hashMapOf( - ID to PRIVATE_ROOM, - TITLE to resources.getString(R.string.private_room), - DESCRIPTION to resources.getString(R.string.private_room_description)) - mutableListOf(publicRoom, privateRoom) + val publishingSettingItems by lazy { + listOf( + PublishingSettingItem( + resources.getString(R.string.public_room), + resources.getString(R.string.public_room_description) + ), + PublishingSettingItem( + resources.getString(R.string.private_room), + resources.getString(R.string.private_room_description) + ) + ) } - val publishingSettingKeys = arrayOf(TITLE, DESCRIPTION) var callback: SnackbarCallback? = null override fun onStart() { @@ -54,7 +55,7 @@ class CreateRoomActivityViewModel @Inject constructor( fun onBackButtonClicked() = navigator.closeActivity() fun onSubmit() { - val isPublic = publishingSettingItems[publishingSetting.get()][ID] == PUBLIC_ROOM + val isPublic = publishingSetting.get() == 0 repository.createNewRoom(roomName.get() ?: "", roomDescription.get() ?: "", isPublic) .subscribe({ response -> From f9423479f734c8fae5d1fd7b4c41b23e4163682f Mon Sep 17 00:00:00 2001 From: Mori Atsushi Date: Wed, 23 May 2018 20:05:44 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[clear]=20=E3=83=AA=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=AF=E3=82=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syncpod/model/PublishingSettingItem.kt | 8 +++++++- .../syncpod/repository/RoomDataRepository.kt | 17 +++++++++++++++++ .../syncpod/repository/RoomRepository.kt | 3 +++ .../viewmodel/CreateRoomActivityViewModel.kt | 14 +++----------- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/cyder/android/syncpod/model/PublishingSettingItem.kt b/app/src/main/kotlin/com/cyder/android/syncpod/model/PublishingSettingItem.kt index ed0706ce..cec21d93 100644 --- a/app/src/main/kotlin/com/cyder/android/syncpod/model/PublishingSettingItem.kt +++ b/app/src/main/kotlin/com/cyder/android/syncpod/model/PublishingSettingItem.kt @@ -1,6 +1,12 @@ package com.cyder.android.syncpod.model data class PublishingSettingItem( + val id: Id, val title: String, val description: String -) +) { + enum class Id { + PUBLIC, + PRIVATE + } +} diff --git a/app/src/main/kotlin/com/cyder/android/syncpod/repository/RoomDataRepository.kt b/app/src/main/kotlin/com/cyder/android/syncpod/repository/RoomDataRepository.kt index c70b1240..4833a0c4 100644 --- a/app/src/main/kotlin/com/cyder/android/syncpod/repository/RoomDataRepository.kt +++ b/app/src/main/kotlin/com/cyder/android/syncpod/repository/RoomDataRepository.kt @@ -1,8 +1,11 @@ package com.cyder.android.syncpod.repository +import android.content.res.Resources +import com.cyder.android.syncpod.R import com.cyder.android.syncpod.api.SyncPodApi import com.cyder.android.syncpod.api.mapper.toModel import com.cyder.android.syncpod.api.request.CreateRoom +import com.cyder.android.syncpod.model.PublishingSettingItem import com.cyder.android.syncpod.model.Room import com.cyder.android.syncpod.model.User import com.cyder.android.syncpod.util.NotFilledFormsException @@ -24,6 +27,20 @@ class RoomDataRepository @Inject constructor( ) : RoomRepository { override val isEntered = syncPodWsApi.isEntered + override fun getPublishingSettingItems(resources: Resources): List { + return listOf( + PublishingSettingItem( + PublishingSettingItem.Id.PUBLIC, + resources.getString(R.string.public_room), + resources.getString(R.string.public_room_description) + ), + PublishingSettingItem( + PublishingSettingItem.Id.PRIVATE, + resources.getString(R.string.private_room), + resources.getString(R.string.private_room_description) + ) + ) + } override fun joinRoom(roomKey: String): Completable { return syncPodWsApi.enterRoom(roomKey) diff --git a/app/src/main/kotlin/com/cyder/android/syncpod/repository/RoomRepository.kt b/app/src/main/kotlin/com/cyder/android/syncpod/repository/RoomRepository.kt index 16e77975..711b1809 100644 --- a/app/src/main/kotlin/com/cyder/android/syncpod/repository/RoomRepository.kt +++ b/app/src/main/kotlin/com/cyder/android/syncpod/repository/RoomRepository.kt @@ -1,5 +1,7 @@ package com.cyder.android.syncpod.repository +import android.content.res.Resources +import com.cyder.android.syncpod.model.PublishingSettingItem import com.cyder.android.syncpod.model.Room import com.cyder.android.syncpod.model.User import io.reactivex.Completable @@ -11,6 +13,7 @@ import io.reactivex.Single */ interface RoomRepository { val isEntered: Flowable + fun getPublishingSettingItems(resources: Resources): List fun createNewRoom(name: String, description: String, isPublic: Boolean): Single fun fetchJoinedRooms(): Single> fun fetchPopularRooms(): Single> diff --git a/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt b/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt index 993a67e6..12d2927d 100644 --- a/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt +++ b/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt @@ -23,16 +23,7 @@ class CreateRoomActivityViewModel @Inject constructor( var publishingSetting = ObservableInt() lateinit var resources: Resources val publishingSettingItems by lazy { - listOf( - PublishingSettingItem( - resources.getString(R.string.public_room), - resources.getString(R.string.public_room_description) - ), - PublishingSettingItem( - resources.getString(R.string.private_room), - resources.getString(R.string.private_room_description) - ) - ) + repository.getPublishingSettingItems(resources) } var callback: SnackbarCallback? = null @@ -55,7 +46,8 @@ class CreateRoomActivityViewModel @Inject constructor( fun onBackButtonClicked() = navigator.closeActivity() fun onSubmit() { - val isPublic = publishingSetting.get() == 0 + val item = publishingSettingItems[publishingSetting.get()] + val isPublic = item.id == PublishingSettingItem.Id.PUBLIC repository.createNewRoom(roomName.get() ?: "", roomDescription.get() ?: "", isPublic) .subscribe({ response -> From a14726c5c2a5ee3788c84e7381f1db802aed515a Mon Sep 17 00:00:00 2001 From: Mori Atsushi Date: Wed, 23 May 2018 20:15:14 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[delete]=20=E4=B8=8D=E8=A6=81=E3=81=AA?= =?UTF-8?q?=E8=A6=81=E7=B4=A0=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syncpod/viewmodel/CreateRoomActivityViewModel.kt | 8 -------- 1 file changed, 8 deletions(-) diff --git a/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt b/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt index 12d2927d..54d77618 100644 --- a/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt +++ b/app/src/main/kotlin/com/cyder/android/syncpod/viewmodel/CreateRoomActivityViewModel.kt @@ -65,12 +65,4 @@ class CreateRoomActivityViewModel @Inject constructor( } }) } - - companion object { - private const val ID = "id" - private const val TITLE = "title" - private const val DESCRIPTION = "description" - private const val PUBLIC_ROOM = "public_room" - private const val PRIVATE_ROOM = "private_room" - } } \ No newline at end of file