Skip to content

Commit

Permalink
Move TaskHandler to ViewModel
Browse files Browse the repository at this point in the history
This should make it possible to properly cancel the running barcode
generation threads on rotation and prevent CPU rising on many rotations.
  • Loading branch information
TheLastProject committed Nov 26, 2024
1 parent c7a7bd5 commit 110fa53
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements
ActivityResultLauncher<Intent> mCropperLauncher;
UCrop.Options mCropperOptions;

final private TaskHandler mTasks = new TaskHandler();

// store system locale for Build.VERSION.SDK_INT < Build.VERSION_CODES.N
private Locale mSystemLocale;

Expand Down Expand Up @@ -1613,7 +1611,7 @@ public void startCropperUri(Uri sourceUri) {
}

private void generateBarcode() {
mTasks.flushTaskList(TaskHandler.TYPE.BARCODE, true, false, false);
viewModel.getTaskHandler().flushTaskList(TaskHandler.TYPE.BARCODE, true, false, false);

String cardIdString = viewModel.getLoyaltyCard().barcodeId != null ? viewModel.getLoyaltyCard().barcodeId : viewModel.getLoyaltyCard().cardId;
CatimaBarcode barcodeFormat = viewModel.getLoyaltyCard().barcodeType;
Expand All @@ -1637,13 +1635,13 @@ public void onGlobalLayout() {

Log.d(TAG, "ImageView size now known");
BarcodeImageWriterTask barcodeWriter = new BarcodeImageWriterTask(getApplicationContext(), barcodeImage, cardIdString, barcodeFormat, null, false, LoyaltyCardEditActivity.this, true);
mTasks.executeTask(TaskHandler.TYPE.BARCODE, barcodeWriter);
viewModel.getTaskHandler().executeTask(TaskHandler.TYPE.BARCODE, barcodeWriter);
}
});
} else {
Log.d(TAG, "ImageView size known known, creating barcode");
BarcodeImageWriterTask barcodeWriter = new BarcodeImageWriterTask(getApplicationContext(), barcodeImage, cardIdString, barcodeFormat, null, false, this, true);
mTasks.executeTask(TaskHandler.TYPE.BARCODE, barcodeWriter);
viewModel.getTaskHandler().executeTask(TaskHandler.TYPE.BARCODE, barcodeWriter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModel
import protect.card_locker.CatimaBarcode
import protect.card_locker.LoyaltyCard
import protect.card_locker.LoyaltyCardField
import protect.card_locker.async.TaskHandler
import java.math.BigDecimal
import java.util.Currency
import java.util.Date
Expand All @@ -14,6 +15,8 @@ class LoyaltyCardEditActivityViewModel : ViewModel() {
var initialized: Boolean = false
var hasChanged: Boolean = false

var taskHandler: TaskHandler = TaskHandler();

var addGroup: String? = null
var openSetIconMenu: Boolean = false
var loyaltyCardId: Int = 0
Expand Down

0 comments on commit 110fa53

Please sign in to comment.