Skip to content

Commit

Permalink
change actvity to dialogfragment
Browse files Browse the repository at this point in the history
  • Loading branch information
rigo9412 committed May 18, 2019
1 parent facb262 commit 206f84c
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 22 additions & 18 deletions app/src/main/java/com/rigoberto/formulatorexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ 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)
setContentView(R.layout.activity_main)

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())
Expand All @@ -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))
}
}

}
8 changes: 8 additions & 0 deletions app/src/main/res/anim/slide_down.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="100%" />
</set>
7 changes: 7 additions & 0 deletions app/src/main/res/anim/slide_up.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
20 changes: 20 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@
<item name="colorControlActivated">@color/orange_ligth</item>
</style>

<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="colorPrimaryDark">@color/blue_dark</item>
<item name="colorPrimary">@color/blue_normal</item>

<!-- Set this to true if you want Full Screen without status bar -->
<item name="android:windowFullscreen">false</item>

<item name="android:windowIsFloating">false</item>

<!-- This is important! Don't forget to set window background -->
<item name="android:windowBackground">@color/blue_normal</item>


<!-- Additionally if you want animations when dialog opening -->
<item name="android:windowEnterAnimation">@anim/slide_up</item>
<item name="android:windowExitAnimation">@anim/slide_down</item>
</style>


<style name="ThemeLight" parent="Theme.MaterialComponents.Light">
<item name="colorPrimary">@color/white</item>
Expand All @@ -35,6 +54,7 @@
<item name="colorControlNormal">@color/blue_light</item>
<item name="colorControlHighlight">@color/blue_light</item>
<item name="colorControlActivated">@color/orange_normal</item>

</style>


Expand Down
6 changes: 0 additions & 6 deletions formslibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
<uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION"/>

<application>
<activity
android:name=".views.FormsActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_forms"
android:windowSoftInputMode="adjustPan"
android:theme="@style/FullscreenTheme"/>

<service
android:name=".services.FetchAddressIntentService"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.rigo.ramos.formslibrary.model

import org.json.JSONObject

interface ActionsListenerForm {

fun onSave(result: JSONObject)

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class FormFragment : Fragment() {

// TODO: Rename and change types of parameters
private var form: Form? = null
private var listener : OnInteractionFormListner? = null
private var index = 0
private var pendingIndex = 0
lateinit var layoutParams : LinearLayout.LayoutParams
Expand Down Expand Up @@ -67,12 +66,12 @@ class FormFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val pref = SharedPreference(this.context!!)
tvTitle.text = form?.title

tvTitle.setTextColor(resources.getColor(pref.getColor(SharedPreference.COLOR_TEXT)!!))
index = 0
form?.fields?.forEach {
//

when(it.type){

TypeField.TEXT,TypeField.TEXT_DEC,TypeField.TEXT_NUM,TypeField.TEXT_HOURS,
Expand Down Expand Up @@ -245,12 +244,6 @@ class FormFragment : Fragment() {
}
}*/

override fun onAttach(context: Context?) {
super.onAttach(context)
listener = context as OnInteractionFormListner

}


fun setImage(index: Int , picture: Picture){
if(this.form?.fields!![index].type == TypeField.SELECT_IMAGE){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,83 @@ package com.rigo.ramos.formslibrary.views

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import com.rigo.ramos.formslibrary.R
import com.rigo.ramos.formslibrary.model.ActionsListenerForm
import com.rigo.ramos.formslibrary.model.Form
import com.rigo.ramos.formslibrary.model.SharedPreference
import kotlinx.android.synthetic.main.activity_forms.*
import kotlinx.android.synthetic.main.container_forms.*
import org.json.JSONObject








/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*/
const val RESULT_FORM = "result_form"
class FormsActivity : AppCompatActivity(), FormFragment.OnInteractionFormListner {

class FormsContainer : DialogFragment(), FormFragment.OnInteractionFormListner {

lateinit var listener: ActionsListenerForm
private var forms = ArrayList<Form>()
private lateinit var adapter:BottomAdapter
private var currentForm = -1;
@SuppressLint("RestrictedApi")
override fun lastField() {
hideControls()
}

private var forms= ArrayList<Form>()
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))
Expand All @@ -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()
}


Expand All @@ -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)
}
}

Expand All @@ -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}"
Expand All @@ -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()
Expand Down Expand Up @@ -161,6 +209,8 @@ class FormsActivity : AppCompatActivity(), FormFragment.OnInteractionFormListner
}




@SuppressLint("RestrictedApi")
fun hideControls(){
if(currentForm == adapter.count-1) {
Expand Down Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<com.rigo.ramos.formslibrary.views.NoSwipePager
android:id="@+id/viewPager"
Expand Down
2 changes: 1 addition & 1 deletion formslibrary/src/main/res/layout/fragment_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout

tools:context=".views.FormsActivity"
tools:context=".views.FormsContainer"
android:layout_width="match_parent"
android:layout_height="fill_parent">

Expand Down
Loading

0 comments on commit 206f84c

Please sign in to comment.