Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.96 KB | None | 0 0
  1. fun rellenarfases(arrayDatos: ByteArray) {
  2.  
  3. val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
  4.  
  5.  
  6. if (arrayDatos[0] == 0.toByte()) {
  7. //guardamos el valor en un array para mostrar
  8. arrayValores1 = arrayDatos
  9. //arrayValores1 = byteArrayOf(1.toByte(),2.toByte(),3.toByte())
  10.  
  11. ///Envia el array al sectionPagerAdapter para luego mandarlo a su fragment correspondiente
  12. sectionsPagerAdapter.setArray(arrayValores1)
  13.  
  14. sectionsPagerAdapter.notifyDataSetChanged()}
  15.  
  16. class SectionsPagerAdapter(private val context: Context, fm: FragmentManager) : FragmentPagerAdapter(fm) {
  17.  
  18.  
  19. //Nombre de las pestañas
  20. private val TAB_TITLES = arrayOf(
  21. "l1",
  22. "l2",
  23. "l3",
  24. "l4"
  25. )
  26.  
  27.  
  28. var arrayValores = byteArrayOf(0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),
  29. 0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),
  30. 0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),
  31. 0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte())
  32.  
  33. override fun getItem(position: Int): Fragment {
  34. // getItem is called to instantiate the fragment for the given page.
  35. // Return a PlaceholderFragment (defined as a static inner class below).
  36. if ( verArrayVacio(arrayValores) == true) {
  37. print ( "El array esta vacio")
  38. } else {
  39. if (position == 0) {
  40. if (arrayValores[0] == 0.toByte()) {
  41. //return PlaceholderFragment.newInstance2(byteArrayOf(1.toByte(),2.toByte(),3.toByte()))
  42. return PlaceholderFragment.newInstance2(arrayValores)
  43. }
  44. }
  45. if (position == 1) {
  46. //return PlaceholderFragment2.newInstance(position + 1)
  47. if (arrayValores[0] == 1.toByte()) {
  48. //return PlaceholderFragment.newInstance2(byteArrayOf(1.toByte(),2.toByte(),3.toByte()))
  49. return PlaceholderFragment.newInstance2(arrayValores)
  50. }
  51. }
  52. if (position == 2) {
  53. //return PlaceholderFragment3.newInstance(position + 1)
  54. if (arrayValores[0] == 2.toByte()) {
  55. //return PlaceholderFragment.newInstance2(byteArrayOf(1.toByte(),2.toByte(),3.toByte()))
  56. return PlaceholderFragment.newInstance2(arrayValores)
  57. }
  58. }
  59. if (position == 3) {
  60. //return PlaceholderFragment4.newInstance(position + 1)
  61. if (arrayValores[0] == 3.toByte()) {
  62. //return PlaceholderFragment.newInstance2(byteArrayOf(1.toByte(),2.toByte(),3.toByte()))
  63. return PlaceholderFragment.newInstance2(arrayValores)
  64. }
  65. }
  66. }
  67. return PlaceholderFragment.newInstance2(arrayValores)
  68. }
  69.  
  70. //manda el valor del array
  71. fun getArray(): ByteArray {
  72. return arrayValores
  73. }
  74.  
  75. //Recoge el valor del array desde otro activity
  76. fun setArray(array:ByteArray) {
  77. this.arrayValores = array
  78. print(arrayValores)
  79. getItem(arrayValores[0].toInt())
  80. }
  81.  
  82. fun verArrayVacio(array: ByteArray) : Boolean {
  83. if (array.contentEquals(byteArrayOf(0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),
  84. 0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),
  85. 0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),
  86. 0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte(),0.toByte())) == true) {
  87. return true
  88. } else {
  89. return false
  90. }
  91. }
  92.  
  93. override fun getPageTitle(position: Int): CharSequence? {
  94. return context.resources.getString(TAB_TITLES[position])
  95. }
  96.  
  97. //Numero de pestañas
  98. override fun getCount(): Int {
  99. // Show 4 total pages.
  100. return 4
  101. }
  102.  
  103. class PlaceholderFragment : Fragment() {
  104.  
  105. private lateinit var pageViewModel: PageViewModel
  106.  
  107.  
  108. override fun onCreate(savedInstanceState: Bundle?) {
  109. super.onCreate(savedInstanceState)
  110. pageViewModel = ViewModelProviders.of(this).get(PageViewModel::class.java).apply {
  111. setIndex(arguments?.getInt(KEY_REG_TEXT) ?: 1)
  112.  
  113. }
  114. }
  115.  
  116.  
  117.  
  118. override fun onCreateView(
  119. inflater: LayoutInflater, container: ViewGroup?,
  120. savedInstanceState: Bundle?
  121. ): View? {
  122. val root = inflater.inflate(R.layout.fragment_informes, container, false)
  123.  
  124.  
  125. //recoge el valor del array pasado por el sectionPagerAdapter a traves del Bundle
  126. val message = arguments!!.getByteArray(KEY_REG_TEXT)
  127.  
  128. val instTension: TextView = root.findViewById(R.id.instTension)
  129. val maxTension: TextView = root.findViewById(R.id.maxTension)
  130. val minTension: TextView = root.findViewById(R.id.minTension)
  131. val instCorriente: TextView = root.findViewById(R.id.instCorriente)
  132. val maxCorriente: TextView = root.findViewById(R.id.maxCorriente)
  133. val minCorriente: TextView = root.findViewById(R.id.minCorriente)
  134. val instActiva: TextView = root.findViewById(R.id.instActiva)
  135. val maxActiva: TextView = root.findViewById(R.id.maxActiva)
  136. val minActiva: TextView = root.findViewById(R.id.minActiva)
  137. val instInductiva: TextView = root.findViewById(R.id.instInductiva)
  138. val maxInductiva: TextView = root.findViewById(R.id.maxInductiva)
  139. val minInductiva: TextView = root.findViewById(R.id.minInductiva)
  140. val instCapacitiva: TextView = root.findViewById(R.id.instCapacitiva)
  141. val maxCapacitiva: TextView = root.findViewById(R.id.maxCapacitiva)
  142. val minCapacitiva: TextView = root.findViewById(R.id.minCapacitiva)
  143. val instAparente: TextView = root.findViewById(R.id.instAparente)
  144. val maxAparente: TextView = root.findViewById(R.id.maxAparente)
  145. val minAparente: TextView = root.findViewById(R.id.minAparente)
  146. val instFactor: TextView = root.findViewById(R.id.instFactor)
  147. val maxFactor: TextView = root.findViewById(R.id.maxFactor)
  148. val minFactor: TextView = root.findViewById(R.id.minFactor)
  149. val instCoseno: TextView = root.findViewById(R.id.instCoseno)
  150. val maxCoseno: TextView = root.findViewById(R.id.maxCoseno)
  151. val minCoseno: TextView = root.findViewById(R.id.minCoseno)
  152.  
  153.  
  154. if (message[1] == 0.toByte()) {
  155.  
  156. instTension.text = convertirValores(message[2],message[3],message[4],message[5])
  157. var test = convertirValores(message[2],message[3],message[4],message[5])
  158. Log.e("VALORESI",test)
  159. instCorriente.text = convertirValores(message[6],message[7],message[8],message[9])
  160. instActiva.text = convertirValores(message[10],message[11],message[12],message[13])
  161. instInductiva.text = convertirValores(message[14],message[15],message[16],message[17])
  162. instCapacitiva.text = convertirValores(message[18],message[19],message[20],message[21])
  163. instAparente.text = convertirValores(message[22],message[23],message[24],message[25])
  164. instFactor.text = convertirValores(message[26],message[27],message[28],message[29])
  165. instCoseno.text = convertirValores(message[30],message[31],message[32],message[33])
  166.  
  167. }
  168.  
  169. if (message[1] == 1.toByte()) {
  170.  
  171. maxTension.text = convertirValores(message[2],message[3],message[4],message[5])
  172. var test = convertirValores(message[2],message[3],message[4],message[5])
  173. Log.e("VALORESI",test)
  174. maxCorriente.text = convertirValores(message[6],message[7],message[8],message[9])
  175. maxActiva.text = convertirValores(message[10],message[11],message[12],message[13])
  176. maxInductiva.text = convertirValores(message[14],message[15],message[16],message[17])
  177. maxCapacitiva.text = convertirValores(message[18],message[19],message[20],message[21])
  178. maxAparente.text = convertirValores(message[22],message[23],message[24],message[25])
  179. maxFactor.text = convertirValores(message[26],message[27],message[28],message[29])
  180. maxCoseno.text = convertirValores(message[30],message[31],message[32],message[33])
  181.  
  182. }
  183.  
  184. if (message[1] == 2.toByte()) {
  185.  
  186. minTension.text = convertirValores(message[2],message[3],message[4],message[5])
  187. var test = convertirValores(message[2],message[3],message[4],message[5])
  188. Log.e("VALORESI",test)
  189. minCorriente.text = convertirValores(message[6],message[7],message[8],message[9])
  190. minActiva.text = convertirValores(message[10],message[11],message[12],message[13])
  191. minInductiva.text = convertirValores(message[14],message[15],message[16],message[17])
  192. minCapacitiva.text = convertirValores(message[18],message[19],message[20],message[21])
  193. minAparente.text = convertirValores(message[22],message[23],message[24],message[25])
  194. minFactor.text = convertirValores(message[26],message[27],message[28],message[29])
  195. minCoseno.text = convertirValores(message[30],message[31],message[32],message[33])
  196.  
  197. }
  198.  
  199. return root
  200. }
  201.  
  202. fun convertirValores(byte1 : Byte, byte2: Byte, byte3: Byte,byte4: Byte) : String{
  203.  
  204. var byteArray = byteArrayOf(byte1,byte2,byte3,byte4)
  205.  
  206. val convertirString = String(byteArray, StandardCharsets.UTF_8).replace(0.toChar().toString(), "")
  207.  
  208. return convertirString
  209. }
  210.  
  211. companion object {
  212. /**
  213. * The fragment argument representing the section number for this
  214. * fragment.
  215. */
  216.  
  217.  
  218. //private const val ARG_SECTION_NUMBER = "section_number"
  219.  
  220. /**
  221. * Returns a new instance of this fragment for the given section
  222. * number.
  223. */
  224. @JvmStatic
  225. var KEY_REG_TEXT = "text"
  226.  
  227.  
  228.  
  229. fun newInstance2(array: ByteArray): PlaceholderFragment {
  230. val frag = PlaceholderFragment()
  231.  
  232. var args: Bundle? = Bundle()
  233. frag.getArguments()
  234. if (args == null)
  235. args = Bundle()
  236.  
  237. args.putByteArray(KEY_REG_TEXT, array)
  238.  
  239. frag.setArguments(args)
  240.  
  241. return frag
  242. }
  243.  
  244.  
  245. }
  246.  
  247. class PageViewModel : ViewModel() {
  248.  
  249.  
  250. private val _index = MutableLiveData<Int>()
  251.  
  252.  
  253. var instTension : LiveData<String> = Transformations.map(_index) {
  254. "$it"
  255. }
  256. var maxTension: LiveData<String> = Transformations.map(_index) {
  257. "$it"
  258. }
  259. var minTension: LiveData<String> = Transformations.map(_index) {
  260. "$it"
  261. }
  262. var instCorriente: LiveData<String> = Transformations.map(_index) {
  263. "$it"
  264. }
  265. var maxCorriente: LiveData<String> = Transformations.map(_index) {
  266. "$it"
  267. }
  268. var minCorriente: LiveData<String> = Transformations.map(_index) {
  269. "$it"
  270. }
  271. var instActiva: LiveData<String> = Transformations.map(_index) {
  272. "$it"
  273. }
  274. var maxActiva: LiveData<String> = Transformations.map(_index) {
  275. "$it"
  276. }
  277. var minActiva: LiveData<String> = Transformations.map(_index) {
  278. "$it"
  279. }
  280.  
  281. var instInductiva: LiveData<String> = Transformations.map(_index) {
  282. "$it"
  283. }
  284.  
  285. var maxInductiva: LiveData<String> = Transformations.map(_index) {
  286. "$it"
  287. }
  288. var minInductiva: LiveData<String> = Transformations.map(_index) {
  289. "$it"
  290. }
  291.  
  292. var instCapacitiva: LiveData<String> = Transformations.map(_index) {
  293. "$it"
  294. }
  295. var maxCapacitiva: LiveData<String> = Transformations.map(_index) {
  296. "$it"
  297. }
  298. var minCapacitiva: LiveData<String> = Transformations.map(_index) {
  299. "$it"
  300. }
  301. var instAparente: LiveData<String> = Transformations.map(_index) {
  302. "$it"
  303. }
  304. var maxAparente: LiveData<String> = Transformations.map(_index) {
  305. "$it"
  306. }
  307. var minAparente: LiveData<String> = Transformations.map(_index) {
  308. "$it"
  309. }
  310. var instFactor: LiveData<String> = Transformations.map(_index) {
  311. "$it"
  312. }
  313. var maxFactor: LiveData<String> = Transformations.map(_index) {
  314. "$it"
  315. }
  316. var minFactor: LiveData<String> = Transformations.map(_index) {
  317. "$it"
  318. }
  319. var instCoseno: LiveData<String> = Transformations.map(_index) {
  320. "$it"
  321. }
  322. var minCoseno: LiveData<String> = Transformations.map(_index) {
  323. "$it"
  324. }
  325. var maxCoseno: LiveData<String> = Transformations.map(_index) {
  326. "$it"
  327. }
  328.  
  329.  
  330.  
  331.  
  332. fun setIndex(index: Int) {
  333. _index.value = index
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement