public void OnClick(View v)
 {
     if (v.Id == Resource.Id.dashboard_user_profile)
     {
         StartActivity(new Android.Content.Intent(this, typeof(ProfileActivity)));
     }
     else if (v.Id == Resource.Id.dashboard_btn_sync)
     {
         var reachability = new Reachability.Net.XamarinAndroid.Reachability();//check network
         if (reachability.IsHostReachable(ReachableHost))
         {
             dataHandler = new OfflineHandler();
             dataHandler.Sync();
             Toast.MakeText(this, "Synced Successfully", ToastLength.Short).Show();
         }
         else
         {
             Toast.MakeText(this, "Device is Offline", ToastLength.Short).Show();
         }
     }
     else if (v.Id == Resource.Id.dashboard_btn_logout)
     {
         LogoutUser();
     }
 }
        private void SaveUserDetail(string firstname, string lastname, string city, string phonenumber)
        {
            User currentUser = db.SelectSingleUser(AppData.LoggedInUser)[0];

            currentUser.FirstName   = firstname;
            currentUser.LastName    = lastname;
            currentUser.City        = city;
            currentUser.PhoneNumber = phonenumber;
            currentUser.UpdatedAt   = DateTime.Now;
            int updatedRows = db.UpdateUserTable(currentUser);

            if (updatedRows == 1)
            {
                User updatedUser  = db.SelectSingleUser(AppData.LoggedInUser)[0];
                var  reachability = new Reachability.Net.XamarinAndroid.Reachability();//check network
                if (reachability.IsHostReachable(ReachableHost))
                {
                    updatedUser.FirebaseUpdated = 1;
                    var firebase = new FirebaseClient(FirebaseURL);
                    var fbResult = firebase.Child(FirebaseUserChild).Child(updatedUser.FirebaseReference).PatchAsync(updatedUser);
                }
                else
                {
                    updatedUser.FirebaseUpdated = 0;
                }
                db.UpdateUserTable(updatedUser);// record that the firebase database was not updated
                Toast.MakeText(this, "Profile Updated.", ToastLength.Short).Show();
            }
            else
            {
                Toast.MakeText(this, "Profile Update Failed. ", ToastLength.Short).Show();
            }
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

//			var reachability = new Reachability.Net.XamarinIOS.Reachability();

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button   button = FindViewById <Button> (Resource.Id.myButton);
            TextView label  = FindViewById <TextView>(Resource.Id.connectionStatusLabel);

            button.Click += delegate {
                var reachability     = new Reachability.Net.XamarinAndroid.Reachability(this, "http://www.bing.com");
                var isConnected      = reachability.IsHostReachable("www.google.com");
                var wifiStatus       = reachability.LocalWifiConnectionStatus();
                var mobileConnStatus = reachability.InternetConnectionStatus();

                var connectionDetails = "Connection Status: " + (isConnected ? "Connected - " : "Disconnected - " + System.Environment.NewLine);
                connectionDetails += "Wifi Status: " + (wifiStatus == NetworkStatus.ConnectedViaWifi ? "Connected - " : "Disconnected - " + System.Environment.NewLine);
                connectionDetails += "Mobile Status: " + (mobileConnStatus == NetworkStatus.ConnectedViaMobile ? "Connected" : "Disconnected");

                label.Text = connectionDetails;
            };
        }
        //user registration
        private async void SignUpUser(string email, string password)
        {
            if (ValidateSignUp(email, password))
            {
                password = AppData.EncryptPassword(password);
                User user = new User()
                {
                    Username  = email,
                    Password  = password,
                    CreatedAt = DateTime.Now
                };


                db.InsertIntoUserTable(user);
                User addedUser = db.SelectSingleUser(email)[0];

                var reachability = new Reachability.Net.XamarinAndroid.Reachability();//check network
                if (reachability.IsHostReachable(ReachableHost))
                {
                    var firebase    = new FirebaseClient(FirebaseURL);
                    var firebaseKey = (await firebase.Child(FirebaseUserChild).PostAsync <User>(addedUser)).Key;
                    user.FirebaseReference = firebaseKey.ToString();
                    db.UpdateUserTable(user);
                    auth.CreateUserWithEmailAndPassword(email, password);
                }
                Toast.MakeText(this, "You Registered Successfully ", ToastLength.Short).Show();
                StartActivity(new Android.Content.Intent(this, typeof(MainActivity)));
            }
        }
        private async void UploadImage()
        {
            var docs = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

            UserImage img = new UserImage();

            try
            {
                var destination = System.IO.Path.Combine(docs, UploadDirectory);      //adding uploads folder in the path to store images
                Directory.CreateDirectory(destination);                               //create uploads folder in specified path
                var absolutePath = GetRealPathFromURI(Application.Context, filePath); //convert uri to a path
                var fileName     = System.IO.Path.GetFileName(absolutePath);          //get the filename from the path
                var uploadImage  = System.IO.Path.Combine(destination, fileName);     //adding the filename to the destination path
                File.Copy(absolutePath, uploadImage);                                 //copying the file to the uploads folder

                img.Username  = AppData.LoggedInUser;
                img.ImageRef  = uploadImage;
                img.CreatedAt = DateTime.Now;
                db.InsertIntoUserImageTable(img); //insert data to userimage table
            }
            catch (Exception ex)
            {
                Console.WriteLine("Thrown exception is" + ex);
            }
            var reachability = new Reachability.Net.XamarinAndroid.Reachability();//check network

            if (reachability.IsHostReachable(ReachableHost))
            {
                String guid = Guid.NewGuid().ToString();            // generate a unique id

                var imagesref = storageRef.Child("images/" + guid); //guid assigns a new unique identifier to the image before storing in firebase
                if (filePath != null)
                {
                    imagesref.PutFile(filePath)//add image to firebase storage
                    .AddOnSuccessListener(this)
                    .AddOnFailureListener(this);
                    var firebase = new FirebaseClient(FirebaseURL); //add image reference to firebase database
                    img.ImageRef = imagesref.ToString();
                    var FirebaseReference = (await firebase.Child(FirebaseUserImageChild).PostAsync <UserImage>(img)).Key;
                    img.FirebaseReference = FirebaseReference;
                    db.UpdateUserImageTable(img);
                }
                else
                {
                    Toast.MakeText(this, "First choose an image", ToastLength.Short).Show();
                }
            }
            else
            {
                Toast.MakeText(this, "Uploaded Successfully", ToastLength.Short).Show();
            }
        }
示例#6
0
        private void LoginUser(string email, string password)
        {
            password             = AppData.EncryptPassword(password);
            AppData.LoggedInUser = email;
            var data         = db.SelectUserTable();                                                            //retrieve all users in the user table
            var userData     = data.Where(x => x.Username == email && x.Password == password).FirstOrDefault(); //getting the matching user
            var reachability = new Reachability.Net.XamarinAndroid.Reachability();                              //check network

            if (reachability.IsHostReachable(ReachableHost))
            {
                auth.SignInWithEmailAndPassword(email, password); //firebase auth signin
            }
            if (userData != null)
            {
                Toast.MakeText(this, "Login Success", ToastLength.Short).Show();//go to dashboard on succesful user login
                StartActivity(new Android.Content.Intent(this, typeof(Dashboard)));
            }
            else
            {
                Toast.MakeText(this, "Login Failed", ToastLength.Short).Show();
            }
        }