ios - C# Xamarin avoiding code repetition -


i making basic note taking app using c# in xamarin studio , have been following along course online. problem code examples provided contain lot of repetitive code making updating app increasingly hard. labelled relevant code below pertinent 2 similar classes, editnoteviewcontroller.cs , addnoteviewcontroller.cs, how can extract code out can call once?

alternatively looking @ extracting 1 class , having conditional flow rendering different attributes based on whether user creating or updating note.

editnotecontroller.cs

using system; using uikit;  namespace notetaker.ios {     public class editnoteviewcontroller : uiviewcontroller     {         note note;          public editnoteviewcontroller (note _note)         {             note = _note;         }          public override void viewdidload ()         {             base.viewdidload ();             this.navigationcontroller.navigationbar.bartintcolor = uicolor.fromrgb (255, 0, 255); // repeated code. used in mainviewcontroller.cs             this.title = "edit note";             this.navigationcontroller.navigationbar.titletextattributes = new uistringattributes () { foregroundcolor = uicolor.white };              this.navigationcontroller.navigationbar.tintcolor = uicolor.white;             this.view.backgroundcolor = uicolor.white;               var titleentrybox = new uitextfield () {                 frame = new coregraphics.cgrect (0, 100, view.bounds.width, 45), // repeated code                 backgroundcolor = uicolor.lightgray,                 textcolor = uicolor.black,                 text = note.title             };              var descriptionlabel = new uilabel () {                 frame = new coregraphics.cgrect (10, 180, 250, 35),                 text = "description",             };              var descriptionentrybox = new uitextview () {                 frame = new coregraphics.cgrect (0, 220, view.bounds.width, 100), // repeated code                 backgroundcolor = uicolor.lightgray,                 textcolor = uicolor.black,                 text = note.description             };              var updatebutton = new uibutton () {                 frame = new coregraphics.cgrect (10, 340, 120, 45)               }; // repeated code              updatebutton.settitle ("update", uicontrolstate.normal);             updatebutton.backgroundcolor = uicolor.fromrgb (255, 0, 255);             updatebutton.settitlecolor (uicolor.white, uicontrolstate.normal);              this.view.add (titleentrybox);             this.view.add (descriptionlabel);             this.view.add (descriptionentrybox);             this.view.add (updatebutton);               updatebutton.touchupinside += (sender, e) => {                 if (titleentrybox.text.length < 4)                     return;                 var notetoupdate = new note () {                     id = note.id,                     title = titleentrybox.text,                     description = descriptionentrybox.text,                     datecreated = datetime.now                 };                 database.updatenote (notetoupdate);                  this.navigationcontroller.popviewcontroller (true);             };         }     } } 

addnoteviewcontroller.cs

using system; using uikit;  namespace notetaker.ios {     public class addnoteviewcontroller : uiviewcontroller     {         public addnoteviewcontroller ()          {         }          public override void viewdidload ()         {             base.viewdidload ();             this.navigationcontroller.navigationbar.bartintcolor = uicolor.fromrgb (255, 0, 255);             this.title = "new note";             this.navigationcontroller.navigationbar.titletextattributes = new uistringattributes () { foregroundcolor = uicolor.white };              this.navigationcontroller.navigationbar.tintcolor = uicolor.white;             this.view.backgroundcolor = uicolor.white;              var titleentrybox = new uitextfield () {                 frame = new coregraphics.cgrect (0, 100, view.bounds.width, 45),                 backgroundcolor = uicolor.lightgray,                 placeholder = "enter title...",                 textcolor = uicolor.black             };              var descriptionlabel = new uilabel () {                 frame = new coregraphics.cgrect (10, 180, 250, 35),                 text = "enter description below"             };              var descriptionentrybox = new uitextview () {                 frame = new coregraphics.cgrect (0, 220, view.bounds.width, 100),                 backgroundcolor = uicolor.lightgray,                 textcolor = uicolor.black             };              var savebutton = new uibutton () {                 frame = new coregraphics.cgrect (10, 340, 120, 45)               };              savebutton.settitle ("save note", uicontrolstate.normal);             savebutton.backgroundcolor = uicolor.fromrgb (255, 0, 255);             savebutton.settitlecolor (uicolor.white, uicontrolstate.normal);              this.view.add (titleentrybox);             this.view.add (descriptionlabel);             this.view.add (descriptionentrybox);             this.view.add (savebutton);               savebutton.touchupinside += (sender, e) => {                 if (titleentrybox.text.length < 4)                     return;                 var notetosave = new note () {                     title = titleentrybox.text,                     description = descriptionentrybox.text,                     datecreated = datetime.now                 };                 database.insertnote (notetosave);                 titleentrybox.text = "";                 descriptionentrybox.text = "";             };         }     } } 

there lots of different ways approach problem this; simple example - passing null controller cause "add", otherwise act "edit" page

public class noteviewcontroller : uiviewcontroller {     note note;      public noteviewcontroller (note _note)     {         note = _note;     }      public override void viewdidload ()     {         base.viewdidload ();         this.navigationcontroller.navigationbar.bartintcolor = uicolor.fromrgb (255, 0, 255); // repeated code. used in mainviewcontroller.cs          this.title = note == null ? "add note" : "edit note";          this.navigationcontroller.navigationbar.titletextattributes = new uistringattributes () { foregroundcolor = uicolor.white };          this.navigationcontroller.navigationbar.tintcolor = uicolor.white;         this.view.backgroundcolor = uicolor.white;           var titleentrybox = new uitextfield () {             frame = new coregraphics.cgrect (0, 100, view.bounds.width, 45), // repeated code             backgroundcolor = uicolor.lightgray,             textcolor = uicolor.black,             text = note == null ? string.empty : note.title         };          var descriptionlabel = new uilabel () {             frame = new coregraphics.cgrect (10, 180, 250, 35),             text = "description",         };          var descriptionentrybox = new uitextview () {             frame = new coregraphics.cgrect (0, 220, view.bounds.width, 100), // repeated code             backgroundcolor = uicolor.lightgray,             textcolor = uicolor.black,             text = note == null ? string.empty : note.description         };          var button = new uibutton () {             frame = new coregraphics.cgrect (10, 340, 120, 45)           }; // repeated code          if (note == null) {           button.settitle ("add", uicontrolstate.normal);         } else {           button.settitle ("update", uicontrolstate.normal);         }          button.backgroundcolor = uicolor.fromrgb (255, 0, 255);         button.settitlecolor (uicolor.white, uicontrolstate.normal);          this.view.add (titleentrybox);         this.view.add (descriptionlabel);         this.view.add (descriptionentrybox);         this.view.add (button);           button.touchupinside += (sender, e) => {              if (note == null) {             var notetosave = new note () {                 title = titleentrybox.text,                 description = descriptionentrybox.text,                 datecreated = datetime.now             };             database.insertnote (notetosave);             titleentrybox.text = "";             descriptionentrybox.text = "";              } else {             if (titleentrybox.text.length < 4)                 return;             var notetoupdate = new note () {                 id = note.id,                 title = titleentrybox.text,                 description = descriptionentrybox.text,                 datecreated = datetime.now             };             database.updatenote (notetoupdate);             this.navigationcontroller.popviewcontroller (true);             }           };     } } 

Comments