Created
          June 18, 2019 08:17 
        
      - 
      
- 
        Save shadygoneinsane/6b5a0e47d4fb71f208a7e98013c3dda9 to your computer and use it in GitHub Desktop. 
    Sealed Class example for mapping enums for Relationship Types for Attendance
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | sealed class AttendanceRelationship(val status: String) { | |
| class Present : AttendanceRelationship("Present") | |
| class Absent : AttendanceRelationship("Absent") | |
| class WeeklyOff : AttendanceRelationship("Weekly off") | |
| companion object { | |
| fun create(int: Int): AttendanceRelationship { | |
| return when (RelationshipType.valueOf(int)) { | |
| RelationshipType.PRESENT -> Present() | |
| RelationshipType.LATE -> Present() | |
| RelationshipType.POW -> Present() | |
| RelationshipType.LEAVEAPPROVED -> Absent() | |
| RelationshipType.LEAVEAPPLIED -> Absent() | |
| RelationshipType.ABSENT -> Absent() | |
| RelationshipType.LEAVECANCELLED -> Absent() | |
| RelationshipType.LEAVECANCELLED -> Absent() | |
| RelationshipType.WEEKLYOFF -> WeeklyOff() | |
| } | |
| } | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | enum class RelationshipType(val value: Int) { | |
| PRESENT(1), | |
| LATE(2), | |
| POW(3), | |
| LEAVEAPPROVED(4), | |
| WEEKLYOFF(5), | |
| ABSENT(6), | |
| LEAVEAPPLIED(7), | |
| LEAVECANCELLED(8), | |
| LEAVEREJECTED(9); | |
| companion object { | |
| fun valueOf(value: Int): RelationshipType { | |
| return values() | |
| .firstOrNull { it.value == value } | |
| ?: throw Resources.NotFoundException("Could not find Relationship Type with value: $value") | |
| } | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | when (val relationship: AttendanceRelationship = AttendanceRelationship.create(/*INT val for RelationshipType enum */)) { | |
| is AttendanceRelationship.Present -> { | |
| some_text_view.text = relationship.status | |
| } | |
| is AttendanceRelationship.Absent -> { | |
| some_text_view.text = relationship.status | |
| } | |
| is AttendanceRelationship.WeeklyOff -> { | |
| some_text_view.text = relationship.status | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment