From b202760cdc2b443fd07dcee593ed7f3e09d6b77b Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Tue, 4 May 2021 23:31:34 -0600 Subject: watched friend codes work --- .../example/wiimmterfaceandroid/MainActivity.java | 28 +++--- .../wiimmterfaceandroid/RecentCodesAdapter.java | 59 ------------ .../wiimmterfaceandroid/RecentCodesFragment.java | 101 --------------------- .../wiimmterfaceandroid/WatchCodeAdapter.java | 59 ++++++++++++ .../wiimmterfaceandroid/WatchCodeFragment.java | 10 +- .../viewmodel/FriendCodeViewModel.java | 3 + .../wiimmterfaceandroid/wiimmfi/RoomData.java | 9 +- .../wiimmterfaceandroid/wiimmfi/RoomFragment.java | 8 +- .../main/res/layout/friend_code_input_fragment.xml | 2 +- .../main/res/layout/recent_friend_codes_item.xml | 12 ++- 10 files changed, 91 insertions(+), 200 deletions(-) delete mode 100644 app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesAdapter.java delete mode 100644 app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesFragment.java create mode 100644 app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeAdapter.java (limited to 'app/src/main') diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/MainActivity.java b/app/src/main/java/com/example/wiimmterfaceandroid/MainActivity.java index 71b25fa..96f1511 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/MainActivity.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/MainActivity.java @@ -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); } } \ No newline at end of file diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesAdapter.java b/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesAdapter.java deleted file mode 100644 index 7d278de..0000000 --- a/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesAdapter.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.example.wiimmterfaceandroid; - -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 java.util.List; - -public class RecentCodesAdapter extends RecyclerView.Adapter{ - ObservableArrayList entries; - OnFriendCodeClicked listener; - public interface OnFriendCodeClicked { - public void onClick(FriendCode entry); - } - - public RecentCodesAdapter (ObservableArrayList entries, OnFriendCodeClicked listener) { - this.entries = entries; - this.listener = listener; - } - - @NonNull - @Override - public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recent_friend_codes_item, parent, false); - return new ViewHolder(view); - } - - @Override - public void onBindViewHolder(@NonNull ViewHolder holder, int position) { - Button 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); - }); - } - - @Override - public int getItemCount() { - return entries.size(); - } - - class ViewHolder extends RecyclerView.ViewHolder { - public ViewHolder(@NonNull View itemView) { - super(itemView); - } - } -} diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesFragment.java b/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesFragment.java deleted file mode 100644 index bde507f..0000000 --- a/app/src/main/java/com/example/wiimmterfaceandroid/RecentCodesFragment.java +++ /dev/null @@ -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>() { - @Override - public void onChanged(ObservableList sender) { - getActivity().runOnUiThread(adapter::notifyDataSetChanged); - } - - @Override - public void onItemRangeChanged(ObservableList sender, int positionStart, int itemCount) { - getActivity().runOnUiThread(() -> { - adapter.notifyItemRangeChanged(positionStart, itemCount); - }); - } - - @Override - public void onItemRangeInserted(ObservableList sender, int positionStart, int itemCount) { - getActivity().runOnUiThread(() -> { - adapter.notifyItemRangeInserted(positionStart, itemCount); - }); - } - - @Override - public void onItemRangeMoved(ObservableList sender, int fromPosition, int toPosition, int itemCount) { - getActivity().runOnUiThread(() -> { - adapter.notifyItemMoved(fromPosition, toPosition); - }); - } - - @Override - public void onItemRangeRemoved(ObservableList 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)); - } - -} diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeAdapter.java b/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeAdapter.java new file mode 100644 index 0000000..aacd9ef --- /dev/null +++ b/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeAdapter.java @@ -0,0 +1,59 @@ +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 androidx.annotation.NonNull; +import androidx.databinding.ObservableArrayList; +import androidx.recyclerview.widget.RecyclerView; + +import com.example.wiimmterfaceandroid.model.FriendCode; +import com.example.wiimmterfaceandroid.wiimmfi.WiimmfiActivity; +import com.google.android.material.button.MaterialButton; + +public class WatchCodeAdapter extends RecyclerView.Adapter{ + ObservableArrayList entries; + OnFriendCodeClicked listener; + public interface OnFriendCodeClicked { + public void onClick(FriendCode entry); + } + + public WatchCodeAdapter(ObservableArrayList entries) { + + this.entries = entries; + } + + @NonNull + @Override + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recent_friend_codes_item, parent, false); + return new ViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { + MaterialButton fcButton = holder.itemView.findViewById(R.id.recent_friend_code_button); + FriendCode currentFC = entries.get(position); + fcButton.setText(currentFC.friendCode); + fcButton.setOnClickListener(view -> { + Intent intent = new Intent(view.getContext(), WiimmfiActivity.class); + intent.putExtra("friendCode", currentFC.friendCode); + view.getContext(); + + }); + } + + @Override + public int getItemCount() { + return entries.size(); + } + + class ViewHolder extends RecyclerView.ViewHolder { + public ViewHolder(@NonNull View itemView) { + super(itemView); + } + } +} diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java b/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java index 98ef40b..67dfdcd 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java @@ -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>() { @Override public void onChanged(ObservableList sender) { diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java b/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java index ac039ff..c7bbe7d 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java @@ -17,6 +17,8 @@ import androidx.room.RoomDatabase; import androidx.databinding.ObservableArrayList; +import java.util.LinkedHashSet; + public class FriendCodeViewModel extends AndroidViewModel { ObservableArrayList entries = new ObservableArrayList<>(); MutableLiveData 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); diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomData.java b/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomData.java index 46f35d8..3799180 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomData.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomData.java @@ -16,10 +16,10 @@ public class RoomData { public RoomData (ArrayList 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 getPlayers() { return players; } diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomFragment.java b/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomFragment.java index 9592186..e72e38a 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomFragment.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/wiimmfi/RoomFragment.java @@ -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); diff --git a/app/src/main/res/layout/friend_code_input_fragment.xml b/app/src/main/res/layout/friend_code_input_fragment.xml index a70e6df..4421a65 100644 --- a/app/src/main/res/layout/friend_code_input_fragment.xml +++ b/app/src/main/res/layout/friend_code_input_fragment.xml @@ -56,7 +56,7 @@ diff --git a/app/src/main/res/layout/recent_friend_codes_item.xml b/app/src/main/res/layout/recent_friend_codes_item.xml index 7609cdc..7a4f42a 100644 --- a/app/src/main/res/layout/recent_friend_codes_item.xml +++ b/app/src/main/res/layout/recent_friend_codes_item.xml @@ -1,16 +1,18 @@ - + android:layout_height="wrap_content">