# P2.1 Batch B - Current State Directive

**Purpose:** State snapshot for reconstituting work on Batch B refactoring  
**Owner:** Development Team  
**Created:** 2025-12-23  
**Status:** in_progress  
**Baseline:** See `docs/progress/00-STATUS.md` (v1.0.11-p3-complete)

---

## Current Work Status

**Phase:** P2.1 - Native Plugin Refactoring (Batch B)  
**Goal:** Refactor methods that validate input then delegate to services  
**Status:** ✅ **BATCH B COMPLETE** — 15 methods refactored

---

## Completed Refactorings

### ✅ Android: `requestNotificationPermissions()`

- **File:** `android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt`
- **Change:** Delegated to `PermissionManager.requestNotificationPermissions(call, activity)`
- **Implementation:**
  - Enhanced `PermissionManager.requestNotificationPermissions()` to accept Activity parameter
  - Plugin method validates activity/context, saves call, then delegates
  - Service method handles permission request logic (check if granted, request if not)
  - Uses PERMISSION_REQUEST_CODE (1001) matching plugin constant
- **Lines removed:** ~43 lines (validation and request logic moved to service)
- **Service:** `PermissionManager` (initialized in `load()`)
- **Note:** Activity parameter required for Android 13+ permission requests

### ✅ Android: `openChannelSettings()`

- **File:** `android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt`
- **Change:** Delegated to `ChannelManager.openChannelSettings(channelId)`
- **Implementation:**
  - Enhanced `ChannelManager.openChannelSettings()` to accept channelId parameter
  - Added fallback logic to app notification settings if channel-specific fails
  - Plugin method validates context, gets channelId from call, then delegates
  - Service method handles channel creation, intent creation, and fallback logic
- **Lines removed:** ~83 lines (channel creation, intent handling, fallback logic moved to service)
- **Service:** `ChannelManager` (initialized in `load()`)

### ✅ Android: `createSchedule()`

- **File:** `android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt`
- **Change:** Delegated to `ScheduleHelper.createSchedule()`
- **Implementation:**
  - Created `ScheduleHelper` Kotlin object with suspend functions for schedule operations
  - Plugin method validates input, creates Schedule entity, then delegates to helper
  - Helper function handles database upsert operation
- **Lines removed:** ~1 line (direct database call replaced with helper delegation)
- **Helper:** `ScheduleHelper` (Kotlin object with suspend function)

### ✅ Android: `updateSchedule()`

- **File:** `android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt`
- **Change:** Delegated to `ScheduleHelper.updateSchedule()`
- **Implementation:**
  - Plugin method validates input, extracts update fields, then delegates to helper
  - Helper function handles field updates and run time updates
  - Returns updated schedule entity
- **Lines removed:** ~18 lines (database update logic moved to helper)
- **Helper:** `ScheduleHelper` (Kotlin object with suspend function)

### ✅ Android: `deleteSchedule()`

- **File:** `android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt`
- **Change:** Delegated to `ScheduleHelper.deleteSchedule()`
- **Implementation:**
  - Plugin method validates schedule ID, then delegates to helper
  - Helper function handles database delete operation
- **Lines removed:** ~1 line (direct database call replaced with helper delegation)
- **Helper:** `ScheduleHelper` (Kotlin object with suspend function)

### ✅ Android: `enableSchedule()`

- **File:** `android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt`
- **Change:** Delegated to `ScheduleHelper.enableSchedule()`
- **Implementation:**
  - Plugin method validates schedule ID and enabled flag, then delegates to helper
  - Helper function handles database enabled/disabled update
- **Lines removed:** ~1 line (direct database call replaced with helper delegation)
- **Helper:** `ScheduleHelper` (Kotlin object with suspend function)

---

## Next Methods (Batch B)

### Permission Requests (Validation + Delegation)

1. **`requestExactAlarmPermission()`** - Refactored (delegated to PermissionManager)
   - **Status:** Delegated to `PermissionManager.requestExactAlarmPermission()`
   - **Implementation:**
     - Added `requestExactAlarmPermission()` method to `PermissionManager`
     - Plugin method validates context, initializes permissionManager if needed, then delegates
     - Service method handles permission checking, reflection for Android 13+, and intent creation
   - **Lines removed:** ~60 lines (permission checking and intent logic moved to service)
   - **Service:** `PermissionManager` (initialized in `load()`)

### Settings Navigation (Validation + Delegation)

2. **`openExactAlarmSettings()`** - Refactored (delegated to PermissionManager)
   - **Status:** Delegated to `PermissionManager.openExactAlarmSettings()`
   - **Implementation:**
     - Plugin method validates context, initializes permissionManager if needed, then delegates
     - Service method handles intent creation and activity launch
   - **Lines removed:** ~15 lines (intent creation and activity launch logic moved to service)
   - **Service:** `PermissionManager` (initialized in `load()`)

### Permission Checks (Validation + Delegation)

3. **`checkExactAlarmPermission()`** - Refactored (delegated to PermissionManager)
   - **Status:** Delegated to `PermissionManager.checkExactAlarmPermission()`
   - **Implementation:**
     - Added `checkExactAlarmPermission()` method to `PermissionManager`
     - Plugin method validates context, initializes permissionManager if needed, then delegates
     - Service method handles permission checking logic (canSchedule, canRequest, required)
   - **Lines removed:** ~25 lines (permission checking logic moved to service)
   - **Service:** `PermissionManager` (initialized in `load()`)

### Permission Checks (Validation + Delegation)

3. **`checkExactAlarmPermission()`** - Refactored (delegated to PermissionManager)
   - **Status:** Delegated to `PermissionManager.checkExactAlarmPermission()`
   - **Implementation:**
     - Added `checkExactAlarmPermission()` method to `PermissionManager`
     - Plugin method validates context, initializes permissionManager if needed, then delegates
     - Service method handles permission checking logic (canSchedule, canRequest, required)
   - **Lines removed:** ~25 lines (permission checking logic moved to service)
   - **Service:** `PermissionManager` (initialized in `load()`)

### Scheduling Operations (Validation + Delegation)

4. **`scheduleDailyNotification()`** - Partially refactored (cleanup logic extracted)
   - **Status:** Cleanup logic extracted to `ScheduleHelper.cleanupExistingNotificationSchedules()`
   - **Remaining:** Complex orchestration method (permission check, scheduling, prefetch, database)
   - **Note:** Full delegation would require refactoring scheduler to handle full flow
   - **Lines removed:** ~40 lines (cleanup logic moved to helper)
   - **Helper:** `ScheduleHelper` (cleanup method added)

5. **`scheduleUserNotification()`** - Refactored (database operations delegated)
   - **Status:** Database operations now use `ScheduleHelper.createSchedule()`
   - **Remaining:** Permission checking and scheduling logic (uses NotifyReceiver directly)
   - **Note:** Scheduling goes through NotifyReceiver, not DailyNotificationScheduler
   - **Lines removed:** ~1 line (direct database call replaced with helper delegation)
   - **Helper:** `ScheduleHelper` (uses existing createSchedule method)

### Callbacks (Validation + Delegation)

6. **`registerCallback()`** - Refactored (database operations delegated)
   - **Status:** Database operations now use `CallbackHelper.registerCallback()`
   - **Implementation:**
     - Created `CallbackHelper` Kotlin object with suspend functions for callback operations
     - Plugin method validates input, creates Callback entity, then delegates to helper
     - Helper function handles database upsert operation
   - **Lines removed:** ~1 line (direct database call replaced with helper delegation)
   - **Helper:** `CallbackHelper` (Kotlin object with suspend function)

### Test Helpers (Validation + Delegation)

7. **`injectInvalidTestData()`** - Refactored (test data injection delegated)
   - **Status:** Test data injection now uses `TestDataHelper` methods
   - **Implementation:**
     - Created `TestDataHelper` Kotlin object with suspend functions for test data operations
     - Plugin method validates input, then delegates to helper methods
     - Helper methods handle schedule and notification injection separately
   - **Lines removed:** ~70 lines (test data injection logic moved to helper)
   - **Helper:** `TestDataHelper` (Kotlin object with suspend functions)

8. **`testAlarm()`** - Refactored (delegated to DailyNotificationScheduler)
   - **Status:** Delegated to `DailyNotificationScheduler.testAlarm()`
   - **Implementation:**
     - Added `testAlarm()` method to `DailyNotificationScheduler` (wraps `NotifyReceiver.testAlarm()`)
     - Plugin method validates context, initializes scheduler lazily if needed, then delegates
     - Service method delegates to `NotifyReceiver.testAlarm()` for actual alarm scheduling
   - **Lines removed:** ~5 lines (direct NotifyReceiver call replaced with service delegation)
   - **Service:** `DailyNotificationScheduler` (lazy initialization, requires AlarmManager)

### Utilities (Orchestration + Delegation)

9. **`cancelAllNotifications()`** - ✅ **COMPLETE**
   - **File:** `android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt`
   - **Change:** Delegated alarm cancellation and WorkManager cancellation to `ScheduleHelper`
   - **Implementation:**
     - Added `ScheduleHelper.cancelAlarmsForSchedules()` to cancel alarms for a list of schedules
     - Added `ScheduleHelper.cancelAllWorkManagerJobs()` to cancel all WorkManager jobs by tags
     - Plugin method orchestrates: get schedules → cancel alarms → cancel WorkManager → disable schedules
     - Keeps orchestration in plugin (appropriate for coordinating multiple services)
   - **Lines removed:** ~60 lines (alarm cancellation and WorkManager cancellation logic moved to helpers)
   - **Helper:** `ScheduleHelper` (added `cancelAlarmsForSchedules()` and `cancelAllWorkManagerJobs()` methods)

---

## Service Initialization State

### Current Service Instances (in `DailyNotificationPlugin.kt`)

```kotlin
private var statusChecker: NotificationStatusChecker? = null
private var permissionManager: PermissionManager? = null
private var exactAlarmManager: DailyNotificationExactAlarmManager? = null  // ⚠️ null (deferred)
private var channelManager: ChannelManager? = null
private var scheduler: DailyNotificationScheduler? = null  // Lazy initialization (requires AlarmManager)
```

### Initialization in `load()` Method

```kotlin
db = DailyNotificationDatabase.getDatabase(context)
statusChecker = NotificationStatusChecker(context)
channelManager = ChannelManager(context)
permissionManager = PermissionManager(context, channelManager)
exactAlarmManager = null // TODO: Requires AlarmManager + DailyNotificationScheduler
```

---

## Modified Files

### `android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt`
- **Status:** Modified (unstaged)
- **Changes:**
  - Refactored `requestNotificationPermissions()` method (delegation)

### `android/src/main/java/com/timesafari/dailynotification/PermissionManager.java`
- **Status:** Modified (unstaged)
- **Changes:**
  - Enhanced `requestNotificationPermissions()` to accept Activity parameter
  - Added proper permission request logic with ActivityCompat

### `android/src/main/java/com/timesafari/dailynotification/ChannelManager.java`
- **Status:** Modified (unstaged)
- **Changes:**
  - Enhanced `openChannelSettings()` to accept channelId parameter
  - Added fallback logic to app notification settings
  - Handles channel creation if channel doesn't exist

### `android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt`
- **Status:** Modified (unstaged)
- **Changes:**
  - Created `ScheduleHelper` object with suspend functions for schedule CRUD operations
  - Added `cleanupExistingNotificationSchedules()` helper method
  - Refactored `createSchedule()` method (delegation)
  - Refactored `updateSchedule()` method (delegation)
  - Refactored `deleteSchedule()` method (delegation)
  - Refactored `enableSchedule()` method (delegation)
  - Partially refactored `scheduleDailyNotification()` (cleanup logic extracted)

---

## Reference Documentation

- **Batch B Plan:** `docs/progress/P2.1-BATCH-2.md`
- **Method-Service Map:** `docs/progress/P2.1-METHOD-SERVICE-MAP.md`
- **Batch A State:** `docs/progress/P2.1-BATCH-A-STATE.md`
- **Overall Status:** `docs/progress/00-STATUS.md`

---

**Last Updated:** 2025-12-23  
**Next Update:** After completing more Batch B methods

