//買う商品の情報オブジェクトBought_thingsを受け取ってJson化してPost後、 //サーバーから帰ってくる[次にこの商品を買うべき日付]をオブジェクト化して変えす //public async Task<List<Next_buy_date>> PostBoughtThingsInfo(Bought_things bt) public async Task <Next_buy_date> PostBoughtThingInfo(Bought_thing bt) { string serverUrl = ServerInfo.url; string APIUrl = "/bought_things"; string reqUrl = $"{serverUrl}{APIUrl}"; string jsonString = JsonConvert.SerializeObject(bt); //HttpClient hc = new HttpClient(); WrappedHttpClient whc = new WrappedHttpClient(); var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); //HttpResponseMessage response = await hc.PostAsync(reqUrl, content); HttpResponseMessage response = await whc.PostAsync(reqUrl, content); string result = await response.Content.ReadAsStringAsync(); Next_buy_date NBD = JsonConvert.DeserializeObject <Next_buy_date>(result); return(NBD); }
void RegistBtnClicked(object sender, EventArgs s) { Bought_thing bt = new Bought_thing(); User userInfo = (User)Application.Current.Properties["user"]; bt.user_id = userInfo.id; //リストから選択してもらった物の数とthing_idを取得 bt.num = 1; if (chosenNum != -1) { bt.thing_id = BuyThingList[chosenNum].thing_id; //登録に必要な情報を渡す Navigation.PushAsync(new ConcernBuyThingPage(bt), true); } else //chosenNum == -1 なので選択されていない { DependencyService.Get <IMyFormsToast>().Show("商品を選択してください"); } }
//Listを取得してセットする処理を書く async Task setBuyList() { User user = (User)Application.Current.Properties["user"]; int userId = user.id; GetObjects go = new GetObjects(); string jsonString = await go.GetBuythingInfo(userId); List <Buy_thing> buythingInfo = go.GetbuythingObjectFromJson(jsonString); Dictionary <string, string> item = new Dictionary <string, string>(); var cell = new DataTemplate(typeof(ImageCell)); cell.SetBinding(ImageCell.TextProperty, "Key"); cell.SetBinding(ImageCell.DetailProperty, "Value"); var listView = new ListView { IsPullToRefreshEnabled = true, ItemsSource = item, ItemTemplate = cell }; var addbutton = new Button { HorizontalOptions = LayoutOptions.FillAndExpand, Text = "+", FontSize = 30, BackgroundColor = Color.OrangeRed, TextColor = Color.White, Command = new Command(async() => { string nextPageResult = await DisplayActionSheet("追加方法を選ぶ", "閉じる", "キャンセル", new string[] { "リストから", "スキャンする" }); if (nextPageResult == "リストから") {//リストから選ぶページを表示 await Navigation.PushAsync(new ChoiceFromBoughtThingPage(), true); } else if (nextPageResult == "スキャンする") {//スキャンするページを表示 //await Navigation.PushAsync(new ScanBuyThingPage(), true); //ZXingのスキャナーを呼ぶ string scanedJancode = ""; var scanPage = new ZXingScannerPage() { DefaultOverlayTopText = "バーコードを読み取ります", DefaultOverlayBottomText = "", }; await Navigation.PushAsync(scanPage); scanPage.OnScanResult += (result) => { //scanPage.OnScanResult += (result) => { scanPage.IsScanning = false; Device.BeginInvokeOnMainThread(async() => { scanedJancode = result.Text; await Navigation.PopAsync(); DependencyService.Get <IMyFormsToast>().Show("Jancode: " + scanedJancode + "で問い合わせ中"); //GetObjects go = new GetObjects(); string json = await go.GetItemJsonString(scanedJancode); if (scanedJancode != null) {//jsonの内容をチェック //jancodeを元に情報を作成 SearchedInfo thingInfo = go.GetItemObjectFromJson(json); Bought_thing bt = new Bought_thing(); User userInfo = (User)Application.Current.Properties["user"]; bt.user_id = userInfo.id; //数は最後に入れてもらうのでとりあえず1を入れる bt.num = 1; bt.thing_id = thingInfo.Id; //登録に必要な情報を渡す await Navigation.PushAsync(new ConcernBuyThingPage(bt), true); } else {//jancode is null DependencyService.Get <IMyFormsToast>().Show("サーバにデータがありません"); //手入力画面に移行する.手入力ページが未実装なのでコメントアウト //await Navigation.PushAsync(new ManualInputBuyThingPage(), true); } }); }; } else {//閉じるかキャンセルが押されているので何もしない(留まる) DependencyService.Get <IMyFormsToast>().Show("追加をキャンセルします."); } }) }; listView.Refreshing += (sender, e) => { setBuyList(); listView.IsRefreshing = false; }; var RefreshList = new Button { HorizontalOptions = LayoutOptions.FillAndExpand, Text = "更新", FontSize = 20, BackgroundColor = Color.Tan, TextColor = Color.White, Command = new Command(() => { setBuyList(); }) }; for (int n = 0; n < buythingInfo.Count; n++) { item[buythingInfo[n].name] = buythingInfo[n].alias; //item.Add(expendablesInfo[n].name, expendablesInfo[n].limit); //await DisplayAlert("商品名", expendablesInfo[n].name, "OK"); //await DisplayAlert("次回購入予定日", expendablesInfo[n].limit, "OK"); } Content = new StackLayout { Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0), // iOSのみ上部にマージンをとる Children = { new StackLayout { Orientation = StackOrientation.Horizontal, Children = { RefreshList, addbutton } }, listView } }; DependencyService.Get <IMyFormsToast>().Show("リストを更新しました!"); }
public ConcernBuyThingPage(Bought_thing bt) { InitializeComponent(); this.bt = bt; }
async void OkBtnClicked(object sender, EventArgs s) { //サーバーに登録情報を送る //購入品情報を作成 //userIdはLoginが実装でき次第書き換える //userIdはとりあえず固定 User user = (User)Application.Current.Properties["user"]; int userId = user.id; Bought_thing bt = new Bought_thing(); //書き換える bt.user_id = userId; bt.thing_id = item.Id; bt.num = itemNum; PostJson pj = new PostJson(); //Postして購入済みリストに追加、次の購入日を取得 Next_buy_date nextBuyDate = await pj.PostBoughtThingInfo(bt); //1990-01-01だった場合 //nextBuyDate.next_buy_dateはstring型なのでDateTimeに変換する DateTime defaultDT = new DateTime(1990, 1, 1); DateTime inputDateTime = defaultDT; DateTime nextBuyDT; DateTime nextBuyDTymd; string sendDTstring = nextBuyDate.next_buy_date; //DateTime inputDate; //defaultDTでプロパティを初期化 Application.Current.Properties["inputDateTime"] = inputDateTime; if (DateTime.TryParse(nextBuyDate.next_buy_date, out nextBuyDT)) { nextBuyDTymd = new DateTime(nextBuyDT.Year, nextBuyDT.Month, nextBuyDT.Day); if (nextBuyDTymd.CompareTo(defaultDT) == 0) //サーバから帰ってきた日付が1990-01-01 //入力してもらう { await Navigation.PushAsync(new DateTimeInputPage(), true); inputDateTime = (DateTime)Application.Current.Properties["inputDateTime"]; //DependencyService.Get<IMyFormsToast>().Show("入力した日付: " + inputDateTime.ToString()); if (inputDateTime.CompareTo(defaultDT) == 0) //1990-01-01なので入力してくれてない { DependencyService.Get <IMyFormsToast>().Show("もう一度日付入力してください"); return; } else { sendDTstring = inputDateTime.ToString("yyyy-MM-dd"); } }//1990-01-01ではないので通常通り続ける //DependencyService.Get<IMyFormsToast>().Show("次の購入日を取得:" + nextBuyDate.next_buy_date); //消耗品リスト作成 Bought_expendable be = new Bought_expendable(); //書き換える be.user_id = userId; be.thing_id = item.Id; be.limit = sendDTstring; //Todo:消す DependencyService.Get <IMyFormsToast>().Show("次のlimitの日付: " + inputDateTime.ToString("yyyy-MM-dd")); //be.limit = nextBuyDate.next_buy_date; //Postして消耗品リストに登録 Expendables postedEx = await pj.PostExpendablesInfo(be); //DependencyService.Get<IMyFormsToast>().Show("消耗品リストに登録しました: " + postedEx.created_at); //登録完了したので完了したことを伝えるページに遷移 //shopName(店名)、item.Name(商品名)、bt(bt.numで個数)、nextBuyDate(limit) //Page cbtPage = new CompleteBoughtThingPage(shopName, item, bt, nextBuyDate); //await Navigation.PushAsync(cbtPage, true); await Navigation.PushAsync(new CompleteBoughtThingPage(shopName, item, bt, nextBuyDate), true); } else //帰ってきた日付が変換できないエラー { DependencyService.Get <IMyFormsToast>().Show("帰ってきた日付を変換できません:" + nextBuyDate.next_buy_date); //Todo:BoughtListPageに戻る処理 //Navigation.RemovePage(); } }