Guest User

Untitled

a guest
Feb 2nd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. package us.slab.seller.viewmodel
  2.  
  3. import android.annotation.SuppressLint
  4. import android.view.View
  5. import android.view.ViewGroup
  6. import android.widget.EditText
  7. import chela.kotlin.Ch
  8. import chela.kotlin.DptoPx
  9. import chela.kotlin.view.ChStyleModel
  10. import chela.kotlin.view.ChViewModel
  11. import com.chela.annotation.EX
  12. import com.chela.annotation.STYLE
  13. import com.chela.annotation.VM
  14. import us.slab.seller.App
  15. import us.slab.seller.R
  16. import us.slab.seller.holder.Signin
  17. import us.slab.seller.vo.User
  18.  
  19. @VM
  20. @SuppressLint("StaticFieldLeak")
  21. object SigninVM:ChViewModel(){
  22. override fun start(){
  23. holder.start()
  24. mx.forEachIndexed { i, _ -> mx[i] = 100.0 }
  25. ma.forEachIndexed { i, _ -> mx[i] = 0.0 }
  26. }
  27. override fun end(){
  28. holder.end()
  29. mx.forEachIndexed { i, _ -> mx[i] = 0.0 }
  30. ma.forEachIndexed { i, _ -> mx[i] = 0.0 }
  31. }
  32. override fun paused(){
  33. holder.paused()
  34. }
  35. fun isValid(){
  36. val v = if(Ch.ruleset.isOk("se.userid" to inputId["userid"], "se.pass" to inputPw["pass"])) "btn"
  37. else "btnDisable"
  38. if(v != signIn["background"]){
  39. App.looper(Ch.time(350)) {
  40. signIn.scaleX = it.bounceOut(0.85, 1.0)
  41. signIn.scaleY = it.bounceOut(0.85, 1.0)
  42. Signin.renderSync()
  43. }
  44. }
  45. signIn.background = v
  46. Signin.renderSync()
  47. }
  48. fun inputEffect(pos:Pair<Int, Int>, view: View, drawable:Int){
  49. val (x, y) = pos
  50. val v = View(view.context)
  51. val dp = Ch.window.DptoPx
  52. Ch.prop.layout.width(v, 14.0*dp)
  53. Ch.prop.layout.height(v, 14.0*dp)
  54. Ch.prop.view.background(v, drawable)
  55. Ch.prop.view.x(v, x.toDouble() - 3.0*dp)
  56. Ch.prop.view.y(v, y.toDouble() - 10.0*dp)
  57. Ch.view.addView(R.id.main, v)
  58. val s = Ch.math.rand(1.5, 2.0)
  59. val a = Ch.math.rand(0.2, 0.5)
  60. val r = Ch.math.rand(-37.0, 37.0)
  61. App.looper(
  62. Ch.time(400),
  63. Ch.ended { (v.parent as ViewGroup).removeView(v) }) {
  64. Ch.prop.view.scaleX(v, it.circleOut(1.0, s))
  65. Ch.prop.view.scaleY(v, it.circleOut(1.0, s))
  66. Ch.prop.view.alpha(v, it.circleIn(a, 0.0))
  67. Ch.prop.view.rotation(v, it.circleIn(0.0, r))
  68. }
  69. }
  70. val delay = 50
  71. val time = Ch.time(700)
  72. val mx = mutableListOf(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  73. val ma = mutableListOf(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  74.  
  75. val holder = ModalHolder()
  76. val label0 = Label0
  77. val label1 = Label1
  78. val vali0 = Vali0
  79. val vali1 = Vali1
  80. val inputId = InputId
  81. val inputPw = InputPw
  82. var isLock = false
  83. val signIn = SignInBtn
  84. val forgot = ForgotBtn
  85. val signup = SignUpBtn
  86. }
  87. @STYLE object Label0:ChStyleModel(){
  88. val style = "label"
  89. val marginStart:Double get() = SigninVM.mx[0]
  90. val alpha:Double get() = SigninVM.ma[0]
  91. val text:Any get() = Ch.i18n["signin.label0"]
  92. }
  93. @STYLE object Label1:ChStyleModel(){
  94. val style = "label"
  95. val marginStart:Double get() = SigninVM.mx[2]
  96. val alpha:Double get() = SigninVM.ma[2]
  97. val text:Any get() = Ch.i18n["signin.label1"]
  98. }
  99. @STYLE object Vali0:ChStyleModel(){
  100. val style = "valiError"
  101. var text = ""
  102. }
  103. @STYLE object Vali1:ChStyleModel(){
  104. val style = "valiError"
  105. var text = ""
  106. }
  107. @STYLE object InputId:ChStyleModel(){
  108. @EX var userid = ""
  109. val style = "input"
  110. val marginStart:Double get() = SigninVM.mx[1]
  111. val alpha:Double get() = SigninVM.ma[1]
  112. val inputType = Ch.prop.text.type.text
  113.  
  114. val maxLength = 30
  115. val focusChange = AppVM.inputFocus
  116. @SuppressLint("StaticFieldLeak")
  117. val textChanged = object: Ch.OnTextChanged(){
  118. override fun onChanged(view: EditText, s:CharSequence, start:Int, before:Int, count:Int){
  119. val v = "$s"
  120. if(v.isEmpty()) userid = ""
  121. else if(!Ch.ruleset.isOk("se.useridInput" to v)){
  122. if(Vali0.text == ""){
  123. Vali0.text = Ch.i18n["signin.valiId"]
  124. Signin.renderSync()
  125. }
  126. view.setText(userid)
  127. view.setSelection(userid.length)
  128. SigninVM.inputEffect(pos(), view, R.drawable.crop_fa635c)
  129. }else{
  130. userid = v
  131. if(before < count) {
  132. if (Vali0.text != "") {
  133. Vali0.text = ""
  134. Signin.renderSync()
  135. }
  136. SigninVM.inputEffect(pos(), view, R.drawable.crop)
  137. }
  138. }
  139. SigninVM.isValid()
  140. }
  141. }
  142. }
  143. @STYLE object InputPw:ChStyleModel(){
  144. @EX var pass = ""
  145. val style = "input"
  146. val marginStart:Double get() = SigninVM.mx[3]
  147. val alpha:Double get() = SigninVM.ma[3]
  148. val inputType = Ch.prop.text.type.textPassword
  149. val maxLength = 30
  150. val focusChange = AppVM.inputFocus
  151. @SuppressLint("StaticFieldLeak")
  152. val textChanged = object: Ch.OnTextChanged(){
  153. override fun onChanged(view: EditText, s:CharSequence, start:Int, before:Int, count:Int){
  154. val v = "$s"
  155. if(v.isEmpty()) pass = ""
  156. else if(!Ch.ruleset.isOk("se.passInput" to v)){
  157. if(Vali1.text == ""){
  158. Vali1.text = Ch.i18n["signin.valiPw"]
  159. Signin.renderSync()
  160. }
  161. view.setText(pass)
  162. view.setSelection(pass.length)
  163. SigninVM.inputEffect(pos(), view, R.drawable.crop_fa635c)
  164. }else{
  165. pass = v
  166. if (before < count) {
  167. if (Vali1.text != "") {
  168. Vali1.text = ""
  169. Signin.renderSync()
  170. }
  171. SigninVM.inputEffect(pos(), view, R.drawable.crop)
  172. }
  173. }
  174. SigninVM.isValid()
  175. }
  176. }
  177. }
  178. @STYLE object SignInBtn:ChStyleModel(){
  179. override fun end() {
  180. background = "btnDisable"
  181. scaleX = 1.0
  182. scaleY = 1.0
  183. }
  184. val style = "button"
  185. val marginStart:Double get() = SigninVM.mx[4]
  186. val alpha:Double get() = SigninVM.ma[4]
  187. val text:Any get() = Ch.i18n["signin.btnSignin"]
  188. var textColor = "#ffffff"
  189. var background = "btnDisable"
  190. var scaleX = 1.0
  191. var scaleY = 1.0
  192. var click = View.OnClickListener{
  193. if(SigninVM.isLock) return@OnClickListener
  194. if(!Ch.net.isOn()) Signin.alert(Ch.i18n["alert.disconnect"])
  195. else if(!Ch.ruleset.isOk("se.userid" to InputId.userid)){
  196. Signin.alert(Ch.i18n["alert.userid"])
  197. }else if(!Ch.ruleset.isOk("se.pass" to InputPw.pass)){
  198. Signin.alert(Ch.i18n["alert.pw"])
  199. }else{
  200. SigninVM.isLock = true
  201. val result = Ch.net.api("login", "userid" to InputId.userid, "pass" to InputPw.pass){
  202. val user = it.result as User
  203. it.header("authtoken")?.let{user.authtoken = it}
  204. App.user = user
  205. //Ch.sql.DB?.exec("se_add", user)
  206. App.modalRouter.pop()
  207. SigninVM.isLock = false
  208. }
  209. if(result is Ch.ApiResult.fail){
  210. Signin.alert(Ch.i18n["signin.failed"])
  211. SigninVM.isLock = false
  212. }
  213. }
  214. }
  215. }
  216. @STYLE object ForgotBtn:ChStyleModel(){
  217. val style = "button"
  218. val marginStart:Double get() = SigninVM.mx[5]
  219. val alpha:Double get() = SigninVM.ma[5]
  220. val text:Any get() = Ch.i18n["signin.btnForgot.ko"]
  221. val fontFamily = "nn"
  222. var textColor = "#ffffff"
  223. var textSize = 20.0.DptoPx
  224. var click = View.OnClickListener{
  225. if(SigninVM.isLock) return@OnClickListener
  226. }
  227. }
  228. @STYLE object SignUpBtn:ChStyleModel(){
  229. val style = "button"
  230. val marginStart:Double get() = SigninVM.mx[6]
  231. val alpha:Double get() = SigninVM.ma[6]
  232. val text:Any get() = Ch.i18n["signin.btnSignup"]
  233. var textColor = "#444444"
  234. var background = "btnBorder"
  235. var click = View.OnClickListener{
  236. if(SigninVM.isLock) return@OnClickListener
  237. }
  238. }
Add Comment
Please, Sign In to add comment