|
1 | 1 | package ceui.lisa.fragments; |
2 | 2 |
|
| 3 | +import android.content.Intent; |
| 4 | +import android.net.Uri; |
| 5 | +import android.os.Build; |
3 | 6 | import android.os.Bundle; |
4 | 7 | import android.text.TextUtils; |
5 | 8 | import android.view.View; |
|
13 | 16 | import androidx.fragment.app.Fragment; |
14 | 17 | import androidx.fragment.app.FragmentPagerAdapter; |
15 | 18 | import androidx.viewpager.widget.ViewPager; |
| 19 | + |
| 20 | +import com.blankj.utilcode.util.UriUtils; |
| 21 | +import com.google.gson.reflect.TypeToken; |
| 22 | + |
| 23 | +import java.lang.reflect.Type; |
| 24 | +import java.util.List; |
| 25 | + |
16 | 26 | import ceui.lisa.R; |
| 27 | +import ceui.lisa.activities.BaseActivity; |
17 | 28 | import ceui.lisa.activities.Shaft; |
| 29 | +import ceui.lisa.database.AppDatabase; |
| 30 | +import ceui.lisa.database.MuteEntity; |
18 | 31 | import ceui.lisa.databinding.ViewpagerWithTablayoutBinding; |
| 32 | +import ceui.lisa.download.IllustDownload; |
| 33 | +import ceui.lisa.interfaces.Callback; |
| 34 | +import ceui.lisa.utils.Common; |
19 | 35 | import ceui.lisa.utils.Dev; |
20 | 36 | import ceui.lisa.utils.MyOnTabSelectedListener; |
21 | 37 | import ceui.lisa.utils.Params; |
22 | 38 |
|
| 39 | +import static android.app.Activity.RESULT_OK; |
| 40 | +import static android.provider.DocumentsContract.EXTRA_INITIAL_URI; |
| 41 | + |
23 | 42 | public class FragmentViewPager extends BaseFragment<ViewpagerWithTablayoutBinding> { |
24 | 43 |
|
| 44 | + private static final int REQUEST_CODE_IMPORT_MUTE = 20082; |
| 45 | + private static final String MUTE_RECORDS_FILE_NAME = "Shaft-MuteRecords.json"; |
| 46 | + |
25 | 47 | private String title; |
26 | 48 | private ListFragment[] mFragments = null; |
27 | 49 |
|
@@ -65,7 +87,7 @@ public void initView() { |
65 | 87 | new FragmentMutedObjects(), |
66 | 88 | }; |
67 | 89 | baseBind.toolbar.inflateMenu(R.menu.delete_and_add); |
68 | | - baseBind.toolbar.setOnMenuItemClickListener((Toolbar.OnMenuItemClickListener) mFragments[0]); |
| 90 | + setMuteMenuListener(mFragments[0]); |
69 | 91 | baseBind.toolbarTitle.setText(R.string.muted_history); |
70 | 92 | baseBind.viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) { |
71 | 93 | @NonNull |
@@ -93,7 +115,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse |
93 | 115 |
|
94 | 116 | @Override |
95 | 117 | public void onPageSelected(int position) { |
96 | | - baseBind.toolbar.setOnMenuItemClickListener((Toolbar.OnMenuItemClickListener) mFragments[position]); |
| 118 | + setMuteMenuListener(mFragments[position]); |
97 | 119 | if (position == 0) { |
98 | 120 | baseBind.toolbar.getMenu().clear(); |
99 | 121 | baseBind.toolbar.inflateMenu(R.menu.delete_and_add); |
@@ -160,4 +182,91 @@ public void forceRefresh() { |
160 | 182 | e.printStackTrace(); |
161 | 183 | } |
162 | 184 | } |
| 185 | + |
| 186 | + private void setMuteMenuListener(ListFragment delegate) { |
| 187 | + baseBind.toolbar.setOnMenuItemClickListener(item -> { |
| 188 | + if (item.getItemId() == R.id.action_export_mute) { |
| 189 | + exportMuteRecords(); |
| 190 | + return true; |
| 191 | + } else if (item.getItemId() == R.id.action_import_mute) { |
| 192 | + pickMuteRecordsFile(); |
| 193 | + return true; |
| 194 | + } |
| 195 | + return ((Toolbar.OnMenuItemClickListener) delegate).onMenuItemClick(item); |
| 196 | + }); |
| 197 | + } |
| 198 | + |
| 199 | + private void exportMuteRecords() { |
| 200 | + new Thread(() -> { |
| 201 | + List<MuteEntity> all = AppDatabase.getAppDatabase(mContext) |
| 202 | + .searchDao().getAllMuteEntities(); |
| 203 | + if (all == null || all.isEmpty()) { |
| 204 | + mActivity.runOnUiThread(() -> |
| 205 | + Common.showToast(getString(R.string.mute_records_export_empty))); |
| 206 | + return; |
| 207 | + } |
| 208 | + String json = Shaft.sGson.toJson(all); |
| 209 | + mActivity.runOnUiThread(() -> |
| 210 | + IllustDownload.downloadBackupFile((BaseActivity<?>) mActivity, |
| 211 | + MUTE_RECORDS_FILE_NAME, json, new Callback<Uri>() { |
| 212 | + @Override |
| 213 | + public void doSomething(Uri t) { |
| 214 | + Common.showToast(getString(R.string.mute_records_export_success, all.size())); |
| 215 | + } |
| 216 | + })); |
| 217 | + }).start(); |
| 218 | + } |
| 219 | + |
| 220 | + private void pickMuteRecordsFile() { |
| 221 | + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
| 222 | + intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 223 | + intent.setType("*/*"); |
| 224 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 225 | + Uri initialUri = Uri.parse("content://com.android.externalstorage.documents/document/primary:" |
| 226 | + + "Download%2fShaftBackups%2f" + MUTE_RECORDS_FILE_NAME); |
| 227 | + intent.putExtra(EXTRA_INITIAL_URI, initialUri); |
| 228 | + } |
| 229 | + startActivityForResult(intent, REQUEST_CODE_IMPORT_MUTE); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
| 234 | + super.onActivityResult(requestCode, resultCode, data); |
| 235 | + if (requestCode == REQUEST_CODE_IMPORT_MUTE && resultCode == RESULT_OK && data != null) { |
| 236 | + Uri uri = data.getData(); |
| 237 | + if (uri == null) { |
| 238 | + Common.showToast(getString(R.string.mute_records_import_no_file)); |
| 239 | + return; |
| 240 | + } |
| 241 | + new Thread(() -> { |
| 242 | + try { |
| 243 | + String fileString = new String(UriUtils.uri2Bytes(uri)); |
| 244 | + Type listType = new TypeToken<List<MuteEntity>>() {}.getType(); |
| 245 | + List<MuteEntity> entities = Shaft.sGson.fromJson(fileString, listType); |
| 246 | + if (entities == null || entities.isEmpty()) { |
| 247 | + mActivity.runOnUiThread(() -> |
| 248 | + Common.showToast(getString(R.string.mute_records_import_invalid))); |
| 249 | + return; |
| 250 | + } |
| 251 | + int imported = 0; |
| 252 | + for (MuteEntity entity : entities) { |
| 253 | + if (entity == null || entity.getTagJson() == null || entity.getTagJson().isEmpty()) { |
| 254 | + continue; |
| 255 | + } |
| 256 | + AppDatabase.getAppDatabase(mContext).searchDao().insertMuteTag(entity); |
| 257 | + imported++; |
| 258 | + } |
| 259 | + int finalImported = imported; |
| 260 | + mActivity.runOnUiThread(() -> { |
| 261 | + forceRefresh(); |
| 262 | + Common.showToast(getString(R.string.mute_records_import_success, finalImported)); |
| 263 | + }); |
| 264 | + } catch (Exception e) { |
| 265 | + e.printStackTrace(); |
| 266 | + mActivity.runOnUiThread(() -> |
| 267 | + Common.showToast(getString(R.string.mute_records_import_failed, String.valueOf(e.getMessage())))); |
| 268 | + } |
| 269 | + }).start(); |
| 270 | + } |
| 271 | + } |
163 | 272 | } |
0 commit comments