-
-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contents: Continue to work on the content manager.
- Loading branch information
1 parent
92462cb
commit a5e7095
Showing
11 changed files
with
561 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/winlator/contentdialog/ContentInfoDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
app/src/main/java/com/winlator/contents/ContentProfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.