106 lines
3.5 KiB
Java
106 lines
3.5 KiB
Java
package fr.ynerant.tarot.ui.home;
|
|
|
|
import android.os.Bundle;
|
|
import android.os.Looper;
|
|
import android.preference.PreferenceManager;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.Button;
|
|
import android.widget.EditText;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.fragment.app.Fragment;
|
|
import androidx.lifecycle.ViewModelProviders;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import java.io.IOException;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.HashMap;
|
|
import java.util.concurrent.Executors;
|
|
|
|
import fr.ynerant.tarot.Game;
|
|
import fr.ynerant.tarot.MainActivity;
|
|
import fr.ynerant.tarot.Player;
|
|
import fr.ynerant.tarot.R;
|
|
import fr.ynerant.tarot.ui.newgame.NewGameFragment;
|
|
|
|
public class HomeFragment extends Fragment {
|
|
|
|
private HomeViewModel homeViewModel;
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
|
ViewGroup container, Bundle savedInstanceState) {
|
|
homeViewModel =
|
|
ViewModelProviders.of(this).get(HomeViewModel.class);
|
|
final View root = inflater.inflate(R.layout.fragment_home, container, false);
|
|
|
|
if (PreferenceManager.getDefaultSharedPreferences(MainActivity.INSTANCE).getString("token", null) != null) {
|
|
//noinspection ConstantConditions
|
|
getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new NewGameFragment(), "Nouvelle partie").commit();
|
|
return root;
|
|
}
|
|
|
|
TextView textView = root.findViewById(R.id.text_home);
|
|
textView.setText(R.string.welcome);
|
|
|
|
Button login = root.findViewById(R.id.login);
|
|
login.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
final EditText token = root.findViewById(R.id.token);
|
|
|
|
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
Looper.prepare();
|
|
URL url = new URL("http://galaxyoyo.com/tarot/check_token.php");
|
|
final HttpURLConnection co = (HttpURLConnection) url.openConnection();
|
|
co.setRequestMethod("POST");
|
|
co.setRequestProperty("token", token.getText().toString());
|
|
co.connect();
|
|
final int resp = co.getResponseCode();
|
|
|
|
MainActivity.INSTANCE.runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
if (resp == 403)
|
|
Toast.makeText(root.getContext(), "Utilisateur non trouvé", Toast.LENGTH_LONG).show();
|
|
else {
|
|
String content = IOUtils.readLines(co.getInputStream(), StandardCharsets.UTF_8).get(0);
|
|
HashMap<String, Integer> map = new Gson().fromJson(content, new TypeToken<HashMap<String, Integer>>(){}.getType());
|
|
//noinspection ConstantConditions
|
|
int id = map.getOrDefault("id", 0);
|
|
PreferenceManager.getDefaultSharedPreferences(root.getContext()).edit().putString("token", token.getText().toString()).putInt("user_id", id).apply();
|
|
Player.updatePlayers();
|
|
Game.updateGames();
|
|
//noinspection ConstantConditions
|
|
getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new NewGameFragment(), "Nouvelle partie").commit();
|
|
}
|
|
} catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
});
|
|
|
|
return root;
|
|
}
|
|
} |