watched friend codes work
This commit is contained in:
parent
50dfc57c61
commit
b202760cdc
9 changed files with 44 additions and 153 deletions
|
@ -21,31 +21,27 @@ public class MainActivity extends AppCompatActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
FragmentContainerView fcInput = findViewById(R.id.room_fragment);
|
// FragmentContainerView fcInput = findViewById(R.id.room_fragment);
|
||||||
this.database = Room.databaseBuilder(this, AppDatabase.class, "friend-codes-db").build();
|
// this.database = Room.databaseBuilder(this, AppDatabase.class, "friend-codes-db").build();
|
||||||
new Thread(() -> {
|
// new Thread(() -> {
|
||||||
try {
|
// try {
|
||||||
Thread.sleep(1000);
|
// Thread.sleep(1000);
|
||||||
} catch (InterruptedException e) {
|
// } catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
// }
|
||||||
recentFCList.addAll(database.getFriendCodeDao().getAll());
|
// recentFCList.addAll(database.getFriendCodeDao().getAll());
|
||||||
}).start();
|
// }).start();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
setContentView(R.layout.activity_main);
|
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
getSupportFragmentManager().beginTransaction()
|
getSupportFragmentManager().beginTransaction()
|
||||||
.replace(R.id.friend_code_input_fragment, new WatchCodeFragment(), null)
|
.replace(R.id.friend_code_input_fragment, new WatchCodeFragment(), null)
|
||||||
.setReorderingAllowed(true)
|
.setReorderingAllowed(true)
|
||||||
.commit();
|
.commit();
|
||||||
getSupportFragmentManager().beginTransaction()
|
|
||||||
.replace(R.id.room_fragment, new RecentCodesFragment(), null)
|
|
||||||
.setReorderingAllowed(true)
|
|
||||||
.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
setContentView(R.layout.activity_main);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,101 +0,0 @@
|
||||||
package com.example.wiimmterfaceandroid;
|
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.View;
|
|
||||||
import android.widget.Button;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
||||||
import androidx.fragment.app.Fragment;
|
|
||||||
|
|
||||||
import com.example.wiimmterfaceandroid.database.AppDatabase;
|
|
||||||
import com.example.wiimmterfaceandroid.model.FriendCode;
|
|
||||||
import com.example.wiimmterfaceandroid.viewmodel.FriendCodeViewModel;
|
|
||||||
import com.example.wiimmterfaceandroid.wiimmfi.WiimmfiActivity;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.databinding.ObservableList;
|
|
||||||
import androidx.fragment.app.Fragment;
|
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
|
||||||
|
|
||||||
public class RecentCodesFragment extends Fragment {
|
|
||||||
|
|
||||||
public RecentCodesFragment() {
|
|
||||||
|
|
||||||
}
|
|
||||||
FriendCodeViewModel viewModel;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
||||||
super.onViewCreated(view, savedInstanceState);
|
|
||||||
viewModel = new ViewModelProvider(getActivity()).get(FriendCodeViewModel.class);
|
|
||||||
|
|
||||||
RecentCodesAdapter adapter = new RecentCodesAdapter(
|
|
||||||
viewModel.getEntries(),
|
|
||||||
(entry) -> {
|
|
||||||
viewModel.setCurrentEntry(entry);
|
|
||||||
Intent intent = new Intent(view.getContext(), WiimmfiActivity.class);
|
|
||||||
intent.putExtra("friendCode", entry.friendCode);
|
|
||||||
startActivity(intent);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
viewModel.getEntries().addOnListChangedCallback(new ObservableList.OnListChangedCallback<ObservableList<FriendCode>>() {
|
|
||||||
@Override
|
|
||||||
public void onChanged(ObservableList<FriendCode> sender) {
|
|
||||||
getActivity().runOnUiThread(adapter::notifyDataSetChanged);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onItemRangeChanged(ObservableList<FriendCode> sender, int positionStart, int itemCount) {
|
|
||||||
getActivity().runOnUiThread(() -> {
|
|
||||||
adapter.notifyItemRangeChanged(positionStart, itemCount);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onItemRangeInserted(ObservableList<FriendCode> sender, int positionStart, int itemCount) {
|
|
||||||
getActivity().runOnUiThread(() -> {
|
|
||||||
adapter.notifyItemRangeInserted(positionStart, itemCount);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onItemRangeMoved(ObservableList<FriendCode> sender, int fromPosition, int toPosition, int itemCount) {
|
|
||||||
getActivity().runOnUiThread(() -> {
|
|
||||||
adapter.notifyItemMoved(fromPosition, toPosition);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onItemRangeRemoved(ObservableList<FriendCode> sender, int positionStart, int itemCount) {
|
|
||||||
getActivity().runOnUiThread(() -> {
|
|
||||||
adapter.notifyItemRangeRemoved(positionStart, itemCount);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
RecyclerView recyclerView = view.findViewById(R.id.recent_friend_codes_recycler_view);
|
|
||||||
recyclerView.setAdapter(adapter);
|
|
||||||
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
|
||||||
|
|
||||||
// view.findViewById(R.id.fab).setOnClickListener((button) -> {
|
|
||||||
// viewModel.setCurrentEntry(null);
|
|
||||||
// getActivity().getSupportFragmentManager().beginTransaction()
|
|
||||||
// .replace(R.id.fragment_container_view, CreateOrUpdateFriendCodeFragment.class, null)
|
|
||||||
// .setReorderingAllowed(true)
|
|
||||||
// .addToBackStack(null)
|
|
||||||
// .commit();
|
|
||||||
// });
|
|
||||||
// RecyclerView recyclerView = view.findViewById(R.id.recent_friend_codes_recycler_view);
|
|
||||||
// recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
|
||||||
// recyclerView.setAdapter(new RecentCodesAdapter(recentFCList));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,31 +1,29 @@
|
||||||
package com.example.wiimmterfaceandroid;
|
package com.example.wiimmterfaceandroid;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.databinding.ObservableArrayList;
|
import androidx.databinding.ObservableArrayList;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import com.example.wiimmterfaceandroid.model.FriendCode;
|
import com.example.wiimmterfaceandroid.model.FriendCode;
|
||||||
import com.google.android.material.card.MaterialCardView;
|
import com.example.wiimmterfaceandroid.wiimmfi.WiimmfiActivity;
|
||||||
import com.google.android.material.textview.MaterialTextView;
|
import com.google.android.material.button.MaterialButton;
|
||||||
|
|
||||||
import java.util.List;
|
public class WatchCodeAdapter extends RecyclerView.Adapter<WatchCodeAdapter.ViewHolder>{
|
||||||
|
|
||||||
public class RecentCodesAdapter extends RecyclerView.Adapter<RecentCodesAdapter.ViewHolder>{
|
|
||||||
ObservableArrayList<FriendCode> entries;
|
ObservableArrayList<FriendCode> entries;
|
||||||
OnFriendCodeClicked listener;
|
OnFriendCodeClicked listener;
|
||||||
public interface OnFriendCodeClicked {
|
public interface OnFriendCodeClicked {
|
||||||
public void onClick(FriendCode entry);
|
public void onClick(FriendCode entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RecentCodesAdapter (ObservableArrayList<FriendCode> entries, OnFriendCodeClicked listener) {
|
public WatchCodeAdapter(ObservableArrayList<FriendCode> entries) {
|
||||||
|
|
||||||
this.entries = entries;
|
this.entries = entries;
|
||||||
this.listener = listener;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
@ -37,12 +35,14 @@ public class RecentCodesAdapter extends RecyclerView.Adapter<RecentCodesAdapter.
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||||
Button fcButton = holder.itemView.findViewById(R.id.recent_friend_code_button);
|
MaterialButton fcButton = holder.itemView.findViewById(R.id.recent_friend_code_button);
|
||||||
FriendCode currentFC = entries.get(position);
|
FriendCode currentFC = entries.get(position);
|
||||||
fcButton.setText(currentFC.friendCode);
|
fcButton.setText(currentFC.friendCode);
|
||||||
fcButton.setOnClickListener(view -> {
|
fcButton.setOnClickListener(view -> {
|
||||||
if (listener == null) return;
|
Intent intent = new Intent(view.getContext(), WiimmfiActivity.class);
|
||||||
listener.onClick(currentFC);
|
intent.putExtra("friendCode", currentFC.friendCode);
|
||||||
|
view.getContext();
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,15 +44,7 @@ public class WatchCodeFragment extends Fragment {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
FriendCodeViewModel viewModel = new ViewModelProvider(getActivity()).get(FriendCodeViewModel.class);
|
FriendCodeViewModel viewModel = new ViewModelProvider(getActivity()).get(FriendCodeViewModel.class);
|
||||||
|
|
||||||
RecentCodesAdapter adapter = new RecentCodesAdapter(
|
WatchCodeAdapter adapter = new WatchCodeAdapter(viewModel.getEntries());
|
||||||
viewModel.getEntries(),
|
|
||||||
(entry) -> {
|
|
||||||
viewModel.setCurrentEntry(entry);
|
|
||||||
Intent intent = new Intent(view.getContext(), WiimmfiActivity.class);
|
|
||||||
intent.putExtra("friendCode", entry.friendCode);
|
|
||||||
startActivity(intent);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
viewModel.getEntries().addOnListChangedCallback(new ObservableList.OnListChangedCallback<ObservableList<FriendCode>>() {
|
viewModel.getEntries().addOnListChangedCallback(new ObservableList.OnListChangedCallback<ObservableList<FriendCode>>() {
|
||||||
@Override
|
@Override
|
||||||
public void onChanged(ObservableList<FriendCode> sender) {
|
public void onChanged(ObservableList<FriendCode> sender) {
|
||||||
|
|
|
@ -17,6 +17,8 @@ import androidx.room.RoomDatabase;
|
||||||
|
|
||||||
import androidx.databinding.ObservableArrayList;
|
import androidx.databinding.ObservableArrayList;
|
||||||
|
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
|
||||||
public class FriendCodeViewModel extends AndroidViewModel {
|
public class FriendCodeViewModel extends AndroidViewModel {
|
||||||
ObservableArrayList<FriendCode> entries = new ObservableArrayList<>();
|
ObservableArrayList<FriendCode> entries = new ObservableArrayList<>();
|
||||||
MutableLiveData<Boolean> saving = new MutableLiveData<>();
|
MutableLiveData<Boolean> saving = new MutableLiveData<>();
|
||||||
|
@ -62,6 +64,7 @@ public class FriendCodeViewModel extends AndroidViewModel {
|
||||||
newEntry.name = name;
|
newEntry.name = name;
|
||||||
newEntry.friendCode = friendCode;
|
newEntry.friendCode = friendCode;
|
||||||
db.getFriendCodeDao().insert(newEntry);
|
db.getFriendCodeDao().insert(newEntry);
|
||||||
|
entries.add(newEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
saving.postValue(false);
|
saving.postValue(false);
|
||||||
|
|
|
@ -16,10 +16,10 @@ public class RoomData {
|
||||||
|
|
||||||
public RoomData (ArrayList<Player> players, String playerLink, String friendCode) {
|
public RoomData (ArrayList<Player> players, String playerLink, String friendCode) {
|
||||||
this.friendCode = friendCode;
|
this.friendCode = friendCode;
|
||||||
this.playerLink = getPlayerLink();
|
getPlayerLink();
|
||||||
Document doc = null;
|
Document doc = null;
|
||||||
|
|
||||||
if (playerLink == null) {
|
if (this.playerLink == null) {
|
||||||
System.out.println("The player link is null for some reason");
|
System.out.println("The player link is null for some reason");
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ public class RoomData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPlayerLink() {
|
public void getPlayerLink() {
|
||||||
try {
|
try {
|
||||||
Document doc = null;
|
Document doc = null;
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ public class RoomData {
|
||||||
System.out.println("Found friend code");
|
System.out.println("Found friend code");
|
||||||
playerLink = data.split("\"")[3];
|
playerLink = data.split("\"")[3];
|
||||||
System.out.println("Player link: " + playerLink);
|
System.out.println("Player link: " + playerLink);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,7 +118,6 @@ public class RoomData {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return playerLink;
|
|
||||||
}
|
}
|
||||||
public ArrayList<Player> getPlayers() { return players; }
|
public ArrayList<Player> getPlayers() { return players; }
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ public class RoomFragment extends Fragment {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
FloatingActionButton refreshButton = view.findViewById(R.id.refresh_button);
|
FloatingActionButton refreshButton = view.findViewById(R.id.refresh_button);
|
||||||
TextView headerTextView = view.findViewById(R.id.room_header_text);
|
TextView headerTextView = view.findViewById(R.id.room_header_text);
|
||||||
if (header == null || playerLink == null || players == null) {
|
if (players == 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.";
|
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.";
|
||||||
}
|
}
|
||||||
headerTextView.setText(header);
|
headerTextView.setText(header);
|
||||||
|
@ -51,10 +51,10 @@ public class RoomFragment extends Fragment {
|
||||||
roomData = roomData.refresh();
|
roomData = roomData.refresh();
|
||||||
RoomData newRoomData = roomData.refresh();
|
RoomData newRoomData = roomData.refresh();
|
||||||
players = roomData.getPlayers();
|
players = roomData.getPlayers();
|
||||||
playerLink = roomData.getPlayerLink();
|
// playerLink = roomData.getPlayerLink();
|
||||||
String otherPlayerLink = newRoomData.getPlayerLink();
|
// String otherPlayerLink = newRoomData.getPlayerLink();
|
||||||
header = newRoomData.getRoomHeader();
|
header = newRoomData.getRoomHeader();
|
||||||
if (playerLink == null) {
|
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.";
|
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.";
|
||||||
}
|
}
|
||||||
headerTextView.setText(header);
|
headerTextView.setText(header);
|
||||||
|
|
|
@ -56,7 +56,7 @@
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/recent_friend_codes_recycler_view"
|
android:id="@+id/recent_friend_codes_recycler_view"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="match_parent"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/textView3"
|
app:layout_constraintTop_toBottomOf="@+id/textView3"
|
||||||
tools:layout_editor_absoluteX="15dp" />
|
tools:layout_editor_absoluteX="15dp" />
|
||||||
|
|
|
@ -1,16 +1,18 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/recent_friend_code_button"
|
android:id="@+id/recent_friend_code_button"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:backgroundTint="#949494"
|
||||||
android:text="Button"
|
android:text="Button"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent" />
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Reference in a new issue