From 206f84c0eac198f410f5111aed28fe43e672b172 Mon Sep 17 00:00:00 2001 From: rigo9412 Date: Sat, 18 May 2019 10:29:33 -0500 Subject: [PATCH] change actvity to dialogfragment --- .idea/misc.xml | 2 +- .../formulatorexample/MainActivity.kt | 40 +++---- app/src/main/res/anim/slide_down.xml | 8 ++ app/src/main/res/anim/slide_up.xml | 7 ++ app/src/main/res/values/styles.xml | 20 ++++ formslibrary/src/main/AndroidManifest.xml | 6 -- .../formslibrary/model/ActionsListenerForm.kt | 9 ++ .../ramos/formslibrary/views/FormFragment.kt | 13 +-- .../{FormsActivity.kt => FormsContainer.kt} | 100 ++++++++++++++---- ...activity_forms.xml => container_forms.xml} | 2 +- .../src/main/res/layout/fragment_form.xml | 2 +- formslibrary/src/main/res/values/styles.xml | 17 +++ 12 files changed, 166 insertions(+), 60 deletions(-) create mode 100644 app/src/main/res/anim/slide_down.xml create mode 100644 app/src/main/res/anim/slide_up.xml create mode 100644 formslibrary/src/main/java/com/rigo/ramos/formslibrary/model/ActionsListenerForm.kt rename formslibrary/src/main/java/com/rigo/ramos/formslibrary/views/{FormsActivity.kt => FormsContainer.kt} (66%) rename formslibrary/src/main/res/layout/{activity_forms.xml => container_forms.xml} (98%) diff --git a/.idea/misc.xml b/.idea/misc.xml index f5c6d9e..0d45e8d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/app/src/main/java/com/rigoberto/formulatorexample/MainActivity.kt b/app/src/main/java/com/rigoberto/formulatorexample/MainActivity.kt index 44832cd..88e9dee 100644 --- a/app/src/main/java/com/rigoberto/formulatorexample/MainActivity.kt +++ b/app/src/main/java/com/rigoberto/formulatorexample/MainActivity.kt @@ -6,11 +6,16 @@ import android.os.Bundle import android.util.Log import androidx.appcompat.app.AppCompatActivity import com.rigo.ramos.formslibrary.model.* -import com.rigo.ramos.formslibrary.views.FormsActivity -import com.rigo.ramos.formslibrary.views.RESULT_FORM +import com.rigo.ramos.formslibrary.views.FormsContainer import kotlinx.android.synthetic.main.activity_main.* +import org.json.JSONObject + +class MainActivity : AppCompatActivity(),ActionsListenerForm { + override fun onSave(result: JSONObject) { + Log.e("result",result.toString()) + + } -class MainActivity : AppCompatActivity() { private val FORM_ACTIVITY = 12 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -18,7 +23,7 @@ class MainActivity : AppCompatActivity() { btnForm.setOnClickListener { //val field1 = FieldImage(arrayListOf("nombre"),"Nombre*",false,"Campo requerido", TypeField.SELECT_IMAGE) - val fieldRazonSocial = FieldText(arrayListOf("razonSocial"), "RazonSocial",true, TypeField.TEXT, + val fieldRazonSocial = FieldText(arrayListOf("razonSocial"), "RazonSocial",false, TypeField.TEXT, "Es requerido",150,true, arrayListOf() ) val fieldTipoFlotilla = FieldOptions(arrayListOf("tipoFlotilla"),"Tipo flotilla",TypeField.SELECT_OPTION, arrayListOf("1","2","3"), arrayListOf()) @@ -30,21 +35,20 @@ class MainActivity : AppCompatActivity() { val form = Form("1","Informacion Personal", arrayListOf(fieldRazonSocial,fieldTipoFlotilla,field3)) - val i = Intent(this, FormsActivity::class.java) - i.putExtra(FormsActivity.EXTRA_FORMS, arrayListOf(form,form)) - i.putExtra(FormsActivity.EXTRA_THEME,R.style.ThemeDark) - i.putExtra(FormsActivity.EXTRA_BACKGROUND_COLOR,R.color.blue_normal) - i.putExtra(FormsActivity.EXTRA_TEXT_COLOR,R.color.white) - i.putExtra(FormsActivity.EXTRA_PRIMARY_COLOR,R.color.primary_dark_material_light) - i.putExtra(FormsActivity.EXTRA_ACCENT_COLOR,R.color.colorAccent) - startActivityForResult(i,FORM_ACTIVITY) - } - } + val i = Bundle() + i.putParcelableArrayList(FormsContainer.EXTRA_FORMS, arrayListOf(form)) + i.putInt(FormsContainer.EXTRA_THEME,R.style.FullScreenDialogStyle) + i.putInt(FormsContainer.EXTRA_BACKGROUND_COLOR,R.color.blue_normal) + i.putInt(FormsContainer.EXTRA_TEXT_COLOR,R.color.white) + i.putInt(FormsContainer.EXTRA_PRIMARY_COLOR,R.color.white) + i.putInt(FormsContainer.EXTRA_ACCENT_COLOR,R.color.colorAccent) + + val dialog = FormsContainer() + val ft = this.supportFragmentManager.beginTransaction() + dialog.arguments = i + dialog.show(ft, FormsContainer.TAG) - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - if(resultCode == Activity.RESULT_OK && requestCode == FORM_ACTIVITY){ - Log.e("RESULT-FORM",data?.getStringExtra(RESULT_FORM)) } } + } diff --git a/app/src/main/res/anim/slide_down.xml b/app/src/main/res/anim/slide_down.xml new file mode 100644 index 0000000..3db2d6b --- /dev/null +++ b/app/src/main/res/anim/slide_down.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/anim/slide_up.xml b/app/src/main/res/anim/slide_up.xml new file mode 100644 index 0000000..053313e --- /dev/null +++ b/app/src/main/res/anim/slide_up.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index eee1d7a..b286ef4 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -23,6 +23,25 @@ @color/orange_ligth + + diff --git a/formslibrary/src/main/AndroidManifest.xml b/formslibrary/src/main/AndroidManifest.xml index 2fa1a74..137b36e 100644 --- a/formslibrary/src/main/AndroidManifest.xml +++ b/formslibrary/src/main/AndroidManifest.xml @@ -10,12 +10,6 @@ - () + private lateinit var adapter:BottomAdapter + private var currentForm = -1; @SuppressLint("RestrictedApi") override fun lastField() { hideControls() } - private var forms= ArrayList
() - var adapter = BottomAdapter(this.supportFragmentManager) - private var currentForm = -1; + + + + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + val extras = arguments + if(extras != null || extras!!.containsKey(EXTRA_FORMS)) { + setStyle(DialogFragment.STYLE_NORMAL, extras.getInt(EXTRA_THEME,R.style.FullScreenDialogStyle)); + }else{ + dismiss() + } + } - setContentView(R.layout.activity_forms) + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + super.onCreateView(inflater, container, savedInstanceState) + val view = inflater.inflate(R.layout.container_forms, container, false); + adapter = BottomAdapter(this.childFragmentManager) + + return view; + } - val extras = intent.extras - val pref = SharedPreference(this) + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val extras = arguments + val pref = SharedPreference(this.context!!) if(extras != null || extras!!.containsKey(EXTRA_FORMS)) { - addForms(intent?.extras!!.getParcelableArrayList(EXTRA_FORMS)!!) + addForms(extras!!.getParcelableArrayList(EXTRA_FORMS)!!) - if(extras.containsKey(EXTRA_THEME)) - setTheme(extras.getInt(EXTRA_THEME,0)) + //if(extras.containsKey(EXTRA_THEME)) + //this.activity?.setTheme(extras.getInt(EXTRA_THEME,0)) - if(extras.containsKey(EXTRA_BACKGROUND_COLOR)){ + /*if(extras.containsKey(EXTRA_BACKGROUND_COLOR)){ pref.setColor(SharedPreference.COLOR_BACKGROUND,extras.getInt(EXTRA_BACKGROUND_COLOR,0)) this.fullscreen_content.setBackgroundColor(resources.getColor(extras.getInt(EXTRA_BACKGROUND_COLOR,0))) - } + }*/ if(extras.containsKey(EXTRA_TEXT_COLOR)){ pref.setColor(SharedPreference.COLOR_TEXT,extras.getInt(EXTRA_TEXT_COLOR,0)) @@ -66,12 +95,12 @@ class FormsActivity : AppCompatActivity(), FormFragment.OnInteractionFormListner btnActionSave.setTextColor(resources.getColor(pref.getColor(SharedPreference.COLOR_ACCENT)!!)) tvSteps.setTextColor(resources.getColor(pref.getColor(SharedPreference.COLOR_TEXT)!!)) - btnBack.setColorFilter(ContextCompat.getColor(this, pref.getColor(SharedPreference.COLOR_TEXT)!!), android.graphics.PorterDuff.Mode.SRC_IN) + btnBack.setColorFilter(ContextCompat.getColor(this.context!!, pref.getColor(SharedPreference.COLOR_TEXT)!!), android.graphics.PorterDuff.Mode.SRC_IN) btnNext.setBackgroundColor(pref.getColor(SharedPreference.COLOR_ACCENT)!!) }else{ - this.finish() + this.dismiss() } @@ -88,11 +117,7 @@ class FormsActivity : AppCompatActivity(), FormFragment.OnInteractionFormListner } Log.e("JSON-RESULT",result.toString()) - - val i = Intent() - i.putExtra(RESULT_FORM,result.toString()) - setResult(Activity.RESULT_OK,i) - this@FormsActivity.finish() + listener.onSave(result) } } @@ -114,7 +139,7 @@ class FormsActivity : AppCompatActivity(), FormFragment.OnInteractionFormListner btnBack.setOnClickListener { if(currentForm == 0) - this@FormsActivity.finish() + this@FormsContainer.dismiss() else{ currentForm-- tvSteps.text = "${(currentForm + 1)} de ${adapter.count}" @@ -125,10 +150,33 @@ class FormsActivity : AppCompatActivity(), FormFragment.OnInteractionFormListner hideControls() } + } + + + + override fun onStart() { + super.onStart() + val dialog = dialog + if (dialog != null) { + val width = ViewGroup.LayoutParams.MATCH_PARENT + val height = ViewGroup.LayoutParams.MATCH_PARENT + dialog.window.setLayout(width, height) + } + } + + + override fun onAttach(context: Context?) { + super.onAttach(context) + listener = context as ActionsListenerForm + } + override fun onAttach(activity: Activity?) { + super.onAttach(activity) + listener = activity as ActionsListenerForm } + fun isValidForm():Boolean{ val fragment = adapter.getItem(currentForm) as FormFragment val result =fragment.validateForm() @@ -161,6 +209,8 @@ class FormsActivity : AppCompatActivity(), FormFragment.OnInteractionFormListner } + + @SuppressLint("RestrictedApi") fun hideControls(){ if(currentForm == adapter.count-1) { @@ -203,5 +253,9 @@ class FormsActivity : AppCompatActivity(), FormFragment.OnInteractionFormListner val EXTRA_ACCENT_COLOR = "accent" val EXTRA_PRIMARY_COLOR = "primary" val EXTRA_BACKGROUND_COLOR = "backgroud" + val TAG = "FORM-CONTAINER" + val BROADCAST_PRIVIDER_FORM = "BROADCAST_PRIVIDER_FORM" + val BROADCAST_PRIVIDER_FORM_OK = "BROADCAST_PRIVIDER_FORM_OK" + val BROADCAST_PRIVIDER_FORM_MESSAGE = "BROADCAST_PRIVIDER_FORM_MESSAGE" } } diff --git a/formslibrary/src/main/res/layout/activity_forms.xml b/formslibrary/src/main/res/layout/container_forms.xml similarity index 98% rename from formslibrary/src/main/res/layout/activity_forms.xml rename to formslibrary/src/main/res/layout/container_forms.xml index 6868bd5..33e5197 100644 --- a/formslibrary/src/main/res/layout/activity_forms.xml +++ b/formslibrary/src/main/res/layout/container_forms.xml @@ -7,7 +7,7 @@ android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".views.FormsActivity"> + tools:context=".views.FormsContainer"> diff --git a/formslibrary/src/main/res/values/styles.xml b/formslibrary/src/main/res/values/styles.xml index 50a5505..50ddfc9 100644 --- a/formslibrary/src/main/res/values/styles.xml +++ b/formslibrary/src/main/res/values/styles.xml @@ -18,6 +18,23 @@ @color/black_overlay +