watched friend codes work

This commit is contained in:
Bryson Steck 2021-05-04 23:31:34 -06:00
parent 50dfc57c61
commit b202760cdc
9 changed files with 44 additions and 153 deletions

View file

@ -21,31 +21,27 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentContainerView fcInput = findViewById(R.id.room_fragment);
this.database = Room.databaseBuilder(this, AppDatabase.class, "friend-codes-db").build();
new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
recentFCList.addAll(database.getFriendCodeDao().getAll());
}).start();
// FragmentContainerView fcInput = findViewById(R.id.room_fragment);
// this.database = Room.databaseBuilder(this, AppDatabase.class, "friend-codes-db").build();
// new Thread(() -> {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// recentFCList.addAll(database.getFriendCodeDao().getAll());
// }).start();
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.friend_code_input_fragment, new WatchCodeFragment(), null)
.setReorderingAllowed(true)
.commit();
getSupportFragmentManager().beginTransaction()
.replace(R.id.room_fragment, new RecentCodesFragment(), null)
.setReorderingAllowed(true)
.commit();
}
setContentView(R.layout.activity_main);
}
}

View file

@ -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));
}
}

View file

@ -1,31 +1,29 @@
package com.example.wiimmterfaceandroid;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wiimmterfaceandroid.model.FriendCode;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.textview.MaterialTextView;
import com.example.wiimmterfaceandroid.wiimmfi.WiimmfiActivity;
import com.google.android.material.button.MaterialButton;
import java.util.List;
public class RecentCodesAdapter extends RecyclerView.Adapter<RecentCodesAdapter.ViewHolder>{
public class WatchCodeAdapter extends RecyclerView.Adapter<WatchCodeAdapter.ViewHolder>{
ObservableArrayList<FriendCode> entries;
OnFriendCodeClicked listener;
public interface OnFriendCodeClicked {
public void onClick(FriendCode entry);
}
public RecentCodesAdapter (ObservableArrayList<FriendCode> entries, OnFriendCodeClicked listener) {
public WatchCodeAdapter(ObservableArrayList<FriendCode> entries) {
this.entries = entries;
this.listener = listener;
}
@NonNull
@ -37,12 +35,14 @@ public class RecentCodesAdapter extends RecyclerView.Adapter<RecentCodesAdapter.
@Override
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);
fcButton.setText(currentFC.friendCode);
fcButton.setOnClickListener(view -> {
if (listener == null) return;
listener.onClick(currentFC);
Intent intent = new Intent(view.getContext(), WiimmfiActivity.class);
intent.putExtra("friendCode", currentFC.friendCode);
view.getContext();
});
}

View file

@ -44,15 +44,7 @@ public class WatchCodeFragment extends Fragment {
super.onViewCreated(view, savedInstanceState);
FriendCodeViewModel 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);
}
);
WatchCodeAdapter adapter = new WatchCodeAdapter(viewModel.getEntries());
viewModel.getEntries().addOnListChangedCallback(new ObservableList.OnListChangedCallback<ObservableList<FriendCode>>() {
@Override
public void onChanged(ObservableList<FriendCode> sender) {

View file

@ -17,6 +17,8 @@ import androidx.room.RoomDatabase;
import androidx.databinding.ObservableArrayList;
import java.util.LinkedHashSet;
public class FriendCodeViewModel extends AndroidViewModel {
ObservableArrayList<FriendCode> entries = new ObservableArrayList<>();
MutableLiveData<Boolean> saving = new MutableLiveData<>();
@ -62,6 +64,7 @@ public class FriendCodeViewModel extends AndroidViewModel {
newEntry.name = name;
newEntry.friendCode = friendCode;
db.getFriendCodeDao().insert(newEntry);
entries.add(newEntry);
}
saving.postValue(false);

View file

@ -16,10 +16,10 @@ public class RoomData {
public RoomData (ArrayList<Player> players, String playerLink, String friendCode) {
this.friendCode = friendCode;
this.playerLink = getPlayerLink();
getPlayerLink();
Document doc = null;
if (playerLink == null) {
if (this.playerLink == null) {
System.out.println("The player link is null for some reason");
} else {
@ -87,7 +87,7 @@ public class RoomData {
}
}
public String getPlayerLink() {
public void getPlayerLink() {
try {
Document doc = null;
@ -109,7 +109,7 @@ public class RoomData {
System.out.println("Found friend code");
playerLink = data.split("\"")[3];
System.out.println("Player link: " + playerLink);
break;
}
}
}
@ -118,7 +118,6 @@ public class RoomData {
} catch (Exception e) {
e.printStackTrace();
}
return playerLink;
}
public ArrayList<Player> getPlayers() { return players; }

View file

@ -36,7 +36,7 @@ public class RoomFragment extends Fragment {
super.onViewCreated(view, savedInstanceState);
FloatingActionButton refreshButton = view.findViewById(R.id.refresh_button);
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.";
}
headerTextView.setText(header);
@ -51,10 +51,10 @@ public class RoomFragment extends Fragment {
roomData = roomData.refresh();
RoomData newRoomData = roomData.refresh();
players = roomData.getPlayers();
playerLink = roomData.getPlayerLink();
String otherPlayerLink = newRoomData.getPlayerLink();
// playerLink = roomData.getPlayerLink();
// String otherPlayerLink = newRoomData.getPlayerLink();
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.";
}
headerTextView.setText(header);

View file

@ -56,7 +56,7 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recent_friend_codes_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
tools:layout_editor_absoluteX="15dp" />

View file

@ -1,16 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content">
<Button
android:id="@+id/recent_friend_code_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:backgroundTint="#949494"
android:text="Button"
app:layout_constraintBottom_toBottomOf="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>