Advertisement
Guest User

Untitled

a guest
Apr 10th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.75 KB | None | 0 0
  1. package dk.medware.model
  2.  
  3. import android.content.Context
  4. import android.util.Log
  5. import android.view.View
  6. import androidsync.BackendModel
  7. import dk.medware.mitforloeb.modules.account.change_password.ChangePasswordAsync
  8. import dk.medware.mitforloeb.modules.account.login.LoginAsync
  9. import dk.medware.mitforloeb.persistence.DistributedBackendModel
  10. import dk.medware.mitforloeb.util.DateUtil
  11. import dk.medware.sugarcipherlib.SugarRecord
  12. import dk.medware.sugarcipherlib.dsl.Ignore
  13. import dk.medware.sugarcipherlib.util.ReflectionUtil
  14. import net.danlew.android.joda.DateUtils
  15. import net.danlew.android.joda.JodaTimeAndroid
  16. import java.util.*
  17.  
  18. /**
  19.  * Created by Mads on 12/05/2016.
  20.  */
  21. class User : SugarRecord() {
  22.  
  23.     var cpr = ""
  24.     var currentCpr = ""
  25.     var sessionKey: String? = null
  26.     var rememberMe = false
  27.     var mustChangePassword = false
  28.     var gcmToken: String? = null
  29.     var isLoggedIn = false
  30.     var currentPatientGroupCode = ""
  31.     var currentPatientGroupTitle = ""
  32.     var hasCourse = false
  33.  
  34.     @Ignore
  35.     private var currentPatientGroup: PatientGroup? = null
  36.     @Ignore
  37.     private var currentPatient: Patient? = null
  38.  
  39.     fun getPatient(): Patient? {
  40.         currentPatient = try {
  41.             if (SugarRecord.count<Patient>(Patient::class.java) == 0L) {
  42.                 Patient()
  43.             } else {
  44.                 SugarRecord.find(Patient::class.java, "cpr = ?", this.currentCpr).first()
  45.             }
  46.         } catch (e: Exception) {
  47.             Patient()
  48.         }
  49.         return currentPatient
  50.     }
  51.  
  52.     fun logIn(view: View, context: Context, cpr: String, password: String, rememberMe: Boolean) {
  53.         LoginAsync(view, context, cpr, password, rememberMe).execute()
  54.     }
  55.  
  56.     fun changePassword(context: Context, newPassword: String, confirmNewPassword: String) {
  57.         ChangePasswordAsync(context, newPassword, confirmNewPassword).execute()
  58.     }
  59.  
  60.     fun logOut() {
  61.         clearData()
  62.     }
  63.  
  64.     fun hasActivePatientRelation(): Boolean = getActivePatientRelations().isNotEmpty()
  65.  
  66.     fun getActivePatientRelations(): List<PatientRelation> {
  67.         Log.d("RELATIONS COUNT", SugarRecord.listAll(PatientRelation::class.java).count().toString())
  68.         val relations = SugarRecord.find(PatientRelation::class.java, "ended_at IS NULL OR ended_at > ?", Date().time.toString()).toList()
  69.         relations.forEach { Log.d("PGC", it.patientGroupId) }
  70.         return relations
  71.     }
  72.  
  73.     fun setCurrentPatientGroup(patientGroup: PatientGroup) {
  74.         this.currentPatientGroup = patientGroup
  75.     }
  76.  
  77.     fun getCurrentPatientGroup(): PatientGroup? {
  78.         if (currentPatientGroup == null) {
  79.             try {
  80.                 currentPatientGroup = SugarRecord.find(PatientGroup::class.java, "code = ?", currentPatientGroupCode)[0]
  81.             } catch (e: IndexOutOfBoundsException) {
  82.                 e.printStackTrace()
  83.             }
  84.         }
  85.         return currentPatientGroup
  86.     }
  87.  
  88.     private fun clearData() {
  89.         this.isLoggedIn = false
  90.         this.sessionKey = null
  91.         this.rememberMe = false
  92.         this.mustChangePassword = false
  93.         this.save()
  94.     }
  95.  
  96.     fun clearStoredData(context: Context, deletePatient: Boolean = true) {
  97.         try {
  98.             val tablesToDelete = ReflectionUtil.getDomainClasses(context)
  99.             tablesToDelete
  100.                     .filter { BackendModel::class.java.isAssignableFrom(it) }
  101.                     .forEach {
  102.                         try {
  103.                             if (it.toString() == "class dk.medware.model.Patient" && !deletePatient) {
  104.                             } else if (!isCourseObject(it)) {
  105.  
  106.                             } else {
  107.                                 SugarRecord.deleteAll(it)
  108.                             }
  109.                             SugarRecord.deleteAll(Attachment::class.java)
  110.                         } catch (ex: Exception) {
  111.                             ex.printStackTrace()
  112.                         }
  113.                     }
  114.         } catch (e: NoClassDefFoundError) {
  115.             e.printStackTrace()
  116.         }
  117.     }
  118.  
  119.     private fun isCourseObject(it: Any): Boolean = it is EducationCourse || it is EducationCourseSession || it is EducationCourseTemplate || it is EducationFamily
  120.  
  121.     companion object Factory {
  122.         private var currentUser: User? = null
  123.  
  124.         fun getCurrent(): User? {
  125.             if (currentUser == null) {
  126.                 if (count<User>(User::class.java) == 0L) {
  127.                     Log.d("Current User", "Creating new")
  128.                     currentUser = User()
  129.                     currentUser?.save()
  130.                 } else {
  131.                     currentUser = first(User::class.java)
  132.                 }
  133.             }
  134.             return currentUser
  135.         }
  136.     }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement