فهرست منبع

Implement CoverEditorDialog (not finish yet)

Not complete:
category
choose scene
Wayne 6 سال پیش
والد
کامیت
e3295c3cd1

+ 34 - 0
src/main/java/com/bomostory/sceneeditmodule/cover/BackCoverEditorView.kt

@@ -0,0 +1,34 @@
+package com.bomostory.sceneeditmodule.cover
+
+import android.content.Context
+import android.util.AttributeSet
+import android.view.View
+import com.example.tfat.myapplication.R
+import kotlinx.android.synthetic.main.view_back_cover_editor.view.*
+
+class BackCoverEditorView @JvmOverloads constructor(
+        context: Context,
+        attrs: AttributeSet? = null,
+        defStyleAttr: Int = 0
+) : BaseCoverEditorView(context, attrs, defStyleAttr) {
+
+    var onCancel: View.OnClickListener? = null
+    var onSave: View.OnClickListener? = null
+    val selectedColor: CoverColor
+        get() {
+            return coverColorSelectorView_backCoverEditor.selectedColor
+        }
+
+    init {
+        View.inflate(context, R.layout.view_back_cover_editor, this)
+        coverColorSelectorView_backCoverEditor.apply {
+            onChangeListener = object : CoverColorSelectorView.OnChangeListener {
+                override fun onChange() {
+                    setCoverColor(selectedColor, this@BackCoverEditorView.iv_backCoverEditor_cover)
+                }
+            }
+        }
+        btn_backCoverEditor_save.setOnClickListener { onSave?.onClick(it) }
+        btn_backCoverEditor_cancel.setOnClickListener { onCancel?.onClick(it) }
+    }
+}

+ 20 - 0
src/main/java/com/bomostory/sceneeditmodule/cover/BaseCoverEditorView.kt

@@ -0,0 +1,20 @@
+package com.bomostory.sceneeditmodule.cover
+
+import android.content.Context
+import android.graphics.drawable.ColorDrawable
+import android.support.constraint.ConstraintLayout
+import android.util.AttributeSet
+import android.widget.ImageView
+
+abstract open class BaseCoverEditorView @JvmOverloads constructor(
+        context: Context,
+        attrs: AttributeSet? = null,
+        defStyleAttr: Int = 0
+) : ConstraintLayout(context, attrs, defStyleAttr) {
+
+    protected fun setCoverColor(coverColor: CoverColor, cover: ImageView) {
+        val colorInt = coverColor.getColor(context)
+        val colorDrawable = ColorDrawable(colorInt)
+        cover.setImageDrawable(colorDrawable)
+    }
+}

+ 16 - 0
src/main/java/com/bomostory/sceneeditmodule/cover/Category.kt

@@ -0,0 +1,16 @@
+package com.bomostory.sceneeditmodule.cover
+
+enum class Category(val id: Int) {
+    Daily(1),
+    HumanRelations(2),
+    Character(3),
+    Ispirational(4),
+    Knowledge(5),
+    Natural(6),
+    EnvironmentalProtection(7),
+    Imagine(8),
+    Life(9),
+    Culture(10),
+    Fables(11),
+    Others(12),
+}

+ 15 - 6
src/main/java/com/bomostory/sceneeditmodule/cover/CoverColorSelectorView.kt

@@ -39,22 +39,27 @@ class CoverColorSelectorView @JvmOverloads constructor(
     }
 
     private val colorViewList = ArrayList<View>()
-    private var selectedColor = CoverColor.Color1
 
     init {
         inflate(context, R.layout.view_cover_color_selector, this)
         viewToColorMap.keys.forEach { ivId ->
             val v = findViewById<View>(ivId)
             v.setOnClickListener { it ->
-                viewToColorMap[it.id]?.let { color -> select(color) }
+                viewToColorMap[it.id]?.let { color -> selectedColor = color }
             }
-            colorViewList.add(this)
+            colorViewList.add(v)
         }
-        select(selectedColor)
     }
 
-    fun select(coverColor: CoverColor) {
-        selectedColor = coverColor
+    var selectedColor: CoverColor = CoverColor.Color1
+        set(value) {
+            field = value
+            select(value)
+            onChangeListener?.onChange()
+        }
+    var onChangeListener: OnChangeListener? = null
+
+    private fun select(coverColor: CoverColor) {
         colorToViewMap[coverColor]?.let {
             val v = findViewById<View>(it)
             colorViewList.forEach {
@@ -79,4 +84,8 @@ class CoverColorSelectorView @JvmOverloads constructor(
     private fun View.setPadding(padding: Int) {
         setPadding(padding, padding, padding, padding)
     }
+
+    interface OnChangeListener {
+        fun onChange()
+    }
 }

+ 82 - 0
src/main/java/com/bomostory/sceneeditmodule/cover/CoverEditorDialog.kt

@@ -0,0 +1,82 @@
+package com.bomostory.sceneeditmodule.cover
+
+import android.os.Bundle
+import android.support.design.widget.TabLayout
+import android.support.v4.app.DialogFragment
+import android.support.v4.app.FragmentManager
+import android.support.v4.view.PagerAdapter
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import com.example.tfat.myapplication.R
+import kotlinx.android.synthetic.main.dialog_cover_editor.*
+import kotlinx.android.synthetic.main.dialog_cover_editor.view.*
+
+class CoverEditorDialog : DialogFragment() {
+    companion object {
+        const val PAGE_COUNT = 2
+    }
+
+    private val dialogTag = this::class.java.simpleName
+
+    var onSave: View.OnClickListener? = null
+    var onCancel: View.OnClickListener? = null
+    var storyName = frontCoverEditorView_coverEditorDialog.storyName
+    var author = frontCoverEditorView_coverEditorDialog.author
+    var frontCoverColor = frontCoverEditorView_coverEditorDialog.selectedColor
+    var backCoverColor = backCoverEditorView_coverEditorDialog.selectedColor
+
+    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
+        return inflater.inflate(R.layout.dialog_cover_editor, container, false).apply {
+            val viewPager = viewPager_coverEditorDialog_content
+            val tabLayout = tabLayout_coverEditorDialog_header
+            viewPager.adapter = object : PagerAdapter() {
+                override fun instantiateItem(container: ViewGroup, position: Int): Any {
+                    return container.getChildAt(position)
+                }
+
+                override fun isViewFromObject(p0: View, p1: Any): Boolean {
+                    return p0 == p1
+                }
+
+                override fun getCount(): Int {
+                    return PAGE_COUNT
+                }
+
+                override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
+                    // no super
+                }
+            }
+            viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))
+            tabLayout.apply {
+                addTab(newTab().setText(R.string.cover_editor_dialog_tab_front))
+                addTab(newTab().setText(R.string.cover_editor_dialog_tab_back))
+                addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
+                    override fun onTabReselected(tab: TabLayout.Tab?) {}
+                    override fun onTabUnselected(tab: TabLayout.Tab?) {}
+                    override fun onTabSelected(tab: TabLayout.Tab?) {
+                        tab?.let { viewPager.currentItem = it.position }
+                    }
+                })
+            }
+            frontCoverEditorView_coverEditorDialog.apply {
+                this.onSave = this@CoverEditorDialog.onSave
+                this.onCancel = this@CoverEditorDialog.onCancel
+            }
+            backCoverEditorView_coverEditorDialog.apply {
+                this.onSave = this@CoverEditorDialog.onSave
+                this.onCancel = this@CoverEditorDialog.onCancel
+            }
+        }
+    }
+
+    fun show(manager: FragmentManager?) {
+        manager?.apply {
+            val ft = beginTransaction()
+            findFragmentByTag(dialogTag)?.let { ft.remove(it) }
+            ft.addToBackStack(null)
+            ft.commit()
+        }
+        super.show(manager, dialogTag)
+    }
+}

+ 52 - 0
src/main/java/com/bomostory/sceneeditmodule/cover/FrontCoverEditorView.kt

@@ -0,0 +1,52 @@
+package com.bomostory.sceneeditmodule.cover
+
+import android.content.Context
+import android.util.AttributeSet
+import android.view.View
+import com.example.tfat.myapplication.R
+import kotlinx.android.synthetic.main.view_front_cover_editor.view.*
+
+class FrontCoverEditorView @JvmOverloads constructor(
+        context: Context,
+        attrs: AttributeSet? = null,
+        defStyleAttr: Int = 0
+) : BaseCoverEditorView(context, attrs, defStyleAttr) {
+
+    var onCancel: View.OnClickListener? = null
+    var onSave: View.OnClickListener? = null
+    var selectedColor: CoverColor
+        get() {
+            return coverColorSelectorView_frontCoverEditor.selectedColor
+        }
+        set(value) {
+            coverColorSelectorView_frontCoverEditor.selectedColor = value
+        }
+    var storyName: String
+        get() {
+            return et_frontCoverEditor_projectName.text.toString()
+        }
+        set(value) {
+            et_frontCoverEditor_projectName.setText(value)
+        }
+    var author: String
+        get() {
+            return et_frontCoverEditor_author.text.toString()
+        }
+        set(value) {
+            et_frontCoverEditor_author.setText(value)
+        }
+    var category: Category = Category.Daily
+
+    init {
+        View.inflate(context, R.layout.view_front_cover_editor, this)
+        coverColorSelectorView_frontCoverEditor.apply {
+            onChangeListener = object : CoverColorSelectorView.OnChangeListener {
+                override fun onChange() {
+                    setCoverColor(selectedColor, this@FrontCoverEditorView.iv_frontCoverEditor_cover)
+                }
+            }
+        }
+        btn_frontCoverEditor_save.setOnClickListener { onSave?.onClick(it) }
+        btn_frontCoverEditor_cancel.setOnClickListener { onCancel?.onClick(it) }
+    }
+}

+ 11 - 2
src/main/res/layout/dialog_cover_editor.xml

@@ -8,7 +8,7 @@
 
     <android.support.design.widget.TabLayout
         android:id="@+id/tabLayout_coverEditorDialog_header"
-        android:layout_width="0dp"
+        android:layout_width="800dp"
         android:layout_height="64dp"
         android:background="@color/pale_peach"
         app:layout_constraintLeft_toLeftOf="parent"
@@ -18,12 +18,21 @@
     <android.support.v4.view.ViewPager
         android:id="@+id/viewPager_coverEditorDialog_content"
         android:layout_width="0dp"
-        android:layout_height="0dp"
+        android:layout_height="490dp"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toBottomOf="@id/tabLayout_coverEditorDialog_header">
 
+        <com.bomostory.sceneeditmodule.cover.FrontCoverEditorView
+            android:id="@+id/frontCoverEditorView_coverEditorDialog"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content" />
+
+        <com.bomostory.sceneeditmodule.cover.BackCoverEditorView
+            android:id="@+id/backCoverEditorView_coverEditorDialog"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content" />
     </android.support.v4.view.ViewPager>
 
 </android.support.constraint.ConstraintLayout>

+ 3 - 2
src/main/res/layout/view_front_cover_editor.xml

@@ -1,7 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
-<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<merge xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
+    tools:parentTag="android.support.constraint.ConstraintLayout"
     android:layout_width="800dp"
     android:layout_height="480dp">
 
@@ -137,4 +138,4 @@
         app:layout_constraintRight_toLeftOf="@id/btn_frontCoverEditor_chooseScene"
         app:layout_constraintTop_toTopOf="@id/btn_frontCoverEditor_chooseScene" />
 
-</android.support.constraint.ConstraintLayout>
+</merge>

+ 3 - 0
src/main/res/values/dimens.xml

@@ -9,4 +9,7 @@
     <dimen name="dialogue_padding">50dp</dimen>
 
     <dimen name="cover_color_item_padding">8dp</dimen>
+
+    <dimen name="cover_editor_dialog_width">800dp</dimen>
+    <dimen name="cover_editor_dialog_height">544dp</dimen>
 </resources>

+ 2 - 0
src/main/res/values/strings.xml

@@ -26,6 +26,8 @@
     <string name="share_dialog_upload_suc">Upload success!</string>
     <string name="share_dialog_upload_failed">Upload failed</string>
 
+    <string name="cover_editor_dialog_tab_front">Front Cover</string>
+    <string name="cover_editor_dialog_tab_back">Back Cover</string>
     <string name="front_cover_editor_dialog_project_name_hint">(Project) Story Name</string>
     <string name="front_cover_editor_dialog_author_hint">Author</string>
     <string name="front_cover_editor_dialog_save_btn">Save</string>