Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ルーム公開設定のリファクタ #283

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +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
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,6 +27,20 @@ class RoomDataRepository @Inject constructor(
) : RoomRepository {
override val isEntered = syncPodWsApi.isEntered

override fun getPublishingSettingItems(resources: Resources): List<PublishingSettingItem> {
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +13,7 @@ import io.reactivex.Single
*/
interface RoomRepository {
val isEntered: Flowable<Boolean>
fun getPublishingSettingItems(resources: Resources): List<PublishingSettingItem>
fun createNewRoom(name: String, description: String, isPublic: Boolean): Single<Room>
fun fetchJoinedRooms(): Single<List<Room>>
fun fetchPopularRooms(): Single<List<Room>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ 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

/**
* Created by atsushi on 2018/03/29.
*/

class CreateRoomActivity : BaseActivity() {
@Inject lateinit var viewModel: CreateRoomActivityViewModel
@Inject
lateinit var viewModel: CreateRoomActivityViewModel
private lateinit var binding: ActivityCreateRoomBinding

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -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<PublishingSettingItem>
) : ArrayAdapter<PublishingSettingItem>(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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,19 +22,10 @@ class CreateRoomActivityViewModel @Inject constructor(
var roomDescription: ObservableField<String?> = ObservableField()
var publishingSetting = ObservableInt()
lateinit var resources: Resources
val publishingSettingItems: MutableList<HashMap<String, String>> 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 {
repository.getPublishingSettingItems(resources)
}

val publishingSettingKeys = arrayOf(TITLE, DESCRIPTION)
var callback: SnackbarCallback? = null

override fun onStart() {
Expand All @@ -54,7 +46,8 @@ class CreateRoomActivityViewModel @Inject constructor(
fun onBackButtonClicked() = navigator.closeActivity()

fun onSubmit() {
val isPublic = publishingSettingItems[publishingSetting.get()][ID] == PUBLIC_ROOM
val item = publishingSettingItems[publishingSetting.get()]
val isPublic = item.id == PublishingSettingItem.Id.PUBLIC

repository.createNewRoom(roomName.get() ?: "", roomDescription.get() ?: "", isPublic)
.subscribe({ response ->
Expand All @@ -72,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"
}
}