aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/me/brysonsteck/wiimmfiwatcher/wiimmfi/RoomFragment.java
blob: 8de81f9a62072028d62ae882eeeb161f3b264013 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package me.brysonsteck.wiimmfiwatcher.wiimmfi;

import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;

import me.brysonsteck.wiimmfiwatcher.R;

public class RoomFragment extends Fragment {
    String display;
    String header;
    String playerLink;
    ArrayList<Player> players;
    RoomData roomData;

    public RoomFragment(String friendCode, ArrayList<Player> players, String playerLink, String display) {
        super(R.layout.room_fragment);
        this.roomData = new RoomData(players, friendCode);
        new Thread(() -> {
            this.header = roomData.getRoomHeader();
        }).start();
        this.display = display;
        this.players = players;
        this.playerLink = playerLink;

    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        FloatingActionButton refreshButton = view.findViewById(R.id.refresh_button);
        TextView headerTextView = view.findViewById(R.id.room_header_text);
        if (header == null) {
            header = "This player is not online, not inside a room or does not exist. Click the refresh button to try again, or click on the back button to enter a different friend code.";
        }
        if (roomData.error != null) {
            header = "Wiimmfi Watcher was unable to connect to the Wiimmfi servers. This could be that you are not connected to the internet, but it could be something else. Here was the error:\n\n" +
                    roomData.error.getMessage() + "\n\n" +
                    "If the error is along the lines of \"Unable to resolve host\" or \"Timeout\", you are probably having internet issues. Make sure you are connected to the internet then click the refresh button or press back to watch a new friend code.\n\n" +
                    "If the error is something other than that or if the error persists, make sure that Wiimmfi's website is currently running. Otherwise, please screenshot this screen and submit a bug report by clicking the About icon on the main page.";

        }
        headerTextView.setText(header);
        RecyclerView recyclerView = view.findViewById(R.id.player_data_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(new RoomAdapter(display, playerLink, header, players, getContext()));

        refreshButton.setOnClickListener((buttonView) -> {
            this.players.clear();
            this.header = "";
            this.roomData = roomData.refresh();
            RoomData newRoomData = roomData.refresh();
            this.players = roomData.getPlayers();
            header = newRoomData.getRoomHeader();
            if (header == null) {
                header = "This player is not online, not inside a room or does not exist. Click the refresh button to try again, or click on the back button to enter a different friend code.";
            }
            if (newRoomData.error instanceof java.net.SocketTimeoutException || newRoomData.error instanceof java.net.UnknownHostException) {
                header = "Wiimmfi Watcher was unable to connect to the Wiimmfi servers. This could be that you are not connected to the internet, but it could be something else. Here was the error:\n\n" +
                        roomData.error.getMessage() + "\n\n" +
                        "If the error is along the lines of \"Unable to resolve host\" or \"Timeout\", you are probably having internet issues. Make sure you are connected to the internet then click the refresh button or press back to watch a new friend code.\n\n" +
                        "If the error is something other than that or if the error persists, make sure that Wiimmfi's website is currently running. Otherwise, please screenshot this screen and submit a bug report by clicking the About icon on the main page.";
            }

            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            recyclerView.setAdapter(new RoomAdapter(display, playerLink, header, players, getContext()));
            headerTextView.setText(header);

        });
    }
}