Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Android.App;
- using Android.OS;
- using Android.Support.V7.App;
- using Android.Runtime;
- using Android.Widget;
- using Xamarin.Essentials;
- using System;
- namespace notepad
- {
- [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
- public class MainActivity : AppCompatActivity
- {
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
- Xamarin.Essentials.Platform.Init(this, savedInstanceState);//inicjalizacja "secure storage"
- SetContentView(Resource.Layout.activity_main);
- Button log = FindViewById<Button>(Resource.Id.logButton);
- Button reg = FindViewById<Button>(Resource.Id.registerBtn);
- EditText pass = FindViewById<EditText>(Resource.Id.passBox);
- log.Click += (o, e) =>//handlowanie przycisku logowania
- {
- Toast.MakeText(this, "Loguję", ToastLength.Short).Show();//toast odpowiada za pokazywania komunikatów w dymkach
- if (pass.Text != "" && pass.Text.Length >= 8)
- {
- login(pass);
- }
- else
- {
- Toast.MakeText(this, "Notatka zostanie usnięta.", ToastLength.Short).Show();
- }
- };
- reg.Click += (o, e) =>//handle przycisku rejestracji
- {
- register(pass);
- };
- EditText pass1 = FindViewById<EditText>(Resource.Id.editText2);
- EditText pass2 = FindViewById<EditText>(Resource.Id.editText3);
- Button save = FindViewById<Button>(Resource.Id.save_pass);
- save.Click += (o, e) =>//handle przycisku rejestracji
- {
- Toast.MakeText(this, "Kliknięto", ToastLength.Short).Show();
- savePass(pass1, pass2);
- };
- EditText edittext = FindViewById<EditText>(Resource.Id.editText1);
- Button logoutbtn = FindViewById<Button>(Resource.Id.signoutBut);
- logoutbtn.Click += (o, e) =>//handle przycisku rejestracji
- {
- logout(edittext);
- };
- }
- private async void register(EditText ed)
- {
- try
- {
- var _pass = await SecureStorage.GetAsync("pass");
- if (_pass == null) SetContentView(Resource.Layout.layout3);
- }
- catch (Exception ex)
- {
- // nie wspiera secure storage
- Toast.MakeText(this, "Aplikacja nie ma wsparcia dla Twojej wersji Androida" + ex, ToastLength.Short).Show();
- }
- }
- private async void login(EditText ed)
- {
- try
- {
- var t = await SecureStorage.GetAsync("tries");
- int t_ = Convert.ToInt32(t);
- if (t_ >= 1)
- {
- var _pass = await SecureStorage.GetAsync("pass");
- if (ed.Text == _pass) SetContentView(Resource.Layout.layout2);
- else
- {
- t_--;
- await SecureStorage.SetAsync("tries", t);
- }
- }
- else
- {
- Toast.MakeText(this, "Przykro mi, ale wykorzystałeś już swoje szanse. Notatka zostanie usunięta.", ToastLength.Short).Show();
- }
- }
- catch (Exception ex)
- {
- // nie wspiera secure storage
- Toast.MakeText(this, "Aplikacja nie ma wsparcia dla Twojej wersji Androida" + ex, ToastLength.Short).Show();
- }
- }
- private async void savePass(EditText ed, EditText ed2)
- {
- try
- {
- if (ed.Text == ed2.Text)
- {
- if (ed.Text.Length >= 8)
- {
- await SecureStorage.SetAsync("pass", ed.Text);
- Toast.MakeText(this, "Zapisano", ToastLength.Short).Show();
- SetContentView(Resource.Layout.activity_main);
- }
- else
- {
- Toast.MakeText(this, "Hasło za krótkie", ToastLength.Short).Show();
- }
- }
- else
- {
- Toast.MakeText(this, "Hasła nie są takie same", ToastLength.Short).Show();
- }
- Toast.MakeText(this, "Zapisano", ToastLength.Short).Show();
- }
- catch (Exception ex)
- {
- // nie wspiera secure storage
- Toast.MakeText(this, "Aplikacja nie ma wsparcia dla Twojej wersji Androida" + ex, ToastLength.Short).Show();
- }
- }
- private async void logout(EditText ed)
- {
- try
- {
- await SecureStorage.SetAsync("note", ed.Text);
- SetContentView(Resource.Layout.activity_main);
- }
- catch (Exception ex)
- {
- // nie wspiera secure storage
- Toast.MakeText(this, "Aplikacja nie ma wsparcia dla Twojej wersji Androida" + ex, ToastLength.Short).Show();
- }
- }
- private async void retrieve(EditText ed)
- {
- try
- {
- var ret = await SecureStorage.GetAsync("note");
- ed.Text = ret;
- }
- catch (Exception ex)
- {
- // nie wspiera secure storage
- Toast.MakeText(this, "Aplikacja nie ma wsparcia dla Twojej wersji Androida" + ex, ToastLength.Short).Show();
- }
- }
- public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
- {
- Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement