That fuck shit the fascists are using
at master 164 lines 5.1 kB view raw
1package org.tm.archive.logsubmit; 2 3import android.net.Uri; 4 5import androidx.annotation.NonNull; 6import androidx.annotation.Nullable; 7import androidx.lifecycle.LiveData; 8import androidx.lifecycle.MediatorLiveData; 9import androidx.lifecycle.MutableLiveData; 10import androidx.lifecycle.ViewModel; 11import androidx.lifecycle.ViewModelProvider; 12 13import org.signal.core.util.ThreadUtil; 14import org.signal.core.util.logging.Log; 15import org.signal.core.util.tracing.Tracer; 16import org.signal.paging.LivePagedData; 17import org.signal.paging.PagedData; 18import org.signal.paging.PagingConfig; 19import org.signal.paging.PagingController; 20import org.signal.paging.ProxyPagingController; 21import org.tm.archive.database.LogDatabase; 22import org.tm.archive.dependencies.ApplicationDependencies; 23import org.tm.archive.util.SingleLiveEvent; 24 25import java.util.ArrayList; 26import java.util.List; 27import java.util.Optional; 28 29public class SubmitDebugLogViewModel extends ViewModel { 30 31 private static final String TAG = Log.tag(SubmitDebugLogViewModel.class); 32 33 private final SubmitDebugLogRepository repo; 34 private final MutableLiveData<Mode> mode; 35 private final ProxyPagingController<Long> pagingController; 36 private final List<LogLine> staticLines; 37 private final MediatorLiveData<List<LogLine>> lines; 38 private final SingleLiveEvent<Event> event; 39 private final long firstViewTime; 40 private final byte[] trace; 41 42 43 private SubmitDebugLogViewModel() { 44 this.repo = new SubmitDebugLogRepository(); 45 this.mode = new MutableLiveData<>(); 46 this.trace = Tracer.getInstance().serialize(); 47 this.pagingController = new ProxyPagingController<>(); 48 this.firstViewTime = System.currentTimeMillis(); 49 this.staticLines = new ArrayList<>(); 50 this.lines = new MediatorLiveData<>(); 51 this.event = new SingleLiveEvent<>(); 52 53 repo.getPrefixLogLines(staticLines -> { 54 this.staticLines.addAll(staticLines); 55 56 Log.blockUntilAllWritesFinished(); 57 LogDatabase.getInstance(ApplicationDependencies.getApplication()).logs().trimToSize(); 58 59 LogDataSource dataSource = new LogDataSource(ApplicationDependencies.getApplication(), staticLines, firstViewTime); 60 PagingConfig config = new PagingConfig.Builder().setPageSize(100) 61 .setBufferPages(3) 62 .setStartIndex(0) 63 .build(); 64 65 LivePagedData<Long, LogLine> pagedData = PagedData.createForLiveData(dataSource, config); 66 67 ThreadUtil.runOnMain(() -> { 68 pagingController.set(pagedData.getController()); 69 lines.addSource(pagedData.getData(), lines::setValue); 70 mode.setValue(Mode.NORMAL); 71 }); 72 }); 73 } 74 75 @NonNull LiveData<List<LogLine>> getLines() { 76 return lines; 77 } 78 79 @NonNull PagingController getPagingController() { 80 return pagingController; 81 } 82 83 @NonNull LiveData<Mode> getMode() { 84 return mode; 85 } 86 87 @NonNull LiveData<Optional<String>> onSubmitClicked() { 88 mode.postValue(Mode.SUBMITTING); 89 90 MutableLiveData<Optional<String>> result = new MutableLiveData<>(); 91 92 repo.submitLogWithPrefixLines(firstViewTime, staticLines, trace, value -> { 93 mode.postValue(Mode.NORMAL); 94 result.postValue(value); 95 }); 96 97 return result; 98 } 99 100 @NonNull LiveData<Event> getEvents() { 101 return event; 102 } 103 104 void onDiskSaveLocationReady(@Nullable Uri uri) { 105 if (uri == null) { 106 Log.w(TAG, "Null URI!"); 107 event.postValue(Event.FILE_SAVE_ERROR); 108 return; 109 } 110 111 repo.writeLogToDisk(uri, firstViewTime, success -> { 112 if (success) { 113 event.postValue(Event.FILE_SAVE_SUCCESS); 114 } else { 115 event.postValue(Event.FILE_SAVE_ERROR); 116 } 117 }); 118 } 119 120 void onQueryUpdated(@NonNull String query) { 121 throw new UnsupportedOperationException("Not yet implemented."); 122 } 123 124 void onSearchClosed() { 125 throw new UnsupportedOperationException("Not yet implemented."); 126 } 127 128 void onEditButtonPressed() { 129 throw new UnsupportedOperationException("Not yet implemented."); 130 } 131 132 void onDoneEditingButtonPressed() { 133 throw new UnsupportedOperationException("Not yet implemented."); 134 } 135 136 void onLogDeleted(@NonNull LogLine line) { 137 throw new UnsupportedOperationException("Not yet implemented."); 138 } 139 140 boolean onBackPressed() { 141 if (mode.getValue() == Mode.EDIT) { 142 mode.setValue(Mode.NORMAL); 143 return true; 144 } else { 145 return false; 146 } 147 } 148 149 enum Mode { 150 NORMAL, EDIT, SUBMITTING 151 } 152 153 enum Event { 154 FILE_SAVE_SUCCESS, FILE_SAVE_ERROR 155 } 156 157 public static class Factory extends ViewModelProvider.NewInstanceFactory { 158 @Override 159 public @NonNull<T extends ViewModel> T create(@NonNull Class<T> modelClass) { 160 //noinspection ConstantConditions 161 return modelClass.cast(new SubmitDebugLogViewModel()); 162 } 163 } 164}