Advertisement
Kapshtag

jebiemnietauczelnia

Nov 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.18 KB | None | 0 0
  1. using Android.App;
  2. using Android.OS;
  3. using Android.Support.V7.App;
  4. using Android.Runtime;
  5. using Android.Widget;
  6. using Xamarin.Essentials;
  7. using System;
  8. namespace notepad
  9. {
  10.     [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
  11.     public class MainActivity : AppCompatActivity
  12.     {
  13.         protected override void OnCreate(Bundle savedInstanceState)
  14.         {
  15.            
  16.             base.OnCreate(savedInstanceState);
  17.             Xamarin.Essentials.Platform.Init(this, savedInstanceState);//inicjalizacja "secure storage"
  18.             SetContentView(Resource.Layout.activity_main);
  19.             Button log = FindViewById<Button>(Resource.Id.logButton);
  20.             Button reg = FindViewById<Button>(Resource.Id.registerBtn);
  21.             EditText pass = FindViewById<EditText>(Resource.Id.passBox);
  22.  
  23.             log.Click += (o, e) =>//handlowanie przycisku logowania
  24.             {
  25.                 Toast.MakeText(this, "Loguję", ToastLength.Short).Show();//toast odpowiada za pokazywania komunikatów w dymkach
  26.                 if (pass.Text != "" && pass.Text.Length >= 8)
  27.                 {
  28.                     login(pass);
  29.                 }
  30.                 else
  31.                 {
  32.                     Toast.MakeText(this, "Notatka zostanie usnięta.", ToastLength.Short).Show();
  33.                 }
  34.             };
  35.             reg.Click += (o, e) =>//handle przycisku rejestracji
  36.             {
  37.                 register(pass);
  38.             };
  39.             EditText pass1 = FindViewById<EditText>(Resource.Id.editText2);
  40.             EditText pass2 = FindViewById<EditText>(Resource.Id.editText3);
  41.  
  42.             Button save = FindViewById<Button>(Resource.Id.save_pass);
  43.             save.Click += (o, e) =>//handle przycisku rejestracji
  44.             {
  45.                 Toast.MakeText(this, "Kliknięto", ToastLength.Short).Show();
  46.                 savePass(pass1, pass2);
  47.             };
  48.  
  49.             EditText edittext = FindViewById<EditText>(Resource.Id.editText1);
  50.  
  51.             Button logoutbtn = FindViewById<Button>(Resource.Id.signoutBut);
  52.             logoutbtn.Click += (o, e) =>//handle przycisku rejestracji
  53.             {
  54.                 logout(edittext);
  55.             };
  56.         }
  57.         private async void register(EditText ed)
  58.         {
  59.             try
  60.             {
  61.                 var _pass = await SecureStorage.GetAsync("pass");
  62.                 if (_pass == null) SetContentView(Resource.Layout.layout3);
  63.             }
  64.             catch (Exception ex)
  65.             {
  66.                 // nie wspiera secure storage
  67.                 Toast.MakeText(this, "Aplikacja nie ma wsparcia dla Twojej wersji Androida" + ex, ToastLength.Short).Show();
  68.             }
  69.         }
  70.         private async void login(EditText ed)
  71.         {
  72.             try
  73.             {
  74.                 var t = await SecureStorage.GetAsync("tries");
  75.                 int t_ = Convert.ToInt32(t);
  76.                 if (t_ >= 1)
  77.                 {
  78.                     var _pass = await SecureStorage.GetAsync("pass");
  79.                     if (ed.Text == _pass) SetContentView(Resource.Layout.layout2);
  80.                     else
  81.                     {
  82.                         t_--;
  83.                         await SecureStorage.SetAsync("tries", t);
  84.                     }
  85.                 }
  86.                 else
  87.                 {
  88.                     Toast.MakeText(this, "Przykro mi, ale wykorzystałeś już swoje szanse. Notatka zostanie usunięta.", ToastLength.Short).Show();
  89.                 }
  90.             }
  91.             catch (Exception ex)
  92.             {
  93.                 // nie wspiera secure storage
  94.                 Toast.MakeText(this, "Aplikacja nie ma wsparcia dla Twojej wersji Androida" + ex, ToastLength.Short).Show();
  95.             }
  96.         }
  97.         private async void savePass(EditText ed, EditText ed2)
  98.         {
  99.             try
  100.             {
  101.                 if (ed.Text == ed2.Text)
  102.                 {
  103.                     if (ed.Text.Length >= 8)
  104.                     {
  105.                         await SecureStorage.SetAsync("pass", ed.Text);
  106.                         Toast.MakeText(this, "Zapisano", ToastLength.Short).Show();
  107.                         SetContentView(Resource.Layout.activity_main);
  108.                     }
  109.                     else
  110.                     {
  111.                         Toast.MakeText(this, "Hasło za krótkie", ToastLength.Short).Show();
  112.                     }
  113.                 }
  114.                 else
  115.                 {
  116.                     Toast.MakeText(this, "Hasła nie są takie same", ToastLength.Short).Show();
  117.                 }
  118.                 Toast.MakeText(this, "Zapisano", ToastLength.Short).Show();
  119.             }
  120.             catch (Exception ex)
  121.             {
  122.                 // nie wspiera secure storage
  123.                 Toast.MakeText(this, "Aplikacja nie ma wsparcia dla Twojej wersji Androida" + ex, ToastLength.Short).Show();
  124.             }
  125.         }
  126.         private async void logout(EditText ed)
  127.         {
  128.             try
  129.             {
  130.                 await SecureStorage.SetAsync("note", ed.Text);
  131.                 SetContentView(Resource.Layout.activity_main);
  132.             }
  133.             catch (Exception ex)
  134.             {
  135.                 // nie wspiera secure storage
  136.                 Toast.MakeText(this, "Aplikacja nie ma wsparcia dla Twojej wersji Androida" + ex, ToastLength.Short).Show();
  137.             }
  138.         }
  139.         private async void retrieve(EditText ed)
  140.         {
  141.             try
  142.             {
  143.                 var ret = await SecureStorage.GetAsync("note");
  144.                 ed.Text = ret;
  145.             }
  146.             catch (Exception ex)
  147.             {
  148.                 // nie wspiera secure storage
  149.                 Toast.MakeText(this, "Aplikacja nie ma wsparcia dla Twojej wersji Androida" + ex, ToastLength.Short).Show();
  150.             }
  151.         }
  152.         public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
  153.         {
  154.             Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  155.  
  156.             base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement