aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java
diff options
context:
space:
mode:
authorBryson Steck <steck.bryson@gmail.com>2021-05-04 20:18:17 -0600
committerBryson Steck <steck.bryson@gmail.com>2021-05-04 20:18:17 -0600
commit50dfc57c61a01dc20a42129246b458bf90e26154 (patch)
tree4c9e38c1fb4130b566b9393b317b7c7c104a51ba /app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java
parent99643e5be84af7e8da217ed48597ceb3c9d3bc85 (diff)
downloadwiimmfi-watcher-50dfc57c61a01dc20a42129246b458bf90e26154.tar
wiimmfi-watcher-50dfc57c61a01dc20a42129246b458bf90e26154.tar.gz
wiimmfi-watcher-50dfc57c61a01dc20a42129246b458bf90e26154.tar.bz2
getting closer to implementing the recycler view
Diffstat (limited to 'app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java')
-rw-r--r--app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java85
1 files changed, 74 insertions, 11 deletions
diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java b/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java
index 55c6a63..98ef40b 100644
--- a/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java
+++ b/app/src/main/java/com/example/wiimmterfaceandroid/WatchCodeFragment.java
@@ -8,29 +8,90 @@ import android.widget.EditText;
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;
-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 com.google.android.material.textview.MaterialTextView;
-import java.util.List;
-
public class WatchCodeFragment extends Fragment {
- List<FriendCode> recentFCList;
- AppDatabase database;
- public WatchCodeFragment(AppDatabase database, List<FriendCode> recentFCList) {
+ public WatchCodeFragment() {
super(R.layout.friend_code_input_fragment);
- this.database = database;
- this.recentFCList = recentFCList;
+ }
+
+ public boolean isValidFriendCode(String friendCode) {
+ String[] friendCodeSplit = friendCode.split("-");
+ boolean valid = false;
+ if (friendCodeSplit.length == 3) {
+ for (String friendCodePart : friendCodeSplit) {
+ valid = friendCodePart.length() == 4;
+ if (!valid) {
+ break;
+ }
+ }
+ }
+ return valid;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
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);
+ }
+ );
+ 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.setLayoutManager(new LinearLayoutManager(getContext()));
+ recyclerView.setAdapter(adapter);
Button watchButton = view.findViewById(R.id.watch_button);
EditText friendCode = view.findViewById(R.id.friend_code_edit_text);
MaterialTextView errorText = view.findViewById(R.id.error_text);
@@ -38,19 +99,21 @@ public class WatchCodeFragment extends Fragment {
@Override
public void onClick(View v) {
Intent intent = new Intent(view.getContext(), WiimmfiActivity.class);
- FriendCodeViewModel friendCodeViewModel = new FriendCodeViewModel(friendCode.getText().toString());
- if (friendCodeViewModel.getFullFriendCode() == null) {
+ if (!isValidFriendCode(friendCode.getText().toString())) {
errorText.setText("ERROR: Insert a friend code in the format XXXX-XXXX-XXXX");
friendCode.setText("");
} else {
errorText.setText("");
+ viewModel.saveFriendCode("", friendCode.getText().toString());
// FriendCodeObj friendCodeObj = new FriendCodeObj();
// friendCodeObj.friendCode = friendCode.getText().toString();
// database.getFriendCodeDao().insert(friendCodeObj);
- intent.putExtra("friendCode", friendCodeViewModel.getFullFriendCode());
+ intent.putExtra("friendCode", friendCode.getText().toString());
startActivity(intent);
}
}
});
+
+
}
}