Newer
Older
CosmosClient / app / src / main / java / com / example / cosmosclient / services / NotificationDone.java
package com.example.cosmosclient.services;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.example.cosmosclient.app.Cosmos;
import com.example.cosmosclient.entities.AddRequestsResponse;
import com.example.cosmosclient.entities.Group;
import com.example.cosmosclient.entities.Request;
import com.example.cosmosclient.resources.GroupsRest;

import java.io.IOException;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class NotificationDone extends BroadcastReceiver {
    //retrofitの処理
    final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/cosmos/rest/")
            //            .baseUrl("http://10.0.2.2:8080/rest/")
            .addConverterFactory(JacksonConverterFactory.create())
            .build();
    //interfaceから実装を取得
    final GroupsRest requestsService = retrofit.create(GroupsRest.class);

    @Override
    public void onReceive(Context context, Intent intent) {
        Cosmos app = (Cosmos) context.getApplicationContext();
        String gId = intent.getExtras().getString("gId");
        Request editRequest = app.getGroup(gId).getRequestList().getRequestById(intent.getExtras().getString("rId"));
        if(gId == null){
            System.out.println("error");
            return;
        }
        if(editRequest == null){
            System.out.println("cannot find Request");
            return;
        }
        doneRequest(gId, editRequest);
    }

    //引数で受け取ったリクエストを達成する
    public void doneRequest(final String gId, final Request request){


        new Thread(new Runnable() {
            public void run() {
                String deadline = null;
                //Date型のフォーマット設定
                if (request.getDeadline() != null) {
                    deadline = String.format("%d-%02d-%02d %02d:%02d:%02d", request.getDeadline().getYear(), request.getDeadline().getMonth() + 1, request.getDeadline().getDate() + 1, 23, 59, 59);
                }               final Call<AddRequestsResponse> updateRequestCall = requestsService.updateRequest(gId, request.getrId(), request.getIssuer().getuId(), request.getProduct(), deadline, request.getLocation(), true, "token");
                Response<AddRequestsResponse> response;
                try {
                    response = updateRequestCall.execute();
                    if (response.isSuccessful()) {
                        AddRequestsResponse result = response.body();
//                Toast.makeText(RequestListActivity.this, "RequestListを達成しました", Toast.LENGTH_SHORT).show();
//                requestList.removeRequests(selectedRequestList);
//                handler.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        ResetRequestListTable();
//                        AddRequestListTable(requestList);
//                    }
//                });
                    } else {
                        // onFailure
                        try {
                            System.out.println(response.errorBody().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        //onFailureでキャッチできないエラーの処理
//                Toast.makeText(RequestListActivity.this, "通信エラー", Toast.LENGTH_SHORT).show();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
//                Toast.makeText(RequestListActivity.this, "RequestListの達成失敗しました", Toast.LENGTH_SHORT).show();

                }
            }
        }).start();

    }
}