Skip to content

Commit

Permalink
contents: Continue to work on the content manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
longjunyu2 committed Aug 11, 2024
1 parent 92462cb commit a5e7095
Show file tree
Hide file tree
Showing 11 changed files with 561 additions and 33 deletions.
156 changes: 156 additions & 0 deletions app/src/main/java/com/winlator/ContentsFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package com.winlator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.winlator.contentdialog.ContentDialog;
import com.winlator.contentdialog.ContentInfoDialog;
import com.winlator.contents.ContentProfile;
import com.winlator.contents.ContentsManager;
import com.winlator.core.AppUtils;
import com.winlator.core.PreloaderDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

public class ContentsFragment extends Fragment {
private RecyclerView recyclerView;
private View emptyText;
private ContentsManager manager;
private ContentProfile.ContentType currentContentType = ContentProfile.ContentType.CONTENT_TYPE_WINE;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
manager = new ContentsManager(getContext());
manager.syncContents();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(R.string.contents);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.contents_fragment, container, false);

Spinner sContentType = layout.findViewById(R.id.SContentType);
updateContentTypeSpinner(sContentType);

recyclerView = layout.findViewById(R.id.RecyclerView);
emptyText = layout.findViewById(R.id.TVEmptyText);

View btInstallContent = layout.findViewById(R.id.BTInstallContent);
btInstallContent.setOnClickListener(v -> {
ContentDialog.confirm(getContext(), getString(R.string.do_you_want_to_install_content) + " "
+ getString(R.string.pls_make_sure_content_trustworthy) + " "
+ getString(R.string.content_suffix_is_wcp_packed_xz_zst), () -> {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
getActivity().startActivityFromFragment(this, intent, MainActivity.OPEN_FILE_REQUEST_CODE);
});
});

return layout;
}

private void updateContentTypeSpinner(Spinner spinner) {
List<String> typeList = new ArrayList<>();
for (ContentProfile.ContentType type : ContentProfile.ContentType.values())
typeList.add(type.toString());
spinner.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, typeList));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
currentContentType = ContentProfile.ContentType.values()[position];
updateContentsListView();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}

private void updateContentsListView() {
List<ContentProfile> profiles = manager.getProfiles(currentContentType);
if (profiles.isEmpty()) {
recyclerView.setVisibility(View.GONE);
emptyText.setVisibility(View.VISIBLE);
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == MainActivity.OPEN_FILE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
PreloaderDialog preloaderDialog = new PreloaderDialog(getActivity());
preloaderDialog.showOnUiThread(R.string.installing_content);
try {
ContentsManager.OnInstallFinishedCallback callback = new ContentsManager.OnInstallFinishedCallback() {
private boolean isExtracting = true;

@Override
public void onFailed(ContentsManager.InstallFailedReason reason, Exception e) {
int msgId = switch (reason) {
case ERROR_BADTAR -> R.string.file_cannot_be_recognied;
case ERROR_NOPROFILE -> R.string.profile_not_found_in_content;
case ERROR_BADPROFILE -> R.string.profile_cannot_be_recognized;
case ERROR_EXIST -> R.string.content_already_exist;
default -> R.string.unable_to_install_content;
};
requireActivity().runOnUiThread(() -> ContentDialog.alert(getContext(), getString(R.string.install_failed) + ": "
+ getString(msgId), preloaderDialog::closeOnUiThread));
}

@Override
public void onSucceed(ContentProfile profile) {
if (isExtracting) {
ContentsManager.OnInstallFinishedCallback callback1 = this;
requireActivity().runOnUiThread(() -> {
ContentInfoDialog dialog = new ContentInfoDialog(getContext(), profile);
((TextView) dialog.findViewById(R.id.BTConfirm)).setText(R.string._continue);
dialog.setOnConfirmCallback(() -> {
isExtracting = false;
manager.finishInstallContent(profile, callback1);
// TODO
});
dialog.show();
});

} else {
preloaderDialog.closeOnUiThread();
requireActivity().runOnUiThread(() -> ContentDialog.alert(getContext(), R.string.content_installed_success, null));
}
}
};
Executors.newSingleThreadExecutor().execute(() -> {
manager.extraContentFile(data.getData(), callback);
});
} catch (Exception e) {
preloaderDialog.closeOnUiThread();
AppUtils.showToast(getContext(), R.string.unable_to_import_profile);
}
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/winlator/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
case R.id.main_menu_box_rc:
show(new Box86_64RCFragment());
break;
case R.id.main_menu_contents:
show(new ContentsFragment());
break;
case R.id.main_menu_settings:
show(new SettingsFragment());
break;
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/winlator/contentdialog/ContentDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,28 @@ public static void alert(Context context, int msgResId, Runnable callback) {
dialog.show();
}

public static void alert(Context context, String msg, Runnable callback) {
ContentDialog dialog = new ContentDialog(context);
dialog.setMessage(msg);
dialog.setOnConfirmCallback(callback);
dialog.findViewById(R.id.BTCancel).setVisibility(View.GONE);
dialog.show();
}

public static void confirm(Context context, int msgResId, Runnable callback) {
ContentDialog dialog = new ContentDialog(context);
dialog.setMessage(msgResId);
dialog.setOnConfirmCallback(callback);
dialog.show();
}

public static void confirm(Context context, String msg, Runnable callback) {
ContentDialog dialog = new ContentDialog(context);
dialog.setMessage(msg);
dialog.setOnConfirmCallback(callback);
dialog.show();
}

public static void prompt(Context context, int titleResId, String defaultText, Callback<String> callback) {
ContentDialog dialog = new ContentDialog(context);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.winlator.contentdialog;

import android.content.Context;
import android.widget.TextView;

import com.winlator.R;
import com.winlator.contents.ContentProfile;

public class ContentInfoDialog extends ContentDialog {
public ContentInfoDialog(Context context, ContentProfile profile) {
super(context, R.layout.content_info_dialog);
setIcon(R.drawable.icon_about);
setTitle(R.string.content_info);

TextView tvType = findViewById(R.id.TVType);
TextView tvVersion = findViewById(R.id.TVVersion);
TextView tvVersionCode = findViewById(R.id.TVVersionCode);
TextView tvDescription = findViewById(R.id.TVDesc);
TextView tvFiles = findViewById(R.id.TVFiles);

tvType.setText(profile.type.toString());
tvVersion.setText(profile.verName);
tvVersionCode.setText(String.valueOf(profile.verCode));
tvDescription.setText(profile.desc);

StringBuilder stb = new StringBuilder();
for (String str : profile.fileList)
stb.append(str).append('\n');
tvFiles.setText(stb.toString());
}
}
40 changes: 34 additions & 6 deletions app/src/main/java/com/winlator/contents/ContentProfile.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
package com.winlator.contents;

import androidx.annotation.NonNull;

import java.util.List;

public class ContentProfile {
public static final String CONTENT_TYPE_WINE = "WINE";
public static final String CONTENT_TYPE_TURNIP = "TURNIP";
public static final String CONTENT_TYPE_VIRGL = "VIRGL";
public static final String CONTENT_TYPE_DXVK = "DXVK";
public static final String CONTENT_TYPE_VKD3D = "VKD3D";
public static final String MARK_TYPE = "type";
public static final String MARK_VERSION_NAME = "versionName";
public static final String MARK_VERSION_CODE = "versionCode";
public static final String MARK_DESC = "description";
public static final String MARK_FILE_LIST = "files";

public enum ContentType {
CONTENT_TYPE_WINE ("Wine"),
CONTENT_TYPE_TURNIP ("Turnip"),
CONTENT_TYPE_VIRGL ("VirGL"),
CONTENT_TYPE_DXVK ("DXVK"),
CONTENT_TYPE_VKD3D ("VKD3D");

final String typeName;
ContentType(String typeNmae) {
this.typeName = typeNmae;
}

@NonNull
@Override
public String toString() {
return typeName;
}

public static ContentType getTypeByName(String name) {
for (ContentType type : ContentType.values())
if (type.typeName.equals(name))
return type;
return null;
}
}

public String type;
public ContentType type;
public String verName;
public int verCode;
public String desc;
Expand Down
Loading

0 comments on commit a5e7095

Please sign in to comment.