From e1b6a2d279274819bdb06ad1499a51e4b210f44f Mon Sep 17 00:00:00 2001 From: stubbfel Date: Wed, 18 Sep 2013 16:21:46 +0200 Subject: [PATCH] add qr and nfc scan to placeNewspage --- .../CampusAppWP8/Model/GeoDb/SpsModel.cs | 6 + .../CampusAppWP8/Pages/Dev/QRScanner.xaml.cs | 18 ++- .../Pages/PlaceNews/PlaceNews.xaml.cs | 139 +++++++++++++++--- .../Resources/Constants.Designer.cs | 9 ++ .../CampusAppWP8/Resources/Constants.resx | 3 + 5 files changed, 150 insertions(+), 25 deletions(-) diff --git a/CampusAppWP8/CampusAppWP8/Model/GeoDb/SpsModel.cs b/CampusAppWP8/CampusAppWP8/Model/GeoDb/SpsModel.cs index b51fb54c..4982eeb3 100644 --- a/CampusAppWP8/CampusAppWP8/Model/GeoDb/SpsModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/GeoDb/SpsModel.cs @@ -169,6 +169,12 @@ namespace CampusAppWP8.Model.GeoDb public List FilterByPid(List pidList) { List fitlerList = new List(); + + if (pidList == null || pidList.Count < 1) + { + return fitlerList; + } + foreach (PlaceModel place in this.Places) { if (pidList.Contains(place.PlaceId)) diff --git a/CampusAppWP8/CampusAppWP8/Pages/Dev/QRScanner.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Dev/QRScanner.xaml.cs index a502d9fc..76f8bd73 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Dev/QRScanner.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Dev/QRScanner.xaml.cs @@ -48,6 +48,8 @@ namespace CampusAppWP8.Pages.Dev /// true if this object is in autofocus. private bool isInAutofocus = false; + private string ResultAppStoreKey; + #endregion #region Constructor @@ -73,6 +75,10 @@ namespace CampusAppWP8.Pages.Dev { if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true) { + if (NavigationContext.QueryString.ContainsKey(Constants.ParamQRResultKey)) + { + this.ResultAppStoreKey = NavigationContext.QueryString[Constants.ParamQRResultKey]; + } this.cam = new PhotoCamera(CameraType.Primary); this.cam.Initialized += new EventHandler(this.Cam_Initialized); this.cam.AutoFocusCompleted += new EventHandler(this.Cam_AutoFocusCompl); @@ -85,7 +91,7 @@ namespace CampusAppWP8.Pages.Dev { this.Dispatcher.BeginInvoke(delegate { - this.scannText.Text = AppResources.PrimCamNotSupported; + MessageBox.Show(AppResources.PrimCamNotSupported); }); } } @@ -211,7 +217,15 @@ namespace CampusAppWP8.Pages.Dev { Dispatcher.BeginInvoke(delegate { - this.scannText.Text = result.Text; + if (this.ResultAppStoreKey != null) + { + App.SaveToIsolatedStorage(this.ResultAppStoreKey, result.Text); + NavigationService.GoBack(); + } + else + { + MessageBox.Show(result.Text); + } }); } } diff --git a/CampusAppWP8/CampusAppWP8/Pages/PlaceNews/PlaceNews.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/PlaceNews/PlaceNews.xaml.cs index f0e1e317..aa1574a9 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/PlaceNews/PlaceNews.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/PlaceNews/PlaceNews.xaml.cs @@ -22,6 +22,9 @@ namespace CampusAppWP8.Pages.PlaceNews using CampusAppWP8.Utility; using CampusAppWP8.Utility.Lui.MessageBoxes; using Microsoft.Phone.Controls; + using Windows.Networking.Proximity; + using CampusAppWP8.Utility.NDEF; + using System.Runtime.InteropServices.WindowsRuntime; /// Place news. /// Stubbfel, 09.09.2013. @@ -49,6 +52,11 @@ namespace CampusAppWP8.Pages.PlaceNews /// List of search pids. private List searchPidList; + private bool qrScan = false; + + /// The device. + private readonly ProximityDevice device = ProximityDevice.GetDefault(); + #endregion #region Constructor @@ -74,11 +82,27 @@ namespace CampusAppWP8.Pages.PlaceNews { base.OnNavigatedTo(e); - if (NavigationMode.Back == e.NavigationMode && this.places == null) + if (NavigationMode.Back == e.NavigationMode) { - this.places = new PlacesFile(); - this.places.Model = App.LoadFromIsolatedStorage(Constants.IsolatedStorage_AllPlaces); - this.SetupResultBox(); + if (this.places == null) + { + this.places = new PlacesFile(); + this.places.Model = App.LoadFromIsolatedStorage(Constants.IsolatedStorage_AllPlaces); + } + + if (qrScan) + { + qrScan = false; + string qrResult = App.LoadFromIsolatedStorage("PlaceNewsQCCode"); + App.SaveToIsolatedStorage("PlaceNewsQCCode", null); + this.searchPidList = new List() { qrResult.Split('\n')[0].Trim() }; + this.SendGetPisPssForPlacenews(this.searchPidList); + } + else + { + this.SetupResultBox(); + } + } else { @@ -120,22 +144,24 @@ namespace CampusAppWP8.Pages.PlaceNews this.places.OnFailedLoad += new PlacesFile.OnFailed(this.PlacesFileIsFail); this.places.LoadData(); } - + // init sps Api - if (this.spsApi == null || this.forceRequest) + if (this.spsApi == null) { this.spsApi = new SpsApi(); this.spsApi.OnLoaded += new SpsApi.OnIO(this.SpsApiIsReady); this.spsApi.OnFailedLoad += new SpsApi.OnFailed(this.ApiIsFail); this.spsApi.SetupCurrentPlaceRequest(Constants.SpsDomain_Buildings); - if (this.forceRequest) - { - this.spsApi.LoadData(); - } + + } + + if (this.forceRequest) + { + this.spsApi.LoadData(); } // init pis API - if (this.pisApi == null || this.forceRequest) + if (this.pisApi == null) { this.pisApi = new PisApi(); this.pisApi.OnLoaded += new PisApi.OnIO(this.PisApiIsReady); @@ -143,7 +169,7 @@ namespace CampusAppWP8.Pages.PlaceNews } // init pss Api - if (this.pssApi == null || this.forceRequest) + if (this.pssApi == null) { this.pssApi = new PssApi(); this.pssApi.OnLoaded += new PssApi.OnIO(this.PssApiIsReady); @@ -174,8 +200,8 @@ namespace CampusAppWP8.Pages.PlaceNews this.spsApi.OnLoaded += new SpsApi.OnIO(this.SpsApiIsReady); this.spsApi.OnFailedLoad += new SpsApi.OnFailed(this.ApiIsFail); this.spsApi.SetupCurrentPlaceRequest(Constants.SpsDomain_Buildings); - } - + } + this.spsApi.LoadData(); this.waitForApi++; } @@ -232,29 +258,59 @@ namespace CampusAppWP8.Pages.PlaceNews private void SpsApiIsReady() { this.waitForApi--; - this.places.Model.AddPlaces(this.spsApi.Model.Places.ToList()); - - this.searchPidList = this.spsApi.Model.CreatePidList(); + if (spsApi.Model.Places.Count > 0) + { + this.places.Model.AddPlaces(this.spsApi.Model.Places.ToList()); + this.searchPidList = this.spsApi.Model.CreatePidList(); + this.SendGetPisPssForPlacenews(this.searchPidList); + } + this.CheckedSetupResultBox(); + } + + /// + /// send get request to pis andd pss for PlaceNews service + /// + /// list of place id + private void SendGetPisPssForPlacenews(List pidList) { List infoNames = new List() { Constants.PisInformationName_Name }; List serviceNames = new List() { Constants.PssServiceName_PlaceNews }; // load from pis api - if (this.forceRequest || !this.places.Model.ContainsInformationNames(this.searchPidList, infoNames)) + this.SendGetPlaceInformation(this.searchPidList, infoNames, this.forceRequest); + + // load from pis api + this.SendGetPlaceService(this.searchPidList, serviceNames); + } + /// + /// Method send get for PlaceInformation of certain places + /// + /// list of place id + /// list of informations name + /// if its true then force api load + private void SendGetPlaceInformation(List pidList, List infoNames, bool force = false) + { + if (force || !this.places.Model.ContainsInformationNames(pidList, infoNames)) { this.pisApi.SetupInformationRequest(this.searchPidList, infoNames); this.pisApi.LoadData(); this.waitForApi++; } - - // load from pis api - if (this.forceRequest || !this.places.Model.ContainsServiceNames(this.searchPidList, serviceNames)) + } + + /// + /// Method send get for PlaceSetvice of certain places + /// + /// list of place id + /// list of service name + /// if its true then force api load + private void SendGetPlaceService(List pidList, List serviceNames, bool force = false) + { + if (this.forceRequest || !this.places.Model.ContainsServiceNames(pidList, serviceNames)) { this.pssApi.SetupServiceRequest(this.searchPidList, serviceNames); this.pssApi.LoadData(); this.waitForApi++; } - - this.CheckedSetupResultBox(); } /// Sets up the result box. @@ -294,6 +350,11 @@ namespace CampusAppWP8.Pages.PlaceNews /// Event information. private void ApplicationBarMenuItem_Click(object sender, EventArgs e) { + this.qrScan = true; + string urlString = "/Pages/Dev/QRScanner.xaml"; + urlString += "?" + Constants.ParamQRResultKey + "=" + "PlaceNewsQCCode"; + Uri url = new Uri(urlString as string, UriKind.Relative); + this.NavigationService.Navigate(url); } /// Event handler. Called by ApplicationBarMenuItem_Click for 1 events. @@ -302,6 +363,7 @@ namespace CampusAppWP8.Pages.PlaceNews /// Event information. private void ApplicationBarMenuItem_Click_1(object sender, EventArgs e) { + this.device.SubscribeForMessage("NDEF", this.NDEFHandler); } /// Checked setup result box. @@ -346,6 +408,37 @@ namespace CampusAppWP8.Pages.PlaceNews txt.Text = place.GetInformationsValue(Constants.PisInformationName_Name); } + + /// Handler, called when the ndef. + /// Stubbfel, 22.08.2013. + /// The sender. + /// The message. + private void NDEFHandler(ProximityDevice sender, ProximityMessage message) + { + this.device.StopSubscribingForMessage(message.SubscriptionId); + var ndefMessage = message.Data; + byte[] data = ndefMessage.ToArray(); + + NDEFMessage ndef = new NDEFMessage(data); + string nfcContent = ndef.GetContent(); + nfcContent = nfcContent.Trim('{'); + nfcContent = nfcContent.Trim('}'); + string[] items = nfcContent.Split(','); + string pid = "0"; + foreach (string item in items) + { + if (item.StartsWith("\"pid\":\"")) + { + pid = item; + pid = pid.TrimStart("\"pid\":\"".ToCharArray()); + pid = pid.TrimEnd('\"'); + break; + } + } + this.searchPidList = new List() { pid }; + this.SendGetPisPssForPlacenews(this.searchPidList); + // this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(ndef.GetContent()))); + } #endregion } } \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/Resources/Constants.Designer.cs b/CampusAppWP8/CampusAppWP8/Resources/Constants.Designer.cs index 8bb7086c..0f3c3b91 100644 --- a/CampusAppWP8/CampusAppWP8/Resources/Constants.Designer.cs +++ b/CampusAppWP8/CampusAppWP8/Resources/Constants.Designer.cs @@ -654,6 +654,15 @@ namespace CampusAppWP8.Resources { } } + /// + /// Sucht eine lokalisierte Zeichenfolge, die QRResultKey ähnelt. + /// + public static string ParamQRResultKey { + get { + return ResourceManager.GetString("ParamQRResultKey", resourceCulture); + } + } + /// /// Sucht eine lokalisierte Zeichenfolge, die Url ähnelt. /// diff --git a/CampusAppWP8/CampusAppWP8/Resources/Constants.resx b/CampusAppWP8/CampusAppWP8/Resources/Constants.resx index 2e17a5d2..675e0e6b 100644 --- a/CampusAppWP8/CampusAppWP8/Resources/Constants.resx +++ b/CampusAppWP8/CampusAppWP8/Resources/Constants.resx @@ -513,4 +513,7 @@ /Pages/Events/EventPage.xaml + + QRResultKey + \ No newline at end of file