软件工程今日总结

今天是项目进行的第二天
今天的目的是实现| 支持创建、编辑施工计划管理信息 | header |
| --------------- | ------ |
| | |
该功能
今天遇到的主要困难有依赖添加困难,但是总体上解决 了
data/
├── local/ # 本地数据源 (Room)
│ ├── dao/ # 数据访问对象
│ │ ├── RecordDao.kt
│ │ └── UserDao.kt
│ ├── entity/ # 数据库实体
│ │ ├── DailyRecord.kt
│ │ └── User.kt
│ └── LocalDatabase.kt # Room数据库实例

├── remote/ # 远程数据源 (Retrofit)
│ ├── api/ # API接口
│ │ └── ApiService.kt
│ ├── model/ # 网络数据模型 (DTO)
│ │ ├── DailyRecordDto.kt
│ │ └── UserDto.kt
│ └── RetrofitModule.kt # Retrofit配置

├── repository/ # 数据仓库
│ ├── RecordRepository.kt
│ └── UserRepository.kt

├── datasource/ # 数据源抽象
│ ├── LocalDataSource.kt
│ └── RemoteDataSource.kt

└── sync/ # 数据同步
└── SyncWorker.kt # WorkManager同步任务
// LocalDatabase.kt
@Database(
entities = [DailyRecord::class, User::class],
version = 1,
exportSchema = false
)
@TypeConverters(Converters::class)
abstract class LocalDatabase : RoomDatabase() {
abstract fun recordDao(): RecordDao
abstract fun userDao(): UserDao

companion object {
    @Volatile
    private var INSTANCE: LocalDatabase? = null

    fun getDatabase(context: Context): LocalDatabase {
        return INSTANCE ?: synchronized(this) {
            val instance = Room.databaseBuilder(
                context.applicationContext,
                LocalDatabase::class.java,
                "learning_db"
            ).fallbackToDestructiveMigration()
             .build()
            INSTANCE = instance
            instance
        }
    }
}

}

// entity/DailyRecord.kt
@Entity(tableName = "daily_records")
data class DailyRecord(
@PrimaryKey val date: String,
val stages: Map<String, Int>,
val total: Int,
val isSynced: Boolean = false
)

// dao/RecordDao.kt
@Dao
interface RecordDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(record: DailyRecord)

@Query("SELECT * FROM daily_records WHERE isSynced = 0")
suspend fun getUnsyncedRecords(): List<DailyRecord>

@Query("UPDATE daily_records SET isSynced = 1 WHERE date = :date")
suspend fun markAsSynced(date: String)

}
// api/ApiService.kt
interface ApiService {
@POST("records")
suspend fun submitRecord(@Body record: DailyRecordDto): Response

@GET("users/{userId}/goals")
suspend fun getWeeklyGoals(@Path("userId") userId: String): Response<List<WeeklyGoalDto>>

}

// model/DailyRecordDto.kt
data class DailyRecordDto(
val date: String,
val stages: Map<String, Int>,
val total: Int
)
// RecordRepository.kt
class RecordRepositoryImpl @Inject constructor(
private val localDataSource: LocalDataSource,
private val remoteDataSource: RemoteDataSource
) : RecordRepository {

override suspend fun getWeeklyGoals(userId: String): List<WeeklyGoal> {
    return try {
        remoteDataSource.getWeeklyGoals(userId).also { goals ->
            localDataSource.saveGoals(goals)
        }
    } catch (e: Exception) {
        localDataSource.getLocalGoals(userId)
    }
}

override suspend fun submitRecord(record: DailyRecord) {
    try {
        remoteDataSource.submitRecord(record.toDto())
        localDataSource.markAsSynced(record.date)
    } catch (e: Exception) {
        localDataSource.saveRecord(record.copy(isSynced = false))
        throw e
    }
}

}
// LocalDataSource.kt
interface LocalDataSource {
suspend fun saveRecord(record: DailyRecord)
suspend fun getUnsyncedRecords(): List
suspend fun markAsSynced(date: String)
}

// RemoteDataSource.kt
interface RemoteDataSource {
suspend fun submitRecord(record: DailyRecordDto)
suspend fun getWeeklyGoals(userId: String): List
}
// DatabaseModule.kt
@Module
@InstallIn(SingletonComponent::class)
object DatabaseModule {
@Provides
@Singleton
fun provideDatabase(context: Context): LocalDatabase {
return LocalDatabase.getDatabase(context)
}

@Provides
fun provideRecordDao(database: LocalDatabase): RecordDao {
    return database.recordDao()
}

}

// RepositoryModule.kt
@Module
@InstallIn(SingletonComponent::class)
object RepositoryModule {
@Provides
fun provideRecordRepository(
local: LocalDataSource,
remote: RemoteDataSource
): RecordRepository {
return RecordRepositoryImpl(local, remote)
}
}
以上为主要内容

posted @ 2025-04-15 23:04  C(5,3)  阅读(13)  评论(0)    收藏  举报