2021-04-30 13:40:14 -06:00
|
|
|
package com.example.wiimmterfaceandroid;
|
|
|
|
|
2021-05-04 17:59:55 -06:00
|
|
|
import android.content.Intent;
|
2021-05-02 22:38:15 -06:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
2021-05-04 17:59:55 -06:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
2021-05-02 22:38:15 -06:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2021-05-04 20:18:17 -06:00
|
|
|
import androidx.databinding.ObservableList;
|
2021-05-02 22:38:15 -06:00
|
|
|
import androidx.fragment.app.Fragment;
|
2021-05-04 20:18:17 -06:00
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2021-05-02 22:38:15 -06:00
|
|
|
|
2021-05-04 17:59:55 -06:00
|
|
|
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;
|
|
|
|
|
2021-05-02 22:38:15 -06:00
|
|
|
public class WatchCodeFragment extends Fragment {
|
|
|
|
|
2021-05-04 20:18:17 -06:00
|
|
|
public WatchCodeFragment() {
|
2021-05-04 17:59:55 -06:00
|
|
|
super(R.layout.friend_code_input_fragment);
|
2021-05-04 20:18:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-05-04 17:59:55 -06:00
|
|
|
}
|
2021-05-02 22:38:15 -06:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
|
|
super.onViewCreated(view, savedInstanceState);
|
2021-05-04 20:18:17 -06:00
|
|
|
FriendCodeViewModel viewModel = new ViewModelProvider(getActivity()).get(FriendCodeViewModel.class);
|
|
|
|
|
2021-05-04 23:31:34 -06:00
|
|
|
WatchCodeAdapter adapter = new WatchCodeAdapter(viewModel.getEntries());
|
2021-05-04 20:18:17 -06:00
|
|
|
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);
|
2021-05-04 17:59:55 -06:00
|
|
|
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);
|
|
|
|
watchButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent intent = new Intent(view.getContext(), WiimmfiActivity.class);
|
2021-05-04 20:18:17 -06:00
|
|
|
if (!isValidFriendCode(friendCode.getText().toString())) {
|
2021-05-04 17:59:55 -06:00
|
|
|
errorText.setText("ERROR: Insert a friend code in the format XXXX-XXXX-XXXX");
|
|
|
|
friendCode.setText("");
|
|
|
|
} else {
|
|
|
|
errorText.setText("");
|
2021-05-04 20:18:17 -06:00
|
|
|
viewModel.saveFriendCode("", friendCode.getText().toString());
|
2021-05-04 17:59:55 -06:00
|
|
|
// FriendCodeObj friendCodeObj = new FriendCodeObj();
|
|
|
|
// friendCodeObj.friendCode = friendCode.getText().toString();
|
|
|
|
// database.getFriendCodeDao().insert(friendCodeObj);
|
2021-05-04 20:18:17 -06:00
|
|
|
intent.putExtra("friendCode", friendCode.getText().toString());
|
2021-05-04 17:59:55 -06:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-05-04 20:18:17 -06:00
|
|
|
|
|
|
|
|
2021-05-02 22:38:15 -06:00
|
|
|
}
|
2021-04-30 13:40:14 -06:00
|
|
|
}
|