/// <summary> /// Provides the data adapter for the RecyclerView /// This simple gets all the current tracking numbers and populates the recycler /// </summary> private void TrackingNumberDataProvider() { mBarcodeScannerList = new BarcodeScannerList(); mBarcodeScannerList.FetchUnCollected(); mRecyclerView = FindViewById <RecyclerView>(Resource.Id.recyclerView); ScrollView scrollView = FindViewById <ScrollView>(Resource.Id.scroll_view); // Plug in the linear layout manager: mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.Vertical, true); mLayoutManager.ScrollToPosition(0); mRecyclerView.SetLayoutManager(mLayoutManager); mRecyclerView.ScrollToPosition(0); // Plug in my adapter: mAdapter = new TrackingNumberDataAdapter(mBarcodeScannerList); mRecyclerView.SetAdapter(mAdapter); mRecyclerView.ScrollToPosition(0); }
protected override void OnCreate(Bundle savedInstanceState) { RequestedOrientation = ScreenOrientation.Portrait; Context applicationContext = Application.Context; AppPreferences applicationPreferences = new AppPreferences(applicationContext); // Check application Preferences have been saved previously if not open Settings Activity and wait there. if ( string.IsNullOrEmpty(applicationPreferences.GetAccessKey("submitDataUrl")) || string.IsNullOrEmpty(applicationPreferences.GetAccessKey("loadConfigUrl")) || string.IsNullOrEmpty(applicationPreferences.GetAccessKey("applicationKey")) || string.IsNullOrEmpty(applicationPreferences.GetAccessKey("retentionPeriod")) ) { // No, well start the setting activity StartActivity(typeof(SettingsActivity)); } base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_main); Android.Support.V7.Widget.Toolbar toolbar = FindViewById <Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); // We only want to create a batch number here once when the app first starts and not everytime the activity loads if (batch == Guid.Empty) { SetBatchNumber(false); } databasePath = System.IO.Path.Combine( System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "localscandata.db3"); databaseConnection = new SQLiteConnection(databasePath); // Create the ParcelScans table databaseConnection.CreateTable <DespatchBayExpressDataBase.ParcelScans>(); if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) == (int)Permission.Granted) { mediaPlayer = MediaPlayer.Create(this, Resource.Raw.beep_07); TrackingNumberDataProvider(); // We have permission, go ahead and use the GPS. Log.Debug("GPS", "We have permission, go ahead and use the GPS."); InitializeLocationManager(); coordinates = FindViewById <TextView>(Resource.Id.footer_text); TrackingScan = FindViewById <EditText>(Resource.Id.txtentry); TrackingScan.Text = ""; TrackingScan.RequestFocus(); TrackingScan.KeyPress += (object sender, View.KeyEventArgs e) => { if ((e.Event.Action == KeyEventActions.Down) && (e.KeyCode == Keycode.Enter)) { if (e.Event.RepeatCount == 0) { /// need to regex the scan against the Tracking Patterns /// TableQuery <TrackingNumberPatterns> trackingPatterns = databaseConnection.Table <TrackingNumberPatterns>(); bool patternFound = false; try { foreach (var trackingPattern in trackingPatterns) { Match m = Regex.Match(@TrackingScan.Text, @trackingPattern.Pattern, RegexOptions.IgnoreCase); if (m.Success) { patternFound = true; } } } catch { } if (patternFound) { var newScan = new DespatchBayExpressDataBase.ParcelScans { TrackingNumber = TrackingScan.Text.ToUpper(), ScanTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"), Batch = batchnumber, Sent = null }; try { newScan.Longitude = currentLocation.Longitude; } catch { newScan.Longitude = null; } try { newScan.Latitude = currentLocation.Latitude; } catch { newScan.Latitude = null; } try { databaseConnection.Insert(newScan); mBarcodeScannerList.FetchUnCollected(); mAdapter.NotifyDataSetChanged(); mRecyclerView.RefreshDrawableState(); mediaPlayer.Start(); } catch (SQLiteException ex) { Toast.MakeText(this, "Scan Error : Duplicated Barcode Scan", ToastLength.Long).Show(); Log.Info("SCANNER", "Scan Error : " + ex.Message); } } else { Toast.MakeText(this, "Barcode format not recognised", ToastLength.Short).Show(); } TrackingScan.RequestFocus(); TrackingScan.Text = ""; } } }; } else { // GPS permission is not granted. If necessary display rationale & request. Log.Debug("GPS", "GPS permission is not granted"); if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.AccessFineLocation)) { // Provide an additional rationale to the user if the permission was not granted // and the user would benefit from additional context for the use of the permission. // For example if the user has previously denied the permission. Log.Info("GPS", "Displaying GPS permission rationale to provide additional context."); var rootView = FindViewById <CoordinatorLayout>(Resource.Id.root_view); var requiredPermissions = new String[] { Manifest.Permission.AccessFineLocation }; ActivityCompat.RequestPermissions(this, requiredPermissions, REQUEST_LOCATION); } else { ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.AccessFineLocation }, REQUEST_LOCATION); } } }